Skip to content

Commit

Permalink
fix(ui-commons): display a popup to warn of unsaved modifications on …
Browse files Browse the repository at this point in the history
…Form (#2071)
  • Loading branch information
skamril authored Jun 18, 2024
1 parent 605713d commit 3da72c1
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
1 change: 1 addition & 0 deletions webapp/public/locales/en/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"maintenance.error.maintenanceError": "Unable to retrieve maintenance status",
"form.submit.error": "Error while submitting",
"form.submit.inProgress": "The form is being submitted. Are you sure you want to leave the page?",
"form.changeNotSaved": "The form has not been saved. Are you sure you want to leave the page?",
"form.asyncDefaultValues.error": "Failed to get values",
"form.field.required": "Field required",
"form.field.duplicate": "Value already exists",
Expand Down
1 change: 1 addition & 0 deletions webapp/public/locales/fr/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
"maintenance.error.maintenanceError": "Impossible de récupérer le status de maintenance de l'application",
"form.submit.error": "Erreur lors de la soumission",
"form.submit.inProgress": "Le formulaire est en cours de soumission. Etes-vous sûr de vouloir quitter la page ?",
"form.changeNotSaved": "Le formulaire n'a pas été sauvegardé. Etes-vous sûr de vouloir quitter la page ?",
"form.asyncDefaultValues.error": "Impossible d'obtenir les valeurs",
"form.field.required": "Champ requis",
"form.field.duplicate": "Cette valeur existe déjà",
Expand Down
9 changes: 8 additions & 1 deletion webapp/src/components/common/Form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,12 @@ function Form<TFieldValues extends FieldValues, TContext>(
const enqueueErrorSnackbar = useEnqueueErrorSnackbar();
const { t } = useTranslation();
const autoSubmitConfig = toAutoSubmitConfig(autoSubmit);

const [showAutoSubmitLoader, setShowAutoSubmitLoader] = useDebouncedState(
false,
750,
);

const fieldAutoSubmitListeners = useRef<
Record<string, ((v: any) => any | Promise<any>) | undefined>
>({});
Expand All @@ -129,6 +131,7 @@ function Form<TFieldValues extends FieldValues, TContext>(
// eslint-disable-next-line @typescript-eslint/no-empty-function
const submitSuccessfulCb = useRef(() => {});
const preventClose = useRef(false);

const contextValue = useMemo(
() => ({ isAutoSubmitEnabled: autoSubmitConfig.enable }),
[autoSubmitConfig.enable],
Expand Down Expand Up @@ -224,6 +227,9 @@ function Form<TFieldValues extends FieldValues, TContext>(
if (preventClose.current) {
// eslint-disable-next-line no-param-reassign
event.returnValue = t("form.submit.inProgress");
} else if (isDirty) {
// eslint-disable-next-line no-param-reassign
event.returnValue = t("form.changeNotSaved");
}
};

Expand All @@ -232,13 +238,14 @@ function Form<TFieldValues extends FieldValues, TContext>(
return () => {
window.removeEventListener("beforeunload", listener);
};
}, [t]);
}, [t, isDirty]);

useUpdateEffect(() => onStateChange?.(formState), [formState]);

useEffect(() => setRef(apiRef, formApiPlus));

usePrompt(t("form.submit.inProgress"), preventClose.current);
usePrompt(t("form.changeNotSaved"), isDirty);

////////////////////////////////////////////////////////////////
// Submit
Expand Down

0 comments on commit 3da72c1

Please sign in to comment.