diff --git a/src/pages/profile/Profile.tsx b/src/pages/profile/Profile.tsx new file mode 100644 index 0000000..e033285 --- /dev/null +++ b/src/pages/profile/Profile.tsx @@ -0,0 +1,168 @@ +import { useState, type FC } from 'react' +import Input from '@/components/Input' +import Textarea from '@/components/Textarea' +import UploadBox from '@/components/UploadBox' +import PresignedImage from '@/components/PresignedImage' +import { useFormik } from 'formik' +import * as Yup from 'yup' +import Button from '@/components/Button' +import Error from '@/components/Error' +import { useGetMe, useUpdateProfile } from '@/pages/user/hooks/useUserData' +import { useSingleUpload } from '@/pages/uploader/hooks/useUploader' +import { toast } from '@/shared/toast' +import { extractErrorMessage } from '@/config/func' +import type { UpdateProfileType } from '@/pages/user/types/Types' +import { Edit } from 'iconsax-react' + +const Profile: FC = () => { + const { data: user, isLoading } = useGetMe() + const updateProfile = useUpdateProfile() + const { mutate: upload, isPending: isUploading } = useSingleUpload() + const [file, setFile] = useState() + + const handleSave = (values: UpdateProfileType) => { + updateProfile.mutate( + { + firstName: values.firstName.trim(), + lastName: values.lastName.trim(), + address: values.address?.trim() || undefined, + ...(values.avatarUrl ? { avatarUrl: values.avatarUrl } : {}), + }, + { + onSuccess() { + toast('پروفایل با موفقیت ویرایش شد', 'success') + }, + onError(error) { + toast(extractErrorMessage(error), 'error') + }, + }, + ) + } + + const formik = useFormik({ + enableReinitialize: true, + initialValues: { + firstName: user?.firstName ?? '', + lastName: user?.lastName ?? '', + address: user?.addresse ?? '', + avatarUrl: user?.avatarUrl ?? undefined, + }, + validationSchema: Yup.object({ + firstName: Yup.string() + .trim() + .required('نام اجباری است.'), + lastName: Yup.string() + .trim() + .required('نام خانوادگی اجباری است.'), + }), + onSubmit(values) { + if (file) { + upload(file, { + onSuccess(data) { + handleSave({ + ...values, + avatarUrl: data?.data?.key, + }) + }, + onError(error) { + toast(extractErrorMessage(error), 'error') + }, + }) + } else { + handleSave(values) + } + }, + }) + + if (isLoading) { + return ( +
+
در حال بارگذاری...
+
+ ) + } + + return ( +
+
+

پروفایل

+ +
+ +
+ {formik.values.avatarUrl && !file && ( + + )} + + setFile(files[0])} + /> + +
+
+ + {formik.touched.firstName && formik.errors.firstName && ( + + )} +
+ +
+ + {formik.touched.lastName && formik.errors.lastName && ( + + )} +
+
+ +
+ +
+ +
+