Skip to content

Commit

Permalink
refactor function for sonarcloud compability
Browse files Browse the repository at this point in the history
  • Loading branch information
sirtawast committed Aug 29, 2023
1 parent bc21c9b commit a361114
Showing 1 changed file with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ type DeMinimisFormikPromises = Promise<
[void | FormikErrors<Application>, void | FormikErrors<Application>]
>;

const hasBusinessActivitiesOrIsCompany = (
hasBusinessActivities: boolean,
organizationType: ORGANIZATION_TYPES
): boolean =>
hasBusinessActivities === true ||
organizationType === ORGANIZATION_TYPES.COMPANY;

const useApplicationFormStep1 = (
application: Partial<Application>,
isUnfinishedDeminimisAid: boolean
Expand Down Expand Up @@ -126,27 +133,38 @@ const useApplicationFormStep1 = (
const getErrorMessage = (fieldName: string): string | undefined =>
getErrorText(errors, touched, fieldName, t, isSubmitted);

const checkForFieldValidity = (errs: FormikErrors<Application>): boolean => {
const errorFieldKey = Object.keys(errs)[0];

if (errorFieldKey) {
focusAndScroll(errorFieldKey);
return false;
}

if (isDeMinimisAidRowUnfinished()) {
focusAndScroll('deMinimisAid');
return false;
}

void formik.validateForm();
return true;
};

const submitIfFormValid = (isFormValid: boolean): boolean => {
void handleDeMinimisRadioButtonChange(fields);
if (isFormValid) {
void formik.submitForm();
return true;
}
return false;
};

const handleSubmit = (): void => {
setIsSubmitted(true);
void formik
.validateForm()
.then((errs) => {
const errorFieldKey = Object.keys(errs)[0];

if (errorFieldKey) {
return focusAndScroll(errorFieldKey);
}

if (isDeMinimisAidRowUnfinished()) {
void formik.validateForm();
return focusAndScroll('deMinimisAid');
}
return true;
})
.then(() => {
void handleDeMinimisRadioButtonChange(fields);
return void formik.submitForm();
});
.then((errs) => checkForFieldValidity(errs))
.then((isFormValid: boolean) => submitIfFormValid(isFormValid));
};

const handleSave = (): void | boolean => {
Expand All @@ -170,9 +188,10 @@ const useApplicationFormStep1 = (
void setFieldValue(fields.deMinimisAid.name, null);
}, [fields.deMinimisAid.name, setDeMinimisAids, setFieldValue]);

const showDeminimisSection =
values.associationHasBusinessActivities === true ||
organizationType === ORGANIZATION_TYPES.COMPANY;
const showDeminimisSection = hasBusinessActivitiesOrIsCompany(
values.associationHasBusinessActivities,
organizationType
);

const languageOptions = React.useMemo(
(): OptionType<string>[] => getLanguageOptions(t, 'languages'),
Expand Down

0 comments on commit a361114

Please sign in to comment.