From 6838868fdcdf9336cb441774dcbb2761cc71a597 Mon Sep 17 00:00:00 2001 From: mirfalltnixein <22786734+mirfalltnixein@users.noreply.github.com> Date: Wed, 28 Oct 2026 00:04:24 +0100 Subject: [PATCH] Prevent dismissing unsaved form modals --- .../src/components/Form/FormModal.test.tsx | 70 +++++++++++++++++++ frontend/src/components/Form/FormModal.tsx | 38 ++++++++++ frontend/src/components/Form/index.ts | 1 + frontend/src/locale/src/en.json | 3 + frontend/src/locale/src/et.json | 3 + frontend/src/modals/AccessListModal.tsx | 17 +++-- frontend/src/modals/ChangePasswordModal.tsx | 17 +++-- .../src/modals/CustomCertificateModal.tsx | 17 +++-- frontend/src/modals/DNSCertificateModal.tsx | 17 +++-- frontend/src/modals/DeadHostModal.tsx | 17 +++-- frontend/src/modals/HTTPCertificateModal.tsx | 17 +++-- frontend/src/modals/PermissionsModal.tsx | 17 +++-- frontend/src/modals/ProxyHostModal.tsx | 17 +++-- frontend/src/modals/RedirectionHostModal.tsx | 17 +++-- frontend/src/modals/SetPasswordModal.tsx | 17 +++-- frontend/src/modals/StreamModal.tsx | 17 +++-- frontend/src/modals/UserModal.tsx | 17 +++-- 17 files changed, 235 insertions(+), 84 deletions(-) create mode 100644 frontend/src/components/Form/FormModal.test.tsx create mode 100644 frontend/src/components/Form/FormModal.tsx diff --git a/frontend/src/components/Form/FormModal.test.tsx b/frontend/src/components/Form/FormModal.test.tsx new file mode 100644 index 000000000..7ed649dc4 --- /dev/null +++ b/frontend/src/components/Form/FormModal.test.tsx @@ -0,0 +1,70 @@ +import { cleanup, fireEvent, render, screen } from "@testing-library/react"; +import { Field, Form } from "formik"; +import Modal from "react-bootstrap/Modal"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; +import { FormModal } from "./FormModal"; + +const renderModal = (onHide = vi.fn()) => { + render( + + {({ onHide: hide }) => ( +
+ + + + + + )} +
, + ); + return onHide; +}; + +describe("FormModal", () => { + beforeEach(() => { + vi.stubGlobal("confirm", vi.fn()); + }); + + afterEach(() => { + cleanup(); + vi.restoreAllMocks(); + vi.unstubAllGlobals(); + }); + + it("dismisses a pristine form without confirmation", () => { + const onHide = renderModal(); + const confirm = vi.spyOn(window, "confirm"); + + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + + expect(confirm).not.toHaveBeenCalled(); + expect(onHide).toHaveBeenCalledOnce(); + }); + + it("keeps a dirty form open when dismissal is cancelled", () => { + const onHide = renderModal(); + const confirm = vi.spyOn(window, "confirm").mockReturnValue(false); + fireEvent.change(screen.getByLabelText("Name"), { target: { value: "example.com" } }); + + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + fireEvent.click(screen.getByRole("button", { name: "Close" })); + fireEvent.click(document.querySelector(".modal") as Element); + fireEvent.keyDown(document, { key: "Escape", keyCode: 27 }); + + expect(confirm).toHaveBeenCalledTimes(4); + expect(onHide).not.toHaveBeenCalled(); + expect(screen.getByLabelText("Name")).toHaveValue("example.com"); + }); + + it("dismisses a dirty form after confirmation", () => { + const onHide = renderModal(); + vi.spyOn(window, "confirm").mockReturnValue(true); + fireEvent.change(screen.getByLabelText("Name"), { target: { value: "example.com" } }); + + fireEvent.click(screen.getByRole("button", { name: "Cancel" })); + + expect(onHide).toHaveBeenCalledOnce(); + }); +}); diff --git a/frontend/src/components/Form/FormModal.tsx b/frontend/src/components/Form/FormModal.tsx new file mode 100644 index 000000000..8c305ac4d --- /dev/null +++ b/frontend/src/components/Form/FormModal.tsx @@ -0,0 +1,38 @@ +import { Formik, type FormikConfig, type FormikProps, type FormikValues } from "formik"; +import type { ReactNode } from "react"; +import Modal from "react-bootstrap/Modal"; +import { intl } from "src/locale"; + +interface FormModalProps extends Omit, "children"> { + show: boolean; + onHide: () => void; + children: (props: FormikProps & { onHide: () => void }) => ReactNode; +} + +const FormModal = ({ show, onHide, children, ...formikProps }: FormModalProps) => ( + + {(formikProps) => { + const handleHide = () => { + if ( + !formikProps.dirty || + window.confirm( + intl.formatMessage({ + id: "form.unsaved-changes", + defaultMessage: "You have unsaved changes. Are you sure you want to discard them?", + }), + ) + ) { + onHide(); + } + }; + + return ( + + {children({ ...formikProps, onHide: handleHide })} + + ); + }} + +); + +export { FormModal }; diff --git a/frontend/src/components/Form/index.ts b/frontend/src/components/Form/index.ts index f218b2e33..8530d6f6d 100644 --- a/frontend/src/components/Form/index.ts +++ b/frontend/src/components/Form/index.ts @@ -3,6 +3,7 @@ export * from "./AccessField"; export * from "./BasicAuthFields"; export * from "./DNSProviderFields"; export * from "./DomainNamesField"; +export * from "./FormModal"; export * from "./LocationsFields"; export * from "./NginxConfigField"; export * from "./SSLCertificateField"; diff --git a/frontend/src/locale/src/en.json b/frontend/src/locale/src/en.json index 11d09ea71..14c1d5b44 100644 --- a/frontend/src/locale/src/en.json +++ b/frontend/src/locale/src/en.json @@ -437,6 +437,9 @@ "footer.github-fork": { "defaultMessage": "Fork me on Github" }, + "form.unsaved-changes": { + "defaultMessage": "You have unsaved changes. Are you sure you want to discard them?" + }, "host.flags.block-exploits": { "defaultMessage": "Block Common Exploits" }, diff --git a/frontend/src/locale/src/et.json b/frontend/src/locale/src/et.json index 316ede752..2301c5a46 100644 --- a/frontend/src/locale/src/et.json +++ b/frontend/src/locale/src/et.json @@ -437,6 +437,9 @@ "footer.github-fork": { "defaultMessage": "Tee GitHubis fork" }, + "form.unsaved-changes": { + "defaultMessage": "Sul on salvestamata muudatusi. Kas oled kindel, et soovid need tühistada?" + }, "host.flags.block-exploits": { "defaultMessage": "Blokeeri levinud ründed" }, diff --git a/frontend/src/modals/AccessListModal.tsx b/frontend/src/modals/AccessListModal.tsx index 79537f5cd..559740d3c 100644 --- a/frontend/src/modals/AccessListModal.tsx +++ b/frontend/src/modals/AccessListModal.tsx @@ -1,6 +1,7 @@ import cn from "classnames"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; -import { Field, Form, Formik } from "formik"; +import { Field, Form } from "formik"; +import { FormModal } from "src/components/Form"; import { type ReactNode, useState } from "react"; import { Alert } from "react-bootstrap"; import Modal from "react-bootstrap/Modal"; @@ -86,7 +87,7 @@ const AccessListModal = EasyModal.create(({ id, visible, remove }: Props) => { const toggleEnabled = cn(toggleClasses, "bg-cyan"); return ( - + <> {!isLoading && error && ( {error?.message || "Unknown error"} @@ -94,7 +95,9 @@ const AccessListModal = EasyModal.create(({ id, visible, remove }: Props) => { )} {isLoading && } {!isLoading && data && ( - { } onSubmit={onSubmit} > - {({ setFieldValue }: any) => ( + {({ onHide, setFieldValue }: any) => (
@@ -267,7 +270,7 @@ const AccessListModal = EasyModal.create(({ id, visible, remove }: Props) => { -
@@ -206,8 +209,8 @@ const HTTPCertificateModal = EasyModal.create(({ visible, remove }: InnerModalPr )} - - + + ); }); diff --git a/frontend/src/modals/PermissionsModal.tsx b/frontend/src/modals/PermissionsModal.tsx index d363de9ed..3c808abf0 100644 --- a/frontend/src/modals/PermissionsModal.tsx +++ b/frontend/src/modals/PermissionsModal.tsx @@ -1,7 +1,8 @@ import { useQueryClient } from "@tanstack/react-query"; import cn from "classnames"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; -import { Field, Form, Formik } from "formik"; +import { Field, Form } from "formik"; +import { FormModal } from "src/components/Form"; import { type ReactNode, useState } from "react"; import { Alert } from "react-bootstrap"; import Modal from "react-bootstrap/Modal"; @@ -133,7 +134,7 @@ const PermissionsModal = EasyModal.create(({ id, visible, remove }: Props) => { const isAdmin = data?.roles.indexOf("admin") !== -1; return ( - + <> {!isLoading && error && ( {error?.message || "Unknown error"} @@ -141,7 +142,9 @@ const PermissionsModal = EasyModal.create(({ id, visible, remove }: Props) => { )} {isLoading && } {!isLoading && data && ( - { } onSubmit={onSubmit} > - {() => ( + {({ onHide }) => (
@@ -263,7 +266,7 @@ const PermissionsModal = EasyModal.create(({ id, visible, remove }: Props) => { )} -
- @@ -368,9 +371,9 @@ const ProxyHostModal = EasyModal.create(({ id, visible, remove }: Props) => { )} -
+ )} -
+ ); }); diff --git a/frontend/src/modals/RedirectionHostModal.tsx b/frontend/src/modals/RedirectionHostModal.tsx index 9a0d95ce8..a621cf1f7 100644 --- a/frontend/src/modals/RedirectionHostModal.tsx +++ b/frontend/src/modals/RedirectionHostModal.tsx @@ -1,7 +1,8 @@ import { IconSettings } from "@tabler/icons-react"; import cn from "classnames"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; -import { Field, Form, Formik } from "formik"; +import { Field, Form } from "formik"; +import { FormModal } from "src/components/Form"; import { type ReactNode, useState } from "react"; import { Alert } from "react-bootstrap"; import Modal from "react-bootstrap/Modal"; @@ -55,7 +56,7 @@ const RedirectionHostModal = EasyModal.create(({ id, visible, remove }: Props) = }; return ( - + <> {!isLoading && error && ( {error?.message || "Unknown error"} @@ -63,7 +64,9 @@ const RedirectionHostModal = EasyModal.create(({ id, visible, remove }: Props) = )} {isLoading && } {!isLoading && data && ( - - {() => ( + {({ onHide }) => (
@@ -326,7 +329,7 @@ const RedirectionHostModal = EasyModal.create(({ id, visible, remove }: Props) = -