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 Link from '@mui/material/Link';
|
||||
|
||||
import { getGeneralErrorMessage } from 'helpers/errors';
|
||||
import useFormatMessage from 'hooks/useFormatMessage';
|
||||
import useInstallation from 'hooks/useInstallation';
|
||||
import * as URLS from 'config/urls';
|
||||
@@ -77,8 +76,7 @@ function InstallationForm() {
|
||||
});
|
||||
} catch (error) {
|
||||
const errors = error?.response?.data?.errors;
|
||||
|
||||
throw errors;
|
||||
throw errors || error;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@ 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';
|
||||
@@ -70,7 +69,7 @@ function SignUpForm() {
|
||||
}
|
||||
}, [authentication.isAuthenticated]);
|
||||
|
||||
const handleSubmit = async (values, e, setError) => {
|
||||
const handleSubmit = async (values) => {
|
||||
try {
|
||||
const { fullName, email, password } = values;
|
||||
await registerUser({
|
||||
@@ -86,29 +85,7 @@ function SignUpForm() {
|
||||
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,
|
||||
});
|
||||
}
|
||||
throw errors || error;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -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.passwordMinLength": "Password must be at least 6 characters long.",
|
||||
"signupForm.mandatoryInput": "{inputName} is required.",
|
||||
"signupForm.error": "Something went wrong. Please try again.",
|
||||
"loginForm.title": "Login",
|
||||
"loginForm.emailFieldLabel": "Email",
|
||||
"loginForm.passwordFieldLabel": "Password",
|
||||
|
||||
Reference in New Issue
Block a user