From a893f3da59a8079188919112b241ff76b9003cb8 Mon Sep 17 00:00:00 2001 From: Kateryna Date: Tue, 17 Dec 2024 19:23:34 +0100 Subject: [PATCH] fixed all test and mentor comments --- src/App.tsx | 4 +- src/components/NewCommentForm.tsx | 38 +++++++++----- src/components/PostDetails.tsx | 85 +++++++++++++++---------------- 3 files changed, 68 insertions(+), 59 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 07bb5e78e..d03477acd 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -71,11 +71,13 @@ export const App = () => { const addComment = ({ name, email, body, postId }: Comment) => { setLoading('addComment'); - servicesComments + return servicesComments .createComment({ name, email, body, postId }) .then(newComment => { setComments(currentComments => [...currentComments, newComment]); + setIsError(false); }) + .catch(() => setIsError(true)) .finally(() => setLoading('')); }; diff --git a/src/components/NewCommentForm.tsx b/src/components/NewCommentForm.tsx index 265058a79..5b10d982a 100644 --- a/src/components/NewCommentForm.tsx +++ b/src/components/NewCommentForm.tsx @@ -22,11 +22,14 @@ export const NewCommentForm: React.FC = ({ const [body, setBody] = useState(''); const [bodyError, setBodyError] = useState(false); + const [isReset, setIsReset] = useState(false); + function reset() { setBody(''); } function resetAllFields() { + setIsReset(true); setName(''); setEmail(''); setBody(''); @@ -50,15 +53,19 @@ export const NewCommentForm: React.FC = ({ const handleSubmit = (event: React.MouseEvent) => { event.preventDefault(); - if (name.length === 0) { + const trimmedName = name.trim(); + const trimmedEmail = email.trim(); + const trimmedBody = body.trim(); + + if (name.length === 0 || !trimmedName) { setTitleError(true); } - if (email.length === 0) { + if (email.length === 0 || !trimmedEmail) { setEmailError(true); } - if (body.length === 0) { + if (body.length === 0 || !trimmedBody) { setBodyError(true); } @@ -89,16 +96,17 @@ export const NewCommentForm: React.FC = ({ value={name} id="comment-author-name" placeholder="Name Surname" - className={classNames('input', { 'is-danger': titleError })} + className={classNames('input', { + 'is-danger': titleError && !isReset, + })} onChange={handleNameChange} /> - {name} - {titleError && ( + {titleError && !isReset && ( = ({ )} - {titleError && ( + {titleError && !isReset && (

Name is required

@@ -122,12 +130,14 @@ export const NewCommentForm: React.FC = ({
@@ -135,7 +145,7 @@ export const NewCommentForm: React.FC = ({ - {emailError && ( + {emailError && !isReset && ( = ({ )}
- {emailError && ( + {emailError && !isReset && (

Email is required

@@ -163,12 +173,14 @@ export const NewCommentForm: React.FC = ({ name="body" value={body} placeholder="Type comment here" - className={classNames('textarea', { 'is-danger': bodyError })} + className={classNames('textarea', { + 'is-danger': bodyError && !isReset, + })} onChange={handleBodyChange} /> - {bodyError && ( + {bodyError && !isReset && (

Enter some text

diff --git a/src/components/PostDetails.tsx b/src/components/PostDetails.tsx index 5782dd996..5e546e4a2 100644 --- a/src/components/PostDetails.tsx +++ b/src/components/PostDetails.tsx @@ -49,57 +49,52 @@ export const PostDetails: React.FC = ({

{currentPost.body}

- {isError && ( -
- Something went wrong -
- )} {loadingComments && } - {!loadingComments && - (comments.length === 0 ? ( - <> -

- No comments yet -

- - ) : ( +
+ {isError && ( +
+ Something went wrong +
+ )} + {!loadingComments && !isError && ( <> -
+ {comments.length === 0 ? ( +

+ No comments yet +

+ ) : (

Comments:

- {comments.map(comment => ( - <> -
-
- {comment.body} -
- - - ))} -
+
+ {comment.body} +
+ + ))} - ))} + )} + {!loadingComments && !isError && (isWritePostButton ? (