refactor: remove redundant errors setting in SignUpForm
This commit is contained in:
@@ -9,7 +9,6 @@ import { yupResolver } from '@hookform/resolvers/yup';
|
|||||||
import { useQueryClient } from '@tanstack/react-query';
|
import { useQueryClient } from '@tanstack/react-query';
|
||||||
import Link from '@mui/material/Link';
|
import Link from '@mui/material/Link';
|
||||||
|
|
||||||
import { getGeneralErrorMessage } from 'helpers/errors';
|
|
||||||
import useFormatMessage from 'hooks/useFormatMessage';
|
import useFormatMessage from 'hooks/useFormatMessage';
|
||||||
import useInstallation from 'hooks/useInstallation';
|
import useInstallation from 'hooks/useInstallation';
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
@@ -77,8 +76,7 @@ function InstallationForm() {
|
|||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errors = error?.response?.data?.errors;
|
const errors = error?.response?.data?.errors;
|
||||||
|
throw errors || error;
|
||||||
throw errors;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import Alert from '@mui/material/Alert';
|
|||||||
import * as yup from 'yup';
|
import * as yup from 'yup';
|
||||||
import { yupResolver } from '@hookform/resolvers/yup';
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
|
|
||||||
import { getGeneralErrorMessage } from 'helpers/errors';
|
|
||||||
import useAuthentication from 'hooks/useAuthentication';
|
import useAuthentication from 'hooks/useAuthentication';
|
||||||
import * as URLS from 'config/urls';
|
import * as URLS from 'config/urls';
|
||||||
import Form from 'components/Form';
|
import Form from 'components/Form';
|
||||||
@@ -70,7 +69,7 @@ function SignUpForm() {
|
|||||||
}
|
}
|
||||||
}, [authentication.isAuthenticated]);
|
}, [authentication.isAuthenticated]);
|
||||||
|
|
||||||
const handleSubmit = async (values, e, setError) => {
|
const handleSubmit = async (values) => {
|
||||||
try {
|
try {
|
||||||
const { fullName, email, password } = values;
|
const { fullName, email, password } = values;
|
||||||
await registerUser({
|
await registerUser({
|
||||||
@@ -86,29 +85,7 @@ function SignUpForm() {
|
|||||||
authentication.updateToken(token);
|
authentication.updateToken(token);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
const errors = error?.response?.data?.errors;
|
const errors = error?.response?.data?.errors;
|
||||||
if (errors) {
|
throw errors || error;
|
||||||
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,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
// Helpers to extract errors received from the API
|
|
||||||
|
|
||||||
export const getGeneralErrorMessage = ({ error, fallbackMessage }) => {
|
|
||||||
if (!error) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const errors = error?.response?.data?.errors;
|
|
||||||
const generalError = errors?.general;
|
|
||||||
|
|
||||||
if (generalError && Array.isArray(generalError)) {
|
|
||||||
return generalError.join(' ');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!errors) {
|
|
||||||
return error?.message || fallbackMessage;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -152,7 +152,6 @@
|
|||||||
"signupForm.passwordsMustMatch": "Passwords must match.",
|
"signupForm.passwordsMustMatch": "Passwords must match.",
|
||||||
"signupForm.passwordMinLength": "Password must be at least 6 characters long.",
|
"signupForm.passwordMinLength": "Password must be at least 6 characters long.",
|
||||||
"signupForm.mandatoryInput": "{inputName} is required.",
|
"signupForm.mandatoryInput": "{inputName} is required.",
|
||||||
"signupForm.error": "Something went wrong. Please try again.",
|
|
||||||
"loginForm.title": "Login",
|
"loginForm.title": "Login",
|
||||||
"loginForm.emailFieldLabel": "Email",
|
"loginForm.emailFieldLabel": "Email",
|
||||||
"loginForm.passwordFieldLabel": "Password",
|
"loginForm.passwordFieldLabel": "Password",
|
||||||
|
|||||||
Reference in New Issue
Block a user