From 834d90f60cd84331154c99edb7eb7c03ec5e0c7f Mon Sep 17 00:00:00 2001 From: david Date: Wed, 3 Jun 2026 15:50:57 +0200 Subject: [PATCH] Confirmar cambios al refrescar el navegador --- .../use-unsaved-changes-notifier.tsx | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx b/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx index 59e07acf..eb0e9381 100644 --- a/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx +++ b/modules/core/src/web/hooks/use-unsaved-changes-notifier/use-unsaved-changes-notifier.tsx @@ -49,12 +49,12 @@ export const UnsavedChangesProvider = ({ isDirty, children }: UnsavedChangesProv }); }, [isDirty]); - const handleConfirm = useCallback((discardChanges: boolean) => { - if (discardChanges) { + const handleConfirm = useCallback((confirmed: boolean) => { + if (confirmed) { allowNavigationRef.current = true; } - resolverRef.current?.(discardChanges); + resolverRef.current?.(confirmed); resolverRef.current = null; 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( () => ({ requestConfirm,