refactor(Form): centralize error management

This commit is contained in:
Ali BARIN
2024-12-17 17:32:31 +00:00
parent 3de18ab46f
commit ce4e4b4885
4 changed files with 59 additions and 28 deletions

View File

@@ -68,7 +68,7 @@ function InstallationForm() {
});
};
const handleSubmit = async ({ fullName, email, password }, e, setError) => {
const handleSubmit = async ({ fullName, email, password }) => {
try {
await install({
fullName,
@@ -77,29 +77,8 @@ function InstallationForm() {
});
} 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('installationForm.error'),
});
if (generalError) {
setError('root.general', {
type: 'requestError',
message: generalError,
});
}
throw errors;
}
};