From 5888ebbcbde7a7a92761f29e3b55777d75d14802 Mon Sep 17 00:00:00 2001 From: Zilola-Nazarova <61951420+Zilola-Nazarova@users.noreply.github.com> Date: Mon, 20 May 2024 06:19:53 +0500 Subject: [PATCH] Create Likes and Comments component, add them under ItemDetails so that the users can see likes/comments when they click on a specific item --- client/src/components/comments/Comments.js | 51 ++++++++++++++++++++++ client/src/components/likes/Likes.js | 46 +++++++++++++++++++ client/src/routes/ItemPage.js | 12 ++++- 3 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 client/src/components/comments/Comments.js create mode 100644 client/src/components/likes/Likes.js diff --git a/client/src/components/comments/Comments.js b/client/src/components/comments/Comments.js new file mode 100644 index 0000000..45d4ae0 --- /dev/null +++ b/client/src/components/comments/Comments.js @@ -0,0 +1,51 @@ +import { useState } from 'react'; +import { useParams } from 'react-router'; +import { v4 as uuidv4 } from 'uuid'; +import { useDispatch, useSelector } from 'react-redux'; +import { deleteComment, postComment } from '../../redux/comments/commentsSlice'; + +const Comments = () => { + const dispatch = useDispatch(); + const { userId, collId, itemId } = useParams(); + const { value, isLoading, error } = useSelector((state) => state.comments); + const [commentData, setCommentData] = useState({ text: '' }); + const clear = () => setCommentData({ text: '' }); + const comment = (e) => { + e.preventDefault(); + dispatch(postComment({ + userId, collId, itemId, newComment: commentData, + })); + clear(); + }; + const removeComment = (id) => { + dispatch(deleteComment({ + userId, collId, itemId, commentId: id, + })); + }; + return ( +
+ {error &&

{error}

} + {isLoading &&

Loading...

} + {value && ( + <> +

Comments:

+ {value?.map((comment) => ( +
  • +

    {comment.text}

    + +
  • + ))} +
    +