shahrbin-citizen/components/Requests/RequestComments.tsx

186 lines
5.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

"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;
}
// Define the post comment function type
type PostCommentArgs = {
id: string;
comment: string;
};
const RequestComments: React.FC<RequestCommentsProps> = ({
reportId,
commentsCount,
}) => {
// Component state
const [comments, setComments] = useState<Comment[]>([]);
const [commentsCounts, setCommentsCounts] = useState<number>(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 (
<>
<span className="flex items-center justify-center text-black text-lg font-bold">
<Drawer>
<DrawerTrigger onClick={(e) => e.stopPropagation()}>
<FaRegComment className="mx-1 cursor-pointer" />
</DrawerTrigger>
<DrawerContent className=" px-64">
<DrawerHeader className="">
{isLoading && (
<div className="flex justify-center items-center text-center w-full h-full">
<ThreeDots
visible={true}
height="50"
width="50"
color="#003778"
radius="9"
ariaLabel="three-dots-loading"
wrapperStyle={{}}
wrapperClass=""
/>
</div>
)}
{isError && <p>Failed to load comments.</p>}
{isSuccess && comments.length > 0 && commentsCounts > 0 ? (
<div>
{comments.map((comment) => (
<>
{console.log(comment)}
<RequestComment
key={comment.id}
id={comment.id}
comment={comment.text}
dateTime={comment.dateTime}
user={comment.user}
canDelete={comment.canDelete}
/>
</>
))}
</div>
) : (
!isLoading && <p>هنوز نظری برای این درخواست ثبت نشده است.</p>
)}
</DrawerHeader>
<form onSubmit={handleSubmit} className="flex justify-between">
<button type="submit" className="ml-2">
{mutation.isPending ? (
<Oval
visible={true}
height="20"
width="20"
color="#000"
ariaLabel="oval-loading"
wrapperStyle={{}}
wrapperClass=""
/>
) : (
<MdSend className="w-5 h-5 text-green"/>
)}
</button>
<Textarea
placeholder="نظر جدید..."
className="resize-y w-full min-h-5 bg-white h-10 mb-4"
onChange={(e) => setCommentText(e.target.value)}
id="comment"
value={commentText}
/>
{/* <input
className="w-full pr-10"
type="text"
id="comment"
value={comment}
placeholder="نظر جدید..."
onChange={(e) => setComment(e.target.value)}
/> */}
</form>
{/* <DrawerFooter>
<DrawerClose>
<Button variant="outline">Cancel</Button>
</DrawerClose>
</DrawerFooter> */}
</DrawerContent>
</Drawer>
</span>
<span className="mx-[2px]">{commentsCounts}</span>
</>
);
};
export default RequestComments;