From c025163c551d359c4596e397531cd13a8fea6eb9 Mon Sep 17 00:00:00 2001 From: Rykkel <220993G@mymail.nyp.edu.sg> Date: Mon, 12 Aug 2024 22:11:04 +0800 Subject: [PATCH] Admin delete comment function --- client/src/components/CommentsModule.tsx | 1 - client/src/pages/CommunityPostManagement.tsx | 127 ++++++++++++++++++- server/routes/post.js | 27 +++- 3 files changed, 151 insertions(+), 4 deletions(-) diff --git a/client/src/components/CommentsModule.tsx b/client/src/components/CommentsModule.tsx index 65d8db2..5634536 100644 --- a/client/src/components/CommentsModule.tsx +++ b/client/src/components/CommentsModule.tsx @@ -1,4 +1,3 @@ -import * as Yup from "yup"; import instance from "../security/http"; import config from "../config"; import { useParams } from "react-router-dom"; diff --git a/client/src/pages/CommunityPostManagement.tsx b/client/src/pages/CommunityPostManagement.tsx index df0c7b1..5494bb7 100644 --- a/client/src/pages/CommunityPostManagement.tsx +++ b/client/src/pages/CommunityPostManagement.tsx @@ -20,7 +20,7 @@ import { useEffect, useState } from "react"; import instance from "../security/http"; import config from "../config"; import { popErrorToast, popToast } from "../utilities"; -import { ClipboardDocumentIcon, TrashDeleteIcon } from "../icons"; +import { ClipboardDocumentIcon, TrashDeleteIcon, EyeIcon } from "../icons"; export default function CommunityPostManagement() { const [userInformationList, setUserInformationList] = useState([]); @@ -28,6 +28,11 @@ export default function CommunityPostManagement() { const [mergedList, setMergedList] = useState([]); const [isModalOpen, setIsModalOpen] = useState(false); const [postToDelete, setPostToDelete] = useState(null); + const [isCommentsModalOpen, setIsCommentsModalOpen] = useState(false); + const [comments, setComments] = useState([]); + const [selectedPostId, setSelectedPostId] = useState(null); + const [selectedPostTitle, setSelectedPostTitle] = useState(null); + const [commentsWithUserInfo, setCommentsWithUserInfo] = useState([]); const columns = [ { key: "profilePicture", label: "PROFILE PICTURE" }, @@ -126,6 +131,47 @@ export default function CommunityPostManagement() { } }; + const handleCommentsClick = async (postId: string) => { + try { + const postResponse = await instance.get(`${config.serverAddress}/post/${postId}`); + const post = postResponse.data; + + const commentsResponse = await instance.get( + `${config.serverAddress}/post/${postId}/getComments` + ); + + const commentsWithInfo = commentsResponse.data.map((comment: any) => { + const user = userInformationList.find((user) => user.id === comment.userId); + return { + ...comment, + firstName: user?.firstName, + lastName: user?.lastName, + }; + }); + + setCommentsWithUserInfo(commentsWithInfo); + setComments(commentsResponse.data); + setSelectedPostId(postId); + setSelectedPostTitle(post.title); + setIsCommentsModalOpen(true); + } catch (error) { + console.error("Error fetching comments:", error); + } + }; + + const handleCommentDelete = async (commentId: string, postId: string) => { + console.log("Deleting comment with ID: ", commentId) + try { + await instance.delete( + `${config.serverAddress}/post/comments/${commentId}` + ); + handleCommentsClick(postId) + popToast("Comment deleted successfully", 1); + } catch (error) { + console.error("Error deleting comment:", error); + } + }; + // Function to safely render item values const renderCellValue = (item: any, columnKey: any) => { const value = getKeyValue(item, columnKey); @@ -174,6 +220,16 @@ export default function CommunityPostManagement() { ) : columnKey === "actions" ? (
+ + +
+ + {/* Comments Modal */} + setIsCommentsModalOpen(false)} + isDismissable={false} + isKeyboardDismissDisabled={true} + className="max-w-6xl max-h-[90vh]" // modal width and height + > + + Comments for Post: {selectedPostTitle} + + {commentsWithUserInfo.length > 0 ? ( +
+ + + PROFILE PICTURE + FIRST NAME + LAST NAME + CONTENT + ACTIONS + + + {(comment) => ( + + + + + {comment.firstName} + {comment.lastName} + {comment.content} + + + + + )} + +
+
+ ) : ( +
No comments... It's so lonely 🥹
+ )} +
+ + + +
+
+ ); } diff --git a/server/routes/post.js b/server/routes/post.js index f17b872..c42c8e9 100644 --- a/server/routes/post.js +++ b/server/routes/post.js @@ -126,7 +126,7 @@ router.get("/", async (req, res) => { if (tag) { condition.include[0].where = { tag: tag }; } - + // You can add condition for other columns here // e.g. condition.columnName = value; @@ -325,6 +325,31 @@ router.get("/:id/getComments", async (req, res) => { res.json(comments); }); +router.delete("/comments/:id", async (req, res) => { + let id = req.params.id; + console.log("Deleting comment with ID: ", id) + // Check id not found + let comment = await Comment.findByPk(id); + if (!comment) { + res.sendStatus(404); + return; + } + let num = await Comment.destroy({ + // destroy() deletes data based on the where condition, and returns the number of rows affected + where: { id: id }, + }); + if (num == 1) { + // destroy() returns no. of rows affected, that's why if num == 1 + res.json({ + message: "Comment was deleted successfully.", + }); + } else { + res.status(400).json({ + message: `Cannot delete comment with id ${id}.`, + }); + } +}); + router.get("/tags/all", async (req, res) => { // Check id not found let tags = await Tag.findAll();