diff --git a/client/src/components/CommentsModule.tsx b/client/src/components/CommentsModule.tsx index 2530f90..37a64e6 100644 --- a/client/src/components/CommentsModule.tsx +++ b/client/src/components/CommentsModule.tsx @@ -7,6 +7,7 @@ import { ChatBubbleOvalLeftEllipsisIcon, EllipsisHorizontalIcon, HandThumbsUpIco import * as Yup from "yup"; import NextUIFormikTextarea from "./NextUIFormikTextarea"; import { Form, Formik, FormikHelpers } from "formik"; +import { retrieveUserInformation } from "../security/users"; interface Comment { id: string; @@ -35,6 +36,7 @@ export default function CommentsModule() { const [editingCommentId, setEditingCommentId] = useState(null); const { isOpen, onOpen, onOpenChange } = useDisclosure(); const [commentToDelete, setCommentToDelete] = useState(null); + const [currentUserId, setCurrentUserId] = useState(null); // State for current user ID let postId = id @@ -43,6 +45,19 @@ export default function CommentsModule() { return `${config.serverAddress}/users/profile-image/${userId}`; }; + // Fetch current user information to get user ID + useEffect(() => { + const getCurrentUserInformation = async () => { + try { + const user = await retrieveUserInformation(); // Get the user information + setCurrentUserId(user.id); // Store user ID + } catch (error) { + console.error(error); + } + }; + getCurrentUserInformation(); + }, []); + const getComments = async () => { instance .get(config.serverAddress + `/post/${postId}/getComments`).then((res) => { @@ -114,13 +129,14 @@ export default function CommentsModule() { {commentList.length > 0 ? ( commentList.map((comment) => { const user = comment.user; // Get the user object from the comment - if (!user) { console.error("User object is undefined for comment:", comment); return null; // Skip rendering this comment if user is undefined } const profilePictureUrl = getProfilePicture(user.id); // Get the user's profile picture + const canEditOrDelete = currentUserId === user.id; + return (
@@ -162,37 +178,39 @@ export default function CommentsModule() { )}
-
- -
- - - -
- - handleEditClick(comment)} - > - Edit - - handleDeleteClick(comment.id)} - > - Delete - - -
-
+ {canEditOrDelete && ( +
+ +
+ + + +
+ + handleEditClick(comment)} + > + Edit + + handleDeleteClick(comment.id)} + > + Delete + + +
+
+ )}