Added part of Profile Componnent
parent
a22a69e6d9
commit
4d399f2124
|
|
@ -1,7 +1,6 @@
|
|||
import React from 'react'
|
||||
import Profile from "@/components/Settings/Profile";
|
||||
import React from "react";
|
||||
|
||||
export default function page() {
|
||||
return (
|
||||
<div>page</div>
|
||||
)
|
||||
return <Profile />;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,337 @@
|
|||
"use client";
|
||||
import React, { useEffect, 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 { Input } from "@/components/ui/input";
|
||||
import Link from "next/link";
|
||||
import { useMutation, useQuery } from "@tanstack/react-query";
|
||||
import { getAuthenticate, putAuthenticate } from "@/utils/authenticateApi";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { toast } from "sonner";
|
||||
import { Oval } from "react-loader-spinner";
|
||||
import {
|
||||
Select,
|
||||
SelectContent,
|
||||
SelectGroup,
|
||||
SelectItem,
|
||||
SelectTrigger,
|
||||
SelectValue,
|
||||
} from "../ui/select";
|
||||
import { educations, genders } from "@/constants";
|
||||
|
||||
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;
|
||||
}),
|
||||
phoneNumber: z
|
||||
.string()
|
||||
.length(11, { message: "شماره موبایل معتبر نمی باشد." })
|
||||
.transform((value) => {
|
||||
const parsedNumber = Number(value);
|
||||
if (Number.isNaN(parsedNumber)) {
|
||||
throw new Error("شماره موبایل معتبر نمی باشد.");
|
||||
} else if (
|
||||
Number(parsedNumber) > 1000000000 &&
|
||||
Number(parsedNumber) < 9999999999
|
||||
) {
|
||||
throw new Error("شماره موبایل معتبر نمی باشد.");
|
||||
}
|
||||
return parsedNumber;
|
||||
}),
|
||||
gender: z.string({
|
||||
required_error: "لطفا جنسیت خود را انتخاب نمایید.",
|
||||
}),
|
||||
education: z.string({
|
||||
required_error: "لطفا تحصیلات خود را انتخاب نمایید.",
|
||||
}),
|
||||
});
|
||||
|
||||
export default function Profile() {
|
||||
const router = useRouter();
|
||||
// const [profileData, setProfileData] = useState<any | null>(null);
|
||||
|
||||
const {
|
||||
data: profileData,
|
||||
isLoading,
|
||||
refetch,
|
||||
} = useQuery({
|
||||
queryKey: ["Profile"],
|
||||
queryFn: () => getAuthenticate(),
|
||||
refetchOnWindowFocus: false,
|
||||
});
|
||||
|
||||
const editProfileMutation = useMutation({
|
||||
mutationKey: ["editProfile"],
|
||||
mutationFn: putAuthenticate,
|
||||
onSuccess: (res) => {
|
||||
refetch();
|
||||
// toast("پروفایل شما با موفقیت ویرایش شد.", {
|
||||
toast(res.message, {
|
||||
className: "!bg-green-500 !text-white",
|
||||
});
|
||||
router.push("/");
|
||||
},
|
||||
onError: (err: any | string) => {
|
||||
toast(<>{err.response.data.detail}</>, {
|
||||
className: "!bg-red !text-white",
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
const form = useForm<z.infer<typeof FormSchema>>({
|
||||
resolver: zodResolver(FormSchema),
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (profileData) {
|
||||
// setProfileData(data?.data);
|
||||
|
||||
form.setValue("firstName", profileData.data?.firstName);
|
||||
form.setValue("lastName", profileData.data?.lastName);
|
||||
form.setValue("nationalId", profileData.data?.nationalId);
|
||||
form.setValue("phoneNumber", profileData.data?.phoneNumber);
|
||||
form.setValue("gender", profileData.data?.gender);
|
||||
form.setValue("education", profileData.data?.education);
|
||||
}
|
||||
}, [profileData]);
|
||||
|
||||
function onSubmit(data: z.infer<typeof FormSchema>) {
|
||||
const strNationalId = String(data.nationalId);
|
||||
const strPhoneNumber = String(data.phoneNumber);
|
||||
const payload = {
|
||||
...data,
|
||||
nationalId: strNationalId,
|
||||
phoneNumber: strPhoneNumber,
|
||||
};
|
||||
// editProfileMutation.mutate(payload);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="w-full h-full pt-12 sm:w-2/3 md:w-2/4 max-sm:p-8 mx-auto bg-white p-20 rounded-md">
|
||||
<div className="flex flex-col justify-center h-full space-y-4 md:space-y-4">
|
||||
<h2 className=" w-full font-bold">پروفایل کاربری</h2>
|
||||
{profileData && (
|
||||
<Form {...form}>
|
||||
<form
|
||||
onSubmit={form.handleSubmit(onSubmit)}
|
||||
className="flex flex-col w-full mt-6 "
|
||||
>
|
||||
<div className="flex flex-row flex-wrap justify-between border p-4 rounded-md">
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="firstName"
|
||||
render={({ field }) => (
|
||||
<FormItem className="w-full sm:w-[48%]">
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||||
نام
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder="نام خود را وارد نمایید"
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="lastName"
|
||||
render={({ field }) => (
|
||||
<FormItem className="w-full sm:w-[48%] ">
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium">
|
||||
نام خانوادگی
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
placeholder="نام خانوادگی خود را وارد نمایید"
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="gender"
|
||||
render={({ field }) => (
|
||||
<FormItem className=" w-full sm:w-[48%]">
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
|
||||
جنسیت
|
||||
</FormLabel>
|
||||
<Select
|
||||
onValueChange={field.onChange}
|
||||
defaultValue={field.value}
|
||||
dir="rtl"
|
||||
>
|
||||
<FormControl>
|
||||
<SelectTrigger
|
||||
className=" !text-[#7C838A]"
|
||||
dir="rtl"
|
||||
>
|
||||
<SelectValue
|
||||
className=""
|
||||
placeholder="جنسیت خود را انتخاب نمایید."
|
||||
/>
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent dir="rtl">
|
||||
<SelectGroup>
|
||||
{genders?.map((item: any) => {
|
||||
return (
|
||||
<SelectItem key={item.id} value={item.id}>
|
||||
{item.title}
|
||||
</SelectItem>
|
||||
);
|
||||
})}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormMessage className="mt-2 " />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="education"
|
||||
render={({ field }) => (
|
||||
<FormItem className=" w-full sm:w-[48%]">
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
|
||||
تحصیلات
|
||||
</FormLabel>
|
||||
<Select
|
||||
onValueChange={field.onChange}
|
||||
defaultValue={field.value}
|
||||
dir="rtl"
|
||||
>
|
||||
<FormControl>
|
||||
<SelectTrigger
|
||||
className=" !text-[#7C838A]"
|
||||
dir="rtl"
|
||||
>
|
||||
<SelectValue
|
||||
className=""
|
||||
placeholder="تحصیلات خود را انتخاب نمایید."
|
||||
/>
|
||||
</SelectTrigger>
|
||||
</FormControl>
|
||||
<SelectContent dir="rtl">
|
||||
<SelectGroup>
|
||||
{educations?.map((item: any) => {
|
||||
return (
|
||||
<SelectItem key={item.id} value={item.id}>
|
||||
{item.title}
|
||||
</SelectItem>
|
||||
);
|
||||
})}
|
||||
</SelectGroup>
|
||||
</SelectContent>
|
||||
</Select>
|
||||
<FormMessage className="mt-2 " />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="nationalId"
|
||||
render={({ field }) => (
|
||||
<FormItem className="w-full sm:w-[48%]">
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
|
||||
کد ملی
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="کد ملی خود را وارد نمایید "
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="phoneNumber"
|
||||
render={({ field }) => (
|
||||
<FormItem className="w-full sm:w-[48%]">
|
||||
<FormLabel className="text-[#7C838A] text-sm font-medium mt-7">
|
||||
شماره موبایل
|
||||
</FormLabel>
|
||||
<FormControl>
|
||||
<Input
|
||||
type="text"
|
||||
placeholder="شماره موبایل خود را وارد نمایید "
|
||||
{...field}
|
||||
/>
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<div className="flex justify-end w-full gap-2 mt-8">
|
||||
<Button
|
||||
className="w-full text-black bg-yellow rounded-md hover:bg-yellow/80 mt-5"
|
||||
disabled={editProfileMutation.isPending}
|
||||
type="submit"
|
||||
>
|
||||
{editProfileMutation.isPending ? (
|
||||
<div className="flex items-center justify-center gap-2">
|
||||
<Oval
|
||||
visible={true}
|
||||
height="20"
|
||||
width="20"
|
||||
color="#fff"
|
||||
ariaLabel="oval-loading"
|
||||
wrapperStyle={{}}
|
||||
wrapperClass=""
|
||||
/>
|
||||
</div>
|
||||
) : (
|
||||
"ذخیره"
|
||||
)}
|
||||
{/* ورود */}
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</Form>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
@ -29,3 +29,54 @@ export const menuItems = [
|
|||
{ id: 5, title: "خروج از حساب کاربری", href: "#" , icon: <LuLogOut /> },
|
||||
];
|
||||
|
||||
export const genders = [
|
||||
{
|
||||
id: "male",
|
||||
title: "مرد",
|
||||
value: 2,
|
||||
},
|
||||
{
|
||||
id: "female",
|
||||
title: "زن",
|
||||
value: 1,
|
||||
},
|
||||
];
|
||||
|
||||
export const educations = [
|
||||
{
|
||||
id: 0,
|
||||
title: "تعریف نشده",
|
||||
value: 0,
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
title: "ابتدایی",
|
||||
value: 1,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "دیپلم",
|
||||
value: 2,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: "فوق دیپلم",
|
||||
value: 3,
|
||||
},
|
||||
{
|
||||
id: 4,
|
||||
title: "لیسانس",
|
||||
value: 4,
|
||||
},
|
||||
{
|
||||
id: 5,
|
||||
title: "فوق لیسانس",
|
||||
value: 5,
|
||||
},
|
||||
{
|
||||
id: 6,
|
||||
title: "دکترا",
|
||||
value: 6,
|
||||
},
|
||||
];
|
||||
|
||||
|
|
@ -70,7 +70,6 @@ export async function revoke(payload: {
|
|||
return data.data;
|
||||
}
|
||||
|
||||
|
||||
export async function RefreshToken(payload: { token: any; refreshToken: any }) {
|
||||
try {
|
||||
const response = await axios.post("api/Authenticate/Refresh", payload);
|
||||
|
|
@ -85,23 +84,41 @@ export async function RefreshToken(payload: { token: any; refreshToken: any }) {
|
|||
}
|
||||
|
||||
export async function logisterCitizen(payload: {
|
||||
phoneNumber: string;
|
||||
captcha: {
|
||||
key: string;
|
||||
value: string;
|
||||
};
|
||||
}) {
|
||||
const data = await axios.post(`api/Authenticate/LogisterCitizen`,payload);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
|
||||
phoneNumber: string;
|
||||
captcha: {
|
||||
key: string;
|
||||
value: string;
|
||||
};
|
||||
}) {
|
||||
const data = await axios.post(`api/Authenticate/LogisterCitizen`, payload);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
export async function verifyCitizen(payload: {
|
||||
otpToken: string | null;
|
||||
verificationCode: string;
|
||||
}) {
|
||||
const data = await axios.post(`api/Authenticate/VerifyCitizen`, payload);
|
||||
return data.data;
|
||||
}
|
||||
otpToken: string | null;
|
||||
verificationCode: string;
|
||||
}) {
|
||||
const data = await axios.post(`api/Authenticate/VerifyCitizen`, payload);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
// Get Authenticate
|
||||
export async function getAuthenticate() {
|
||||
const data = await axios.get(`/api/Authenticate`);
|
||||
return data.data;
|
||||
}
|
||||
|
||||
|
||||
// Put Authenticate
|
||||
export async function putAuthenticate(payload: {
|
||||
firstName: string;
|
||||
lastName: string;
|
||||
birthDate?:string;
|
||||
educationId?:number;
|
||||
gender?:number;
|
||||
phoneNumber?:string;
|
||||
nationalId?: string;
|
||||
}) {
|
||||
const data = await axios.put(`/api/Authenticate` ,payload);
|
||||
return data.data;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue