import * as React from 'react'; import { useNavigate } from 'react-router-dom'; import Paper from '@mui/material/Paper'; import Typography from '@mui/material/Typography'; import LoadingButton from '@mui/lab/LoadingButton'; import Alert from '@mui/material/Alert'; import * as yup from 'yup'; import { yupResolver } from '@hookform/resolvers/yup'; import { getGeneralErrorMessage } from 'helpers/errors'; import useAuthentication from 'hooks/useAuthentication'; import * as URLS from 'config/urls'; import Form from 'components/Form'; import TextField from 'components/TextField'; import useFormatMessage from 'hooks/useFormatMessage'; import useCreateAccessToken from 'hooks/useCreateAccessToken'; import useRegisterUser from 'hooks/useRegisterUser'; const getValidationSchema = (formatMessage) => { const getMandatoryInputMessage = (inputNameId) => formatMessage('signupForm.mandatoryInput', { inputName: formatMessage(inputNameId), }); return yup.object().shape({ fullName: yup .string() .trim() .required(getMandatoryInputMessage('signupForm.fullNameFieldLabel')), email: yup .string() .trim() .required(getMandatoryInputMessage('signupForm.emailFieldLabel')) .email(formatMessage('signupForm.validateEmail')), password: yup .string() .required(getMandatoryInputMessage('signupForm.passwordFieldLabel')) .min(6, formatMessage('signupForm.passwordMinLength')), confirmPassword: yup .string() .required( getMandatoryInputMessage('signupForm.confirmPasswordFieldLabel'), ) .oneOf( [yup.ref('password')], formatMessage('signupForm.passwordsMustMatch'), ), }); }; const defaultValues = { fullName: '', email: '', password: '', confirmPassword: '', }; function SignUpForm() { const navigate = useNavigate(); const authentication = useAuthentication(); const formatMessage = useFormatMessage(); const { mutateAsync: registerUser, isPending: isRegisterUserPending } = useRegisterUser(); const { mutateAsync: createAccessToken, isPending: loginLoading } = useCreateAccessToken(); React.useEffect(() => { if (authentication.isAuthenticated) { navigate(URLS.DASHBOARD); } }, [authentication.isAuthenticated]); const handleSubmit = async (values, e, setError) => { try { const { fullName, email, password } = values; await registerUser({ fullName, email, password, }); const { data } = await createAccessToken({ email, password, }); const { token } = data; authentication.updateToken(token); } catch (error) { const errors = error?.response?.data?.errors; if (errors) { const fieldNames = Object.keys(defaultValues); Object.entries(errors).forEach(([fieldName, fieldErrors]) => { if (fieldNames.includes(fieldName) && Array.isArray(fieldErrors)) { setError(fieldName, { type: 'fieldRequestError', message: fieldErrors.join(', '), }); } }); } const generalError = getGeneralErrorMessage({ error, fallbackMessage: formatMessage('signupForm.error'), }); if (generalError) { setError('root.general', { type: 'requestError', message: generalError, }); } } }; return ( theme.palette.text.disabled, pb: 2, mb: 2, }} gutterBottom > {formatMessage('signupForm.title')}
( <> {errors?.root?.general && ( {errors.root.general.message} )} {formatMessage('signupForm.submit')} )} /> ); } export default SignUpForm;