diff --git a/client/src/components/CommentSection.jsx b/client/src/components/CommentSection.jsx index 61f713b..911157b 100644 --- a/client/src/components/CommentSection.jsx +++ b/client/src/components/CommentSection.jsx @@ -1,101 +1,107 @@ -import { useState } from 'react' -import { useMutation, useQueryClient } from 'react-query' -import { toast } from 'react-hot-toast' -import { - FiMessageSquare, - FiUser, - FiClock, - FiEdit, +import { useState } from 'react'; +import { useMutation, useQueryClient } from 'react-query'; +import { toast } from 'react-hot-toast'; +import { + FiMessageSquare, + FiUser, + FiClock, + FiEdit, FiTrash2, - FiX -} from 'react-icons/fi' -import { useAuth } from '../contexts/AuthContext' -import api from '../utils/api' + FiX, +} from 'react-icons/fi'; +import { useAuth } from '../contexts/AuthContext'; +import api from '../utils/api'; const CommentSection = ({ contentType, contentId, comments = [] }) => { - const [showForm, setShowForm] = useState(false) - const [commentText, setCommentText] = useState('') - const [editingComment, setEditingComment] = useState(null) - const { user } = useAuth() - const queryClient = useQueryClient() + const [showForm, setShowForm] = useState(false); + const [commentText, setCommentText] = useState(''); + const [editingComment, setEditingComment] = useState(null); + const { user } = useAuth(); + const queryClient = useQueryClient(); const addCommentMutation = useMutation( - (text) => api.post('/comments', { - contentType, - contentId, - content: text - }), + text => + api.post('api/comments', { + contentType, + contentId, + content: text, + }), { onSuccess: () => { - queryClient.invalidateQueries(['question', contentId]) - setCommentText('') - setShowForm(false) - toast.success('Comment added successfully!') + queryClient.invalidateQueries(['question', contentId]); + setCommentText(''); + setShowForm(false); + toast.success('Comment added successfully!'); + }, + onError: error => { + toast.error(error.response?.data?.message || 'Failed to add comment'); }, - onError: (error) => { - toast.error(error.response?.data?.message || 'Failed to add comment') - } } - ) + ); const updateCommentMutation = useMutation( - ({ commentId, content }) => api.put(`/comments/${commentId}`, { content }), + ({ commentId, content }) => + api.put(`api/comments/${commentId}`, { content }), { onSuccess: () => { - queryClient.invalidateQueries(['question', contentId]) - setEditingComment(null) - toast.success('Comment updated successfully!') + queryClient.invalidateQueries(['question', contentId]); + setEditingComment(null); + toast.success('Comment updated successfully!'); + }, + onError: error => { + toast.error( + error.response?.data?.message || 'Failed to update comment' + ); }, - onError: (error) => { - toast.error(error.response?.data?.message || 'Failed to update comment') - } } - ) + ); const deleteCommentMutation = useMutation( - (commentId) => api.delete(`/comments/${commentId}`), + commentId => api.delete(`api/comments/${commentId}`), { onSuccess: () => { - queryClient.invalidateQueries(['question', contentId]) - toast.success('Comment deleted successfully!') + queryClient.invalidateQueries(['question', contentId]); + toast.success('Comment deleted successfully!'); + }, + onError: error => { + toast.error( + error.response?.data?.message || 'Failed to delete comment' + ); }, - onError: (error) => { - toast.error(error.response?.data?.message || 'Failed to delete comment') - } } - ) + ); const handleSubmit = () => { if (!commentText.trim()) { - toast.error('Please enter a comment') - return + toast.error('Please enter a comment'); + return; } - addCommentMutation.mutate(commentText) - } + addCommentMutation.mutate(commentText); + }; - const handleUpdate = (commentId) => { + const handleUpdate = commentId => { if (!commentText.trim()) { - toast.error('Please enter a comment') - return + toast.error('Please enter a comment'); + return; } - updateCommentMutation.mutate({ commentId, content: commentText }) - } + updateCommentMutation.mutate({ commentId, content: commentText }); + }; - const handleDelete = (commentId) => { + const handleDelete = commentId => { if (window.confirm('Are you sure you want to delete this comment?')) { - deleteCommentMutation.mutate(commentId) + deleteCommentMutation.mutate(commentId); } - } + }; - const startEditing = (comment) => { - setEditingComment(comment._id) - setCommentText(comment.content) - } + const startEditing = comment => { + setEditingComment(comment._id); + setCommentText(comment.content); + }; const cancelEditing = () => { - setEditingComment(null) - setCommentText('') - } + setEditingComment(null); + setCommentText(''); + }; return (
@@ -125,7 +131,7 @@ const CommentSection = ({ contentType, contentId, comments = [] }) => {