shahrbin-citizen/components/Requests/RequestComments.tsx

120 lines
3.4 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 } from "@/utils/CitizenReport";
import { useQuery } from "@tanstack/react-query";
import React, { 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";
// Define types for props
interface RequestCommentsProps {
reportId: string;
commentsCount: number;
}
const RequestComments: React.FC<RequestCommentsProps> = ({
reportId,
commentsCount,
}) => {
// Component state
const [commentsCounts, setCommentsCounts] = useState<number>(commentsCount);
const [comment, setComment] = useState("");
const { data, isLoading, isError, isSuccess, refetch } = useQuery({
queryKey: ["Comments"],
queryFn: () => getComments(reportId),
// refetchOnWindowFocus: false,
});
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
const requestBody = {
comment: comment,
};
try {
const response = await fetch(`/api/comments/${reportId}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestBody),
});
if (!response.ok) {
throw new Error("Network response was not ok");
}
// Handle success (e.g., clear the input, show a success message, etc.)
setComment("");
console.log("Comment submitted successfully!");
} catch (error) {
// Handle error
console.error("There was an error submitting the comment:", error);
}
};
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="bg-rose-400 px-64">
<DrawerHeader className="bg-yellow">
<p>
هنوز نظری برای این درخواست ثبت نشده است.
</p>
</DrawerHeader>
<form
onSubmit={handleSubmit}
className="flex justify-between bg-blue"
>
<button type="submit" className="ml-2">
<MdSend />
</button>
<Textarea
placeholder="نظر جدید..."
className="w-full min-h-5"
onChange={(e) => setComment(e.target.value)}
id="comment"
value={comment}
/>
{/* <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;