refactor: remove redundant errors setting in SignUpForm

This commit is contained in:
kasia.oczkowska
2024-12-19 13:23:33 +00:00
parent ce4e4b4885
commit ebc21e90ac
4 changed files with 3 additions and 47 deletions

View File

@@ -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;
}
};

View File

@@ -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;
}
};

View File

@@ -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;
}
};

View File

@@ -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",