From b2408f94715677c253f27c59e1898351b1aa1544 Mon Sep 17 00:00:00 2001 From: Olexii Bulhakov Date: Sun, 14 Jul 2024 09:59:56 +0300 Subject: [PATCH] Added Unsaved Changes Warning --- app/components/UnsavedChangesWarning.js | 22 ++++++++++++++++++++++ app/questionnaire/layout.jsx | 3 ++- app/results/page.jsx | 15 ++++++++++----- 3 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 app/components/UnsavedChangesWarning.js diff --git a/app/components/UnsavedChangesWarning.js b/app/components/UnsavedChangesWarning.js new file mode 100644 index 0000000..1256d1e --- /dev/null +++ b/app/components/UnsavedChangesWarning.js @@ -0,0 +1,22 @@ +"use client"; + +import { useEffect } from "react"; + +const UnsavedChangesWarning = () => { + useEffect(() => { + const handleBeforeUnload = (event) => { + event.preventDefault(); + event.returnValue = ""; // Современные браузеры требуют, чтобы значение было пустой строкой + }; + + window.addEventListener("beforeunload", handleBeforeUnload); + + return () => { + window.removeEventListener("beforeunload", handleBeforeUnload); + }; + }, []); + + return null; +}; + +export default UnsavedChangesWarning; diff --git a/app/questionnaire/layout.jsx b/app/questionnaire/layout.jsx index d27fea9..503dae7 100644 --- a/app/questionnaire/layout.jsx +++ b/app/questionnaire/layout.jsx @@ -12,7 +12,7 @@ import FormNavigationButton from "../components/FormNavigationButton"; import Building from "../model/Building"; import monthlyDurationIntervals from "../model/reference-data/monthlyDurationIntervals"; import CustomAppBar from "../components/MyAppBar"; -import Background from "../components/Background"; +import UnsavedChangesWarning from "../components/UnsavedChangesWarning"; const steps = [ { @@ -156,6 +156,7 @@ export default function QuestionnaireLayout({ children }) { return ( <> + {