import React from "react"; import { useAuth } from "../../context/AuthContext"; import { commentApi } from "../../api/apiService"; const CommentItem = ({ comment, onCommentDelete, onCommentUpdate, postId }) => { const { user } = useAuth(); const [isEditing, setIsEditing] = React.useState(false); const [editContent, setEditContent] = React.useState(comment.content); const isAuthor = user && user.username === comment.username; const handleEditClick = () => { setIsEditing(true); setEditContent(comment.content); }; const handleCancelEdit = () => { setIsEditing(false); }; const handleSaveEdit = async () => { if (editContent.trim() === "") return; try { await commentApi.updateComment(postId, comment.id, editContent, user.username); onCommentUpdate({ ...comment, content: editContent }); setIsEditing(false); } catch (error) { console.error("Error occurred while editing comment:", error); } }; const handleDeleteClick = async () => { if (window.confirm("Are you sure you want to delete this comment?")) { try { await commentApi.deleteComment(postId, comment.id); onCommentDelete(comment.id); } catch (error) { console.error("Error occurred while deleting comment:", error); } } }; return (
{comment.content}
{isAuthor && (