"use client"; import { getComments, postComment } from "@/utils/CitizenReport"; import { useMutation, useQuery } from "@tanstack/react-query"; import React, { useEffect, useState } from "react"; import { FaRegComment } from "react-icons/fa"; import { Drawer, DrawerClose, DrawerContent, DrawerDescription, DrawerFooter, DrawerHeader, DrawerTitle, DrawerTrigger, } from "@/components/ui/drawer"; import { Button } from "../ui/button"; import { MdSend } from "react-icons/md"; import { Textarea } from "../ui/textarea"; import { toast } from "sonner"; import { Oval, ThreeDots } from "react-loader-spinner"; import RequestComment from "./RequestComment"; // Define types for props interface RequestCommentsProps { reportId: string; commentsCount: number; } // Define types for comments interface Comment { id: string; text: string; dateTime: string; user:any; canDelete:boolean; onDeleteCommentSuccess:any } // Define the post comment function type type PostCommentArgs = { id: string; comment: string; }; const RequestComments: React.FC = ({ reportId, commentsCount, }) => { // Component state const [comments, setComments] = useState([]); const [commentsCounts, setCommentsCounts] = useState(commentsCount); const [commentText, setCommentText] = useState(""); const { data, isLoading, isError, isSuccess, refetch } = useQuery({ queryKey: ["Comments", reportId], queryFn: () => getComments(reportId), // refetchOnWindowFocus: false, }); useEffect(() => { console.log("Fetched data:", data); // Log the fetched data if (data) { setComments(data.data); console.log("Comments state updated:", data); } }, [data]); // Post Commnet const mutation = useMutation({ mutationFn: ({ id, comment }: PostCommentArgs) => postComment(id, comment), onSuccess: () => { // Refetch comments after a new one is added refetch(); // Reset the comment input field setCommentText(""); // Increment the comments count setCommentsCounts((prevCount) => prevCount + 1); toast("successfully", { className: "!bg-green !text-black", }); }, onError: (error) => { console.error("Failed to post comment:", error); toast(<>{error.message}, { className: "!bg-red-500 !text-white", }); }, }); const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); mutation.mutate({ id: reportId, comment: commentText }); }; return ( <> e.stopPropagation()}> {isLoading && (
)} {isError &&

Failed to load comments.

} {isSuccess && comments.length > 0 && commentsCounts > 0 ? (
{comments.map((comment) => ( <> {console.log(comment)} ))}
) : ( !isLoading &&

هنوز نظری برای این درخواست ثبت نشده است.

)}