import { motion } from 'framer-motion'; import { X } from 'lucide-react'; import { useMutation, useQueryClient } from '@tanstack/react-query'; import { useForm } from 'react-hook-form'; import { zodResolver } from '@hookform/resolvers/zod'; import { z } from 'zod'; import { toast } from 'sonner'; import { api } from '../api'; import { useAuth } from '../context/AuthContext'; const commentSchema = z.object({ content: z.string().min(1, 'Comment cannot be empty').max(500), }); type CommentFormData = z.infer; interface CommentSheetProps { postId: string; onClose: () => void; } export default function CommentSheet({ postId, onClose }: CommentSheetProps) { const { user } = useAuth(); const queryClient = useQueryClient(); const { register, handleSubmit, reset, formState: { errors } } = useForm({ resolver: zodResolver(commentSchema), }); const { mutate: addComment, isPending } = useMutation({ mutationFn: (data: CommentFormData) => api.post(`/api/posts/${postId}/comments`, data), onSuccess: () => { toast.success('Comment added!'); queryClient.invalidateQueries({ queryKey: ['posts'] }); reset(); }, onError: (error) => { toast.error('Failed to add comment.'); console.error(error); } }); const onSubmit = (data: CommentFormData) => { addComment(data); }; return (
e.stopPropagation()} >

Comments

{/* NOTE: API to GET comments is not available in the spec. */}

Be the first to comment!

(Viewing comments is not yet supported)

Your avatar
{errors.content &&

{errors.content.message}

}
); }