From c0c441e1ac23a62ad375fe7144fbc6bb6b04d250 Mon Sep 17 00:00:00 2001 From: Igor <4430704@gmail.com> Date: Thu, 5 Dec 2024 17:07:43 +0200 Subject: [PATCH] fixed solution --- src/App.tsx | 14 +++++++------- src/components/Comment.tsx | 15 +++++++++++++-- src/components/PostDetails.tsx | 16 ++++++++++++---- src/components/PostItem.tsx | 4 ++-- src/components/UserSelector.tsx | 27 ++++++++++++++++++++++++-- src/hooks/useComments.ts | 34 +++++++++++++++------------------ src/hooks/usePosts.ts | 20 +++++++------------ src/hooks/useUsers.ts | 7 ++----- 8 files changed, 83 insertions(+), 54 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index 62961a9c7..e94f0b9b5 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -13,14 +13,14 @@ import { usePosts } from './hooks/usePosts'; import { useState } from 'react'; export const App: React.FC = () => { - const [isShowForm, setIsShowForm] = useState(false); + const [isFormShown, setIsFormShown] = useState(false); const { users, currentUser, setCurrentUser } = useUsers(); const { posts, currentPost, setCurrentPost, isPostsLoading, isPostsError } = usePosts(currentUser?.id || null); - const isShowPostsBlock = currentUser && !isPostsError && !isPostsLoading; + const isPostsBlockShown = currentUser && !isPostsError && !isPostsLoading; return (
@@ -53,8 +53,8 @@ export const App: React.FC = () => { )} - {isShowPostsBlock && - (posts.length === 0 ? ( + {isPostsBlockShown && + (!posts.length ? (
{ posts={posts} currentPost={currentPost} setCurrentPost={setCurrentPost} - setIsShowForm={setIsShowForm} + setIsShowForm={setIsFormShown} /> ))}
@@ -83,8 +83,8 @@ export const App: React.FC = () => {
)} diff --git a/src/components/Comment.tsx b/src/components/Comment.tsx index e5aa71377..f60709b41 100644 --- a/src/components/Comment.tsx +++ b/src/components/Comment.tsx @@ -4,9 +4,14 @@ import type { Comment } from '../types/Comment'; type Props = { comment: Comment; deleteComment: (commentId: number) => void; + isCommentDeleteError: boolean; }; -const CommentItem: React.FC = ({ comment, deleteComment }) => { +const CommentItem: React.FC = ({ + comment, + deleteComment, + isCommentDeleteError, +}) => { const handleDelete = () => { deleteComment(comment.id); }; @@ -14,7 +19,7 @@ const CommentItem: React.FC = ({ comment, deleteComment }) => { return (
- + {comment.name}
); }; diff --git a/src/components/PostDetails.tsx b/src/components/PostDetails.tsx index 29c7a8206..4b1f7a8aa 100644 --- a/src/components/PostDetails.tsx +++ b/src/components/PostDetails.tsx @@ -23,15 +23,22 @@ export const PostDetails: React.FC = ({ isCommentsError, deleteComment, isCommentCreating, + isCommentDeleteError, } = useComments(currentPost.id); const isShowCommentsBlock = !isCommentsError && !isCommentsLoading; + const isNoCommentsMessageShown = isShowCommentsBlock && !comments.length; + + const isCommentsBlockShown = isShowCommentsBlock && comments.length !== 0; + + const isCommentButtonShown = isShowCommentsBlock && !isShowForm; + return (
-

{`#${currentPost.id} ${currentPost.title}`}

+

{`#${currentPost.id}: ${currentPost.title}`}

{currentPost.body}

@@ -45,13 +52,13 @@ export const PostDetails: React.FC = ({
)} - {isShowCommentsBlock && comments.length === 0 && ( + {isNoCommentsMessageShown && (

No comments yet

)} - {isShowCommentsBlock && comments.length !== 0 && ( + {isCommentsBlockShown && ( <>

Comments:{comments.length}

{comments.map(comment => ( @@ -59,12 +66,13 @@ export const PostDetails: React.FC = ({ key={comment.id} comment={comment} deleteComment={deleteComment} + isCommentDeleteError={isCommentDeleteError} /> ))} )} - {isShowCommentsBlock && !isShowForm && ( + {isCommentButtonShown && (