From 28f888560930f0af8d4ed1a61733e80a227b6e57 Mon Sep 17 00:00:00 2001 From: Philimuhire Date: Thu, 28 Nov 2024 03:04:55 +0200 Subject: [PATCH] Fix(#266): added comments and reactions --- src/pages/Blogs/BlogComment.tsx | 156 ++++++++++++ src/pages/Blogs/BlogReactions.tsx | 105 ++++++++ src/pages/Blogs/singleBlog.tsx | 79 +----- src/redux/actions/commentActions.tsx | 320 ++++++++++++++++++++++++ src/redux/actions/reactionActions.tsx | 94 +++++++ src/redux/actiontypes/commentTypes.tsx | 24 ++ src/redux/actiontypes/reactionTypes.tsx | 18 ++ src/redux/index.ts | 21 ++ src/redux/reducers/commentReducers.ts | 163 ++++++++++++ src/redux/reducers/index.ts | 6 +- src/redux/reducers/reactionReducers.tsx | 94 +++++++ 11 files changed, 1004 insertions(+), 76 deletions(-) create mode 100644 src/pages/Blogs/BlogComment.tsx create mode 100644 src/pages/Blogs/BlogReactions.tsx create mode 100644 src/redux/actions/commentActions.tsx create mode 100644 src/redux/actions/reactionActions.tsx create mode 100644 src/redux/actiontypes/commentTypes.tsx create mode 100644 src/redux/actiontypes/reactionTypes.tsx create mode 100644 src/redux/reducers/commentReducers.ts create mode 100644 src/redux/reducers/reactionReducers.tsx diff --git a/src/pages/Blogs/BlogComment.tsx b/src/pages/Blogs/BlogComment.tsx new file mode 100644 index 000000000..fb020fa5f --- /dev/null +++ b/src/pages/Blogs/BlogComment.tsx @@ -0,0 +1,156 @@ +import React, { useState, useEffect } from "react"; +import { useDispatch, useSelector } from "react-redux"; +import { RootState } from "../../redux/reducers"; +import { + getCommentsByBlogId, + createCommentAction, + fetchRepliesByComment, + addReplyToComment, +} from "../../redux/actions/commentActions"; +const profile: string = require("../../assets/avatar.png").default; + +interface BlogCommentProps { + blogId: string; +} + +const BlogComment: React.FC = ({ blogId }) => { + const dispatch = useDispatch(); + + const { + comment_data: comments, + isCommentLoading, + errors, + repliesByCommentId, + } = useSelector((state: RootState) => state.comments); + + const [newComment, setNewComment] = useState(""); + const [activeCommentId, setActiveCommentId] = useState(null); + const [replyContent, setReplyContent] = useState(""); + + useEffect(() => { + if (blogId) { + dispatch(getCommentsByBlogId(blogId)); + } + }, [dispatch, blogId]); + + useEffect(() => { + if (activeCommentId) { + dispatch(fetchRepliesByComment(activeCommentId)); + } + }, [dispatch, activeCommentId]); + + const handleAddComment = () => { + if (newComment.trim()) { + dispatch(createCommentAction(blogId, newComment)); + setNewComment(""); + } + }; + + const handleAddReply = (commentId: string) => { + const userId = localStorage.getItem("userId"); + if (!userId) { + alert("You must be logged in to reply."); + return; + } + + if (replyContent.trim()) { + dispatch(addReplyToComment(replyContent, commentId)); + setReplyContent(""); + } + }; + + return ( +
+

+ {`${comments.length} Comments`} +

+ +
+