Confirmar cambios al refrescar el navegador
This commit is contained in:
parent
66623cbe78
commit
834d90f60c
@ -49,12 +49,12 @@ export const UnsavedChangesProvider = ({ isDirty, children }: UnsavedChangesProv
|
|||||||
});
|
});
|
||||||
}, [isDirty]);
|
}, [isDirty]);
|
||||||
|
|
||||||
const handleConfirm = useCallback((discardChanges: boolean) => {
|
const handleConfirm = useCallback((confirmed: boolean) => {
|
||||||
if (discardChanges) {
|
if (confirmed) {
|
||||||
allowNavigationRef.current = true;
|
allowNavigationRef.current = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
resolverRef.current?.(discardChanges);
|
resolverRef.current?.(confirmed);
|
||||||
resolverRef.current = null;
|
resolverRef.current = null;
|
||||||
setOpen(false);
|
setOpen(false);
|
||||||
}, []);
|
}, []);
|
||||||
@ -94,6 +94,25 @@ export const UnsavedChangesProvider = ({ isDirty, children }: UnsavedChangesProv
|
|||||||
};
|
};
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isDirty) return;
|
||||||
|
|
||||||
|
const handleBeforeUnload = (event: BeforeUnloadEvent) => {
|
||||||
|
if (allowNavigationRef.current) return;
|
||||||
|
|
||||||
|
event.preventDefault();
|
||||||
|
|
||||||
|
// Necesario para compatibilidad con navegadores legacy.
|
||||||
|
event.returnValue = "";
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener("beforeunload", handleBeforeUnload);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener("beforeunload", handleBeforeUnload);
|
||||||
|
};
|
||||||
|
}, [isDirty]);
|
||||||
|
|
||||||
const contextValue = useMemo<UnsavedChangesContextValue>(
|
const contextValue = useMemo<UnsavedChangesContextValue>(
|
||||||
() => ({
|
() => ({
|
||||||
requestConfirm,
|
requestConfirm,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user