Skip to content

Commit

Permalink
Added Unsaved Changes Warning
Browse files Browse the repository at this point in the history
  • Loading branch information
bulhakovolexii committed Jul 14, 2024
1 parent d8e0e06 commit b2408f9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
22 changes: 22 additions & 0 deletions app/components/UnsavedChangesWarning.js
Original file line number Diff line number Diff line change
@@ -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;
3 changes: 2 additions & 1 deletion app/questionnaire/layout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
{
Expand Down Expand Up @@ -156,6 +156,7 @@ export default function QuestionnaireLayout({ children }) {

return (
<>
<UnsavedChangesWarning />
<CustomAppBar color="secondary" />
<Container
maxWidth="lg"
Expand Down
15 changes: 10 additions & 5 deletions app/results/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import {
Button,
TextField,
} from "@mui/material";
import Background from "../components/Background";
import MyAppBar from "../components/MyAppBar";
import UnsavedChangesWarning from "../components/UnsavedChangesWarning";
import Link from "next/link";

const biluxSystem = {
heatGenerator:
Expand Down Expand Up @@ -203,6 +204,7 @@ const SuccessfulResult = ({ inputData }) => {
<Grid container spacing={2} mt={1}>
<Grid item xs={12} md={6} spacing={2}>
<Button
LinkComponent={Link}
href="/questionnaire/step-7"
color="primary"
variant="contained"
Expand Down Expand Up @@ -311,9 +313,12 @@ export default function Results() {
return !inputData.city ? (
router.push("/")
) : (
<Container maxWidth="lg">
<MyAppBar color="transparent" />
<SuccessfulResult inputData={inputData} />
</Container>
<>
<UnsavedChangesWarning />
<Container maxWidth="lg">
<MyAppBar color="transparent" />
<SuccessfulResult inputData={inputData} />
</Container>
</>
);
}

0 comments on commit b2408f9

Please sign in to comment.