Skip to content

Commit

Permalink
Create Likes and Comments component, add them under ItemDetails so th…
Browse files Browse the repository at this point in the history
…at the users can see likes/comments when they click on a specific item
  • Loading branch information
Zilola-Nazarova committed May 20, 2024
1 parent 465eec7 commit 5888ebb
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 1 deletion.
51 changes: 51 additions & 0 deletions client/src/components/comments/Comments.js
Original file line number Diff line number Diff line change
@@ -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 (
<div>
{error && <p>{error}</p>}
{isLoading && <p>Loading...</p>}
{value && (
<>
<h4>Comments:</h4>
{value?.map((comment) => (
<li key={uuidv4()}>
<p>{comment.text}</p>
<button type="button" onClick={() => removeComment(comment._id)}>Delete</button>
</li>
))}
<form onSubmit={comment}>
<textarea
value={commentData.text}
onChange={(e) => setCommentData({ text: e.target.value })}
/>
<button type="submit">Comment</button>
</form>
</>
)}
</div>
);
};

export default Comments;
46 changes: 46 additions & 0 deletions client/src/components/likes/Likes.js
Original file line number Diff line number Diff line change
@@ -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 (
<div>
{error && <p>{error}</p>}
{isLoading && <p>Loading...</p>}
{value && (
<>
<h4>
Likes:
<span>{value?.length}</span>
</h4>
<h5>{ currentLike ? 'Liked' : 'Disliked' }</h5>
<button
type="button"
onClick={handleLike}
>
{ currentLike ? 'Dislike' : 'Like' }
</button>
</>
)}
</div>
);
};

export default Likes;
12 changes: 11 additions & 1 deletion client/src/routes/ItemPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<ItemDetails />
<>
<ItemDetails />
<Likes />
<Comments />
</>
);
};

Expand Down

0 comments on commit 5888ebb

Please sign in to comment.