"use client"; import React, { useState } from "react"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import * as z from "zod"; import { Button } from "@/components/ui/button"; import { Form, FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@/components/ui/form"; import { Select, SelectContent, SelectGroup, SelectItem, SelectLabel, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Input } from "@/components/ui/input"; import { toast } from "@/components/ui/use-toast"; import { Textarea } from "@/components/ui/textarea"; import { Label } from "@/components/ui/label"; import Link from "next/link"; import RequestResult from "./RequestResult"; import Image from "next/image"; import Img1 from "../public/img/logo.png"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { postComplaintSubmit } from "@/utils/citizenApi"; import { useRouter } from "next/navigation"; import { getCaptcha, getCategory } from "@/utils/commonApi"; const MAX_IMAGE_SIZE = 5242880; // 5 MB const ALLOWED_IMAGE_TYPES = [ "image/jpeg", "image/png", "image/webp", "image/jpg", ]; const FormSchema = z.object({ subject: z.string().min(2, { message: "subject must be at least 2 characters.", }), organisation: z.string().min(2, { message: "organisation must be at least 2 characters.", }), person: z.string().min(2, { message: "personName must be at least 2 characters.", }), description: z .string() .min(5, { message: "description must be at least 5 characters.", }) .max(200, { message: "description must not be longer than 200 characters.", }), uploadFile: z .custom((val) => val instanceof FileList, "Required") .refine((files) => files.length > 0, `Required`) .refine((files) => files.length <= 5, `Maximum of 5 File are allowed.`) .refine( (files) => Array.from(files).every((file) => file.size <= MAX_IMAGE_SIZE), `Each file size should be less than 5 MB.` ) .refine( (files) => Array.from(files).every((file) => ALLOWED_IMAGE_TYPES.includes(file.type) ), "Only these types are allowed .jpg, .jpeg, .png and .webp" ), categoryId: z.string({ required_error: "Please select an category to display.", }), captchaValue: z.string().min(4, { message: "captcha must be at least 4 characters.", }), }); // Form Hook export default function RequestStep2() { // const [trackingComplaint, setTrackingComplaint] = useState({}); const router = useRouter(); const complaintSubmitMutation = useMutation({ mutationKey: ["complaintSubmit"], mutationFn: postComplaintSubmit, onSuccess: (res) => { console.log(res); localStorage.setItem("trackingNumber", res.trackingNumber); localStorage.setItem("password", res.password); alert("Success"); router.push("/RequestResult"); // router.push("/RequestResult", { data: trackingComplaint }); // return (res) }, onError: (error) => { console.log(error); console.log(error?.message); }, }); // All Category const { data: categoryData } = useQuery({ queryKey: ["Category"], queryFn: () => getCategory(), }); // Captcha const { data: CaptchaData, isLoading } = useQuery({ queryKey: ["Captcha"], queryFn: () => getCaptcha(), }); // console.log(captchaData); const srcForImage = CaptchaData ? URL.createObjectURL(CaptchaData.data) : null; const form = useForm>({ resolver: zodResolver(FormSchema), defaultValues: { subject: "", organisation: "", person: "", description: "", captchaValue: "", // uploadFile: null, }, }); function onSubmit(data: z.infer) { // toast({ // title: "You submitted the following values:", // description: ( //
    //       {JSON.stringify(data, null, 2)}
    //     
// ), // }); const formData = new FormData(); formData.append("Title", data.subject); formData.append("Text", data.description); formData.append("CategoryId", data.categoryId); formData.append("Medias", data.uploadFile[0]); formData.append("Captcha.Value", data.captchaValue); formData.append("Captcha.Key", CaptchaData?.headers["captcha-key"]); complaintSubmitMutation.mutate({ Data: formData, }); resetField("Title"); resetField("Text"); resetField("Medias"); resetField("CategoryId"); resetField("Captcha.Value"); console.log("submited"); console.log(data); } const { resetField } = useForm(); return (

لطفا جهت ارسال درخواست فرم زیر را تکمیل نمایید.

( *موضوع شکایت )} />
( *دستگاه مربوطه )} /> ( فرد مربوطه )} />
( توضیحات