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`} +

+ +
+