import { FC, useCallback, useEffect, useState } from 'react' import { useTranslation } from 'react-i18next' import AvatarImage from '../../assets/images/avatar_image.png' import Button from '../../components/Button' import { DocumentUpload } from 'iconsax-react' import Input from '../../components/Input' import DatePickerComponent from '../../components/DatePicker' import { useGetProfile, useUpdateProfile } from './hooks/useProfileData' import Username from './components/Username' import { useDropzone } from 'react-dropzone' import { toast } from '../../components/Toast' import { useSingleUpload } from '../ticket/hooks/useTicketData' import { ErrorType } from '../../helpers/types' import { UpdateProfileType } from './types/ProfileTypes' import PageLoading from '../../components/PageLoading' import { useGetProvines } from '../financial/hooks/useFinancialData' import { useFormik } from 'formik' import * as Yup from 'yup' import Email from './components/Email' import Phone from './components/Phone' const Profile: FC = () => { const { t } = useTranslation('global') const [file, setFile] = useState() const getProvines = useGetProvines() const getProfile = useGetProfile() const [percent, setPercent] = useState(0) const singleUpload = useSingleUpload(setPercent) const updateProfile = useUpdateProfile() const formik = useFormik({ initialValues: { cityId: '', userAddress: '', postalCode: '', }, validationSchema: Yup.object({ cityId: Yup.string().required(t('errors.required')), userAddress: Yup.string().required(t('errors.required')), postalCode: Yup.string().required(t('errors.required')), }), onSubmit: (values) => { updateProfile.mutate(values, { onSuccess: () => { toast(t('success'), 'success') }, onError: (error: ErrorType) => { toast(error.response?.data?.error?.message[0], 'error') } }) } }) useEffect(() => { if (getProfile.data) { formik.setValues({ cityId: getProfile.data.data.user.city?.id || '', userAddress: getProfile.data.data.user.userAddress || '', postalCode: getProfile.data.data.user.postalCode || '', }) if (getProfile.data.data.user.city) { } } }, [getProfile.data]) const onDrop = useCallback((acceptedFiles: File[]) => { if (acceptedFiles.length > 0) { const file = acceptedFiles[0]; if (file.type.startsWith('image/')) { setFile(file); const formData = new FormData() formData.append('file', file) singleUpload.mutate(formData, { onSuccess: (data) => { const params: UpdateProfileType = { profilePic: data.data?.url } updateProfile.mutate(params, { onSuccess: () => { toast(t('profile.image_uploaded_successfully'), 'success') }, onError: (error: ErrorType) => { toast(error.response?.data?.error?.message[0], 'error') } }) }, onError: (error: ErrorType) => { toast(error.response?.data?.error?.message[0], 'error') } }) } else { toast(t('errors.is_not_image'), 'error') } } }, []) const { getRootProps, getInputProps } = useDropzone({ onDrop }) return (
{t('profile.account_user')}
{ getProfile.isPending || getProvines.isPending ? :
{t('profile.image_profile')}
{t('profile.image_profile_desc')}

{t('profile.formats')}

{t('profile.format_image')}
{t('profile.max_size')}

{t('profile.info_account')}
{t('profile.auth_after_change_info')}
{t('profile.personal_information')}
{t('profile.enter_carefully_your_information')}
{ }} placeholder='' defaulValue={getProfile.data?.data?.user?.birthDate} />
{/*
{t('profile.address')}
{t('profile.address_live')}
({ label: item.name, value: item.id })) : []} placeholder={t('select')} {...formik.getFieldProps('cityId')} error_text={formik.touched.cityId && formik.errors.cityId ? formik.errors.cityId : ''} />