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}
+
+
+ ))}
+
+ >
+ )}
+
+ );
+};
+
+export default Comments;
diff --git a/client/src/components/likes/Likes.js b/client/src/components/likes/Likes.js
new file mode 100644
index 0000000..1e3179c
--- /dev/null
+++ b/client/src/components/likes/Likes.js
@@ -0,0 +1,46 @@
+// import React, { useEffect } from 'react';
+import { useParams } from 'react-router';
+import { useDispatch, useSelector } from 'react-redux';
+import { deleteLike, postLike } from '../../redux/likes/likesSlice';
+
+const Likes = () => {
+ const dispatch = useDispatch();
+ const { value, isLoading, error } = useSelector((state) => state.likes);
+ const { userId, collId, itemId } = useParams();
+ const { user } = useSelector((state) => state.auth);
+ const currentLike = value?.find(
+ ({ item, author }) => item === itemId && author === user?.user._id,
+ );
+ const handleLike = (e) => {
+ e.preventDefault();
+ dispatch(currentLike
+ ? deleteLike({
+ userId, collId, itemId, likeId: currentLike._id,
+ })
+ : postLike({ userId, collId, itemId }));
+ };
+
+ return (
+
+ {error &&
{error}
}
+ {isLoading &&
Loading...
}
+ {value && (
+ <>
+
+ Likes:
+ {value?.length}
+
+
{ currentLike ? 'Liked' : 'Disliked' }
+
+ >
+ )}
+
+ );
+};
+
+export default Likes;
diff --git a/client/src/routes/ItemPage.js b/client/src/routes/ItemPage.js
index 9b41089..2ebfdbb 100644
--- a/client/src/routes/ItemPage.js
+++ b/client/src/routes/ItemPage.js
@@ -2,18 +2,28 @@ import { useEffect } from 'react';
import { useParams } from 'react-router';
import { useDispatch } from 'react-redux';
import ItemDetails from '../components/items/ItemDetails';
+import Likes from '../components/likes/Likes';
+import Comments from '../components/comments/Comments';
import { getItem } from '../redux/items/itemsSlice';
+import { getComments } from '../redux/comments/commentsSlice';
+import { getLikes } from '../redux/likes/likesSlice';
const ItemPage = () => {
const dispatch = useDispatch();
const { userId, collId, itemId } = useParams();
useEffect(() => {
+ dispatch(getLikes({ userId, collId, itemId }));
+ dispatch(getComments({ userId, collId, itemId }));
dispatch(getItem({ userId, collId, itemId }));
}, [dispatch, collId, userId, itemId]);
return (
-
+ <>
+
+
+
+ >
);
};