283 lines
9.3 KiB
TypeScript
283 lines
9.3 KiB
TypeScript
// 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<FeedbackProps> = ({ id }) => {
|
||
const router = useRouter();
|
||
const [satisfaction, setSatisfaction] = useState<number>(2);
|
||
const [reportData, setReportData] = useState<any | null>(null);
|
||
const [rating, setRating] = useState<number>(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<z.infer<typeof FormSchema>>({
|
||
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<typeof FormSchema>) => {
|
||
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 (
|
||
<div className="w-full max-w-[640px] mx-auto flex flex-col items-center">
|
||
{isLoading ? (
|
||
<div className="flex flex-col justify-center items-center text-center w-full h-full">
|
||
{/* Loading Spinner */}
|
||
<ThreeDots
|
||
visible={true}
|
||
height="50"
|
||
width="50"
|
||
color="#003778"
|
||
radius="9"
|
||
ariaLabel="three-dots-loading"
|
||
wrapperStyle={{}}
|
||
wrapperClass=""
|
||
/>
|
||
<div>در حال بارگذاری...</div>
|
||
</div>
|
||
) : (
|
||
<>
|
||
<p className="text-sm text-justify leading-7">
|
||
شهروند گرامی، درخواست شما با کد رهگیری{" "}
|
||
<span className="text-green font-bold">
|
||
{reportData?.trackingNumber}
|
||
</span>{" "}
|
||
و مربوط به گروه موضوعی{" "}
|
||
<span className="text-sm text-blue font-bold">
|
||
{reportData?.categoryTitle}
|
||
</span>{" "}
|
||
ثبت شده در تاریخ{" "}
|
||
<span className="text-sm text-red font-bold">
|
||
{convertFullTime(reportData?.sent)}
|
||
</span>{" "}
|
||
پیگیری و رسیدگی شد.
|
||
</p>
|
||
<p className="text-right w-full text-sm leading-7">
|
||
لطفا میزان رضایتمندی خود را از فرآیند رسیدگی اعلام نمایید.
|
||
</p>
|
||
<p className="text-right w-full text-sm leading-7">
|
||
نظرات شما باعث ارتقای سطح خدمترسانی ما خواهد شد.
|
||
</p>
|
||
|
||
<p className="text-right w-full text-sm leading-7 font-bold">
|
||
آیا از نحوه ی پاسخگویی راضی بودید؟
|
||
</p>
|
||
|
||
<RadioGroup
|
||
onValueChange={(value) => setSatisfaction(Number(value))}
|
||
className="flex flex-row justify-end mt-4 w-full"
|
||
>
|
||
<div className="flex items-center space-x-2">
|
||
<RadioGroupItem value="1" id="r1" />
|
||
<Label htmlFor="r1">بله</Label>
|
||
</div>
|
||
<div className="flex items-center space-x-2">
|
||
<RadioGroupItem value="0" id="r2" />
|
||
<Label htmlFor="r2">خیر</Label>
|
||
</div>
|
||
</RadioGroup>
|
||
|
||
<div className="flex w-full justify-center flex-col">
|
||
{satisfaction === 1 && (
|
||
<div className="mb-3">
|
||
<Rating
|
||
name="rating"
|
||
onChange={setRating}
|
||
// onChange={(value: any) => form.setValue("fileInput", value)}
|
||
/>
|
||
|
||
<Button
|
||
className="w-full text-black bg-yellow rounded-md hover:bg-yellow/80 mt-5"
|
||
onClick={onRatingSubmit} // Attach the onRatingSubmit function to this button
|
||
>
|
||
ثبت امتیاز
|
||
</Button>
|
||
</div>
|
||
)}
|
||
|
||
{satisfaction === 0 && (
|
||
<>
|
||
<p className="text-sm mt-4">
|
||
باتوجه به عدم رضایت شما از نحوه رسیدگی، درخواست برای بررسی
|
||
مجدد به واحد بازرسی ارجاع داده میشود.
|
||
</p>
|
||
<Form {...form}>
|
||
<form
|
||
// onSubmit={form.handleSubmit(handleSubmit)}
|
||
onSubmit={form.handleSubmit(onObjectionSubmit)}
|
||
className="flex flex-col w-full mt-6"
|
||
>
|
||
<div className="flex flex-row flex-wrap justify-between">
|
||
<FormField
|
||
control={form.control}
|
||
name="comments"
|
||
render={({ field }) => (
|
||
<FormItem className="w-full">
|
||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||
توضیحات
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input {...field} />
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
<FormField
|
||
control={form.control}
|
||
name="fileInput"
|
||
render={({ field }) => (
|
||
<FormItem className="w-full">
|
||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||
پیوستها
|
||
</FormLabel>
|
||
<FormControl>
|
||
<Input type="file" {...field} />
|
||
</FormControl>
|
||
<FormMessage />
|
||
</FormItem>
|
||
)}
|
||
/>
|
||
</div>
|
||
<Button
|
||
type="submit"
|
||
className="w-full text-black bg-yellow rounded-md hover:bg-yellow/80 mt-5"
|
||
// onClick={handleSubmit}
|
||
>
|
||
ثبت
|
||
</Button>
|
||
</form>
|
||
</Form>
|
||
</>
|
||
)}
|
||
</div>
|
||
</>
|
||
)}
|
||
</div>
|
||
);
|
||
};
|
||
|
||
export default Feedback;
|