"use client"; import React, { useEffect } 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 { Input } from "@/components/ui/input"; import Link from "next/link"; import RequestStep2 from "./RequestStep2"; import { useMutation, useQuery } from "@tanstack/react-query"; import { editProfile, getProfile } from "@/utils/authenticateApi"; import { useRouter } from "next/navigation"; import { toast } from "sonner"; const FormSchema = z.object({ firstName: z.string().min(1, { message: "لطفا نام خود را وارد نمایید.", }), lastName: z.string().min(1, { message: "لطفا نام خانوادگی خود را وارد نمایید.", }), nationalId: z .string() .length(10, { message: "کد ملی معتبر نمی باشد." }) .transform((value) => { const parsedNumber = Number(value); if (Number.isNaN(parsedNumber)) { throw new Error("کد ملی معتبر نمی باشد."); } else if ( Number(parsedNumber) > 100000000 && Number(parsedNumber) < 999999999 ) { throw new Error("کد ملی معتبر نمی باشد."); } return parsedNumber; }), }); export default function RequestStep1() { const router = useRouter(); const { data: profileData, isLoading, refetch, } = useQuery({ queryKey: ["Profile"], queryFn: () => getProfile(), refetchOnWindowFocus: false, }); const editProfileMutation = useMutation({ mutationKey: ["editProfile"], mutationFn: editProfile, onSuccess: (res) => { router.push("/RequestStep2"); localStorage.setItem("authorized", "true"); }, onError: (err: any | string) => { toast(<>{err.response.data.detail}, { className: "!bg-red-500 !text-white", }); }, }); const form = useForm>({ resolver: zodResolver(FormSchema), }); if (profileData?.firstName) { } useEffect(() => { if (profileData) { form.setValue("firstName", profileData.firstName); form.setValue("lastName", profileData.lastName); form.setValue("nationalId", profileData.nationalId); } }, [profileData]); function onSubmit(data: z.infer) { // toast({ // title: "You submitted the following values:", // description: ( //
    //       {JSON.stringify(data, null, 2)}
    //     
// ), // }); const strNationalId = String(data.nationalId); const payload = { ...data, nationalId: strNationalId }; editProfileMutation.mutate(payload); } return (

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

{profileData && (
( نام )} /> ( نام خانوادگی )} /> ( کد ملی )} />
)}
); }