import FusePageSimple from '@fuse/core/FusePageSimple'; import _ from '@lodash'; import FuseSvgIcon from '@fuse/core/FuseSvgIcon/FuseSvgIcon'; import { yupResolver } from '@hookform/resolvers/yup'; import Button from '@mui/material/Button'; import Paper from '@mui/material/Paper'; import TextField from '@mui/material/TextField'; import { styled } from '@mui/material/styles'; import { selectUser, updateUserSettings } from 'app/store/userSlice'; import { Controller, useForm } from 'react-hook-form'; import { withTranslation } from 'react-i18next'; import { useDispatch, useSelector } from 'react-redux'; import * as yup from 'yup'; import { toBase64 } from 'src/app/utils'; import { forwardRef } from 'react'; const MAX_PICTURE_SIZE = 5000000; const AVAILABLE_MEDIA_TYPES = ['image/png', 'image/jpeg']; const Root = styled(FusePageSimple)(({ theme }) => ({ '& .FusePageSimple-header': {}, '& .FusePageSimple-toolbar': {}, '& .FusePageSimple-content': { backgroundColor: theme.palette.background.default, }, '& .FusePageSimple-sidebarHeader': {}, '& .FusePageSimple-sidebarContent': {}, })); const StyledTextField = forwardRef((props, ref) => ( theme.palette.background.paper, borderRadius: '10px', }, }} {...props} ref={ref} /> )); function ProfilePage({ t }) { const dispatch = useDispatch(); const user = useSelector(selectUser); const defaultValues = { photoURL: '', firstName: '', lastName: '', displayName: '', email: '', mobileNumber: '', information: '', address: '', ...user.data, }; const schema = yup.object().shape({ photoURL: yup.string().notRequired(), firstName: yup .string() .max(150, t('max_length_error', { length: 150 })) .trim() .notRequired(), lastName: yup .string() .max(150, t('max_length_error', { length: 150 })) .trim() .notRequired(), displayName: yup .string() .required(t('display_name_error')) .max(30, t('max_length_error', { length: 30 })) .trim(), email: yup .string() .matches(/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/g, t('email_error')) .required(t('email_error')) .max(40, t('max_length_error', { length: 40 })), mobileNumber: yup .string() .matches(/^(?=(.*\d){7,})\+?(\d[\d-. ]+)?(\([\d-. ]+\))?[\d-. ]+\d$/, { message: t('mobile_number_error'), excludeEmptyString: true, }) .max(20, t('max_length_error', { length: 20 })) .notRequired(), information: yup .string() .max(300, t('max_length_error', { length: 300 })) .trim() .notRequired(), address: yup .string() .max(150, t('max_length_error', { length: 150 })) .trim() .notRequired(), }); const { control, formState, watch, setValue, handleSubmit, setError, reset } = useForm({ mode: 'onChange', defaultValues, resolver: yupResolver(schema), }); const { dirtyFields, errors } = formState; const uploadPicture = async (event) => { const { target } = event; if (target.files && target.files[0]) { const file = target.files[0]; if (file.size > MAX_PICTURE_SIZE) { return setError('photoURL', { type: 'custom', message: t('picture_size_error') }); } if (!AVAILABLE_MEDIA_TYPES.includes(file.type)) { return setError('photoURL', { type: 'custom', message: t('picture_extensions_error'), }); } const base64 = await toBase64(file); setValue('photoURL', base64, { shouldDirty: true }); } else { setError('photoURL', { type: 'custom', message: 'Choose a file please' }); } }; const deletePicture = () => setValue('photoURL', '', { shouldDirty: true }); const onSubmit = (data) => { dispatch(updateUserSettings(data)).catch((error) => { setError('root', { type: 'manual', message: error.message, }); }); reset(data); }; return (
{watch('photoURL') && ( user )}
( )} />
  • {t('first_picture_req')}
  • {t('second_picture_req')}
( )} /> ( )} /> ( )} /> ( )} /> ( )} /> ( theme.palette.background.paper, borderRadius: '10px', }, }} // eslint-disable-next-line react/jsx-no-duplicate-props inputProps={{ sx: { padding: '16.5px 14px', }, }} /> )} /> ( theme.palette.background.paper, borderRadius: '10px', }, }} // eslint-disable-next-line react/jsx-no-duplicate-props inputProps={{ sx: { padding: '16.5px 14px', }, }} /> )} />
{errors.root?.message && (

{errors.root?.message}

)}
} scroll="content" /> ); } export default withTranslation('profilePage')(ProfilePage);