// Feedback.tsx "use client"; import React, { useEffect, useState, MouseEvent } from "react"; import { useForm } from "react-hook-form"; import { number, z } from "zod"; import { zodResolver } from "@hookform/resolvers/zod"; import { useMutation, useQuery } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; import Rating from "@/components/Rating/Rating"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Form, FormControl, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Label } from "@/components/ui/label"; import { getSingleCitizenReportMine, postFeedback, postObjection, } from "@/utils/CitizenReport"; import { convertFullTime } from "@/lib/utils"; import { ThreeDots } from "react-loader-spinner"; import { Value } from "@radix-ui/react-select"; // Define the post Feedback function type type PostFeedbackArgs = { id: string; rating: number; }; // Define the post Objection function type type PostObjectionArgs = { id: string; payload: { comments: string; attachments: string[]; }; }; const FormSchema = z.object({ comments: z.string().min(1, { message: "لطفا توضیحات خود را وارد نمایید.", }), fileInput: z.string().optional(), type: z.enum(["1", "0"], { required_error: "لطفا یکی از دو گزینه زیر را انتخاب کنید.", }), }); interface FeedbackProps { id: string; } interface ReportData { trackingNumber: string; categoryTitle: string; sent: string; } const Feedback: React.FC = ({ id }) => { const router = useRouter(); const [satisfaction, setSatisfaction] = useState(2); const [reportData, setReportData] = useState(null); const [rating, setRating] = useState(0); // State to hold rating value const { data, isLoading, isSuccess } = useQuery({ queryKey: ["ReportMineId", id], queryFn: () => getSingleCitizenReportMine(id), }); useEffect(() => { if (data) { setReportData(data.data); } }, [data, isSuccess]); const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { comments: "", fileInput: "", type: "0", }, }); const postFeedbackMutation = useMutation({ mutationKey: ["postFeedback"], mutationFn: ({ id, rating }: PostFeedbackArgs) => postFeedback(id, rating), onSuccess: () => { // setValues((prev) => ({ ...prev, rating: 0 })); // N form.reset(); router.push("/User/MyRequest"); toast.success("بازخورد شما با موفقیت ثبت شد"); }, onError: (error: any) => { toast.error(`خطا در ارسال بازخورد: ${error.message}`); }, }); const postObjectionMutation = useMutation({ mutationKey: ["postObjection"], mutationFn: ({ id, payload }: PostObjectionArgs) => postObjection(id, payload), onSuccess: () => { // N form.reset(); toast.success("اعتراض شما با موفقیت ثبت شد"); router.push("/User/MyRequest"); }, onError: (error: any) => { toast.error(`خطا در ارسال اعتراض: ${error.message}`); }, }); // N const onRatingSubmit = () => { postFeedbackMutation.mutate({ id, rating }); }; const onObjectionSubmit = (data: z.infer) => { const formData = new FormData(); formData.append("comments", data.comments); if (data.fileInput) { formData.append("attachments", data.fileInput); } postObjectionMutation.mutate({ id, payload: { comments: data.comments, attachments: data.fileInput ? [data.fileInput as string] : [], }, }); }; return (
{isLoading ? (
{/* Loading Spinner */}
در حال بارگذاری...
) : ( <>

شهروند گرامی، درخواست شما با کد رهگیری{" "} {reportData?.trackingNumber} {" "} و مربوط به گروه موضوعی{" "} {reportData?.categoryTitle} {" "} ثبت شده در تاریخ{" "} {convertFullTime(reportData?.sent)} {" "} پیگیری و رسیدگی شد.

لطفا میزان رضایتمندی خود را از فرآیند رسیدگی اعلام نمایید.

نظرات شما باعث ارتقای سطح خدمت‌رسانی ما خواهد شد.

آیا از نحوه ی پاسخگویی راضی بودید؟

setSatisfaction(Number(value))} className="flex flex-row justify-end mt-4 w-full" >
{satisfaction === 1 && (
form.setValue("fileInput", value)} />
)} {satisfaction === 0 && ( <>

باتوجه به عدم رضایت شما از نحوه رسیدگی، درخواست برای بررسی مجدد به واحد بازرسی ارجاع داده می‌شود.

( توضیحات )} /> ( پیوست‌ها )} />
)}
)}
); }; export default Feedback;