diff --git a/frontend/src/components/Form/FormModal.test.tsx b/frontend/src/components/Form/FormModal.test.tsx index 7ed649dc4..de4080596 100644 --- a/frontend/src/components/Form/FormModal.test.tsx +++ b/frontend/src/components/Form/FormModal.test.tsx @@ -4,9 +4,9 @@ import Modal from "react-bootstrap/Modal"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { FormModal } from "./FormModal"; -const renderModal = (onHide = vi.fn()) => { +const renderModal = (onHide = vi.fn(), preventHide = false) => { render( - + {({ onHide: hide }) => (
@@ -67,4 +67,16 @@ describe("FormModal", () => { expect(onHide).toHaveBeenCalledOnce(); }); + + it("does not offer to discard changes while submission is in progress", () => { + const onHide = renderModal(vi.fn(), true); + const confirm = vi.spyOn(window, "confirm"); + fireEvent.change(screen.getByLabelText("Name"), { target: { value: "example.com" } }); + + fireEvent.click(screen.getByRole("button", { name: "Close" })); + + expect(confirm).not.toHaveBeenCalled(); + expect(onHide).not.toHaveBeenCalled(); + expect(screen.getByLabelText("Name")).toHaveValue("example.com"); + }); }); diff --git a/frontend/src/components/Form/FormModal.tsx b/frontend/src/components/Form/FormModal.tsx index 8c305ac4d..d9698521d 100644 --- a/frontend/src/components/Form/FormModal.tsx +++ b/frontend/src/components/Form/FormModal.tsx @@ -1,27 +1,44 @@ import { Formik, type FormikConfig, type FormikProps, type FormikValues } from "formik"; -import type { ReactNode } from "react"; +import { type ReactNode, useEffect } from "react"; import Modal from "react-bootstrap/Modal"; import { intl } from "src/locale"; +const confirmDiscardChanges = (dirty: boolean) => + !dirty || + window.confirm( + intl.formatMessage({ + id: "form.unsaved-changes", + defaultMessage: "You have unsaved changes. Are you sure you want to discard them?", + }), + ); + +const FormDirtyState = ({ dirty, onChange }: { dirty: boolean; onChange: (dirty: boolean) => void }) => { + useEffect(() => { + onChange(dirty); + return () => onChange(false); + }, [dirty, onChange]); + + return null; +}; + interface FormModalProps extends Omit, "children"> { show: boolean; onHide: () => void; + preventHide?: boolean; children: (props: FormikProps & { onHide: () => void }) => ReactNode; } -const FormModal = ({ show, onHide, children, ...formikProps }: FormModalProps) => ( +const FormModal = ({ + show, + onHide, + preventHide = false, + 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?", - }), - ) - ) { + if (!preventHide && confirmDiscardChanges(formikProps.dirty)) { onHide(); } }; @@ -35,4 +52,4 @@ const FormModal = ({ show, onHide, children, ...for ); -export { FormModal }; +export { confirmDiscardChanges, FormDirtyState, FormModal }; diff --git a/frontend/src/modals/AccessListModal.tsx b/frontend/src/modals/AccessListModal.tsx index 559740d3c..f1a9c9fa2 100644 --- a/frontend/src/modals/AccessListModal.tsx +++ b/frontend/src/modals/AccessListModal.tsx @@ -1,12 +1,12 @@ import cn from "classnames"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; 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"; import type { AccessList, AccessListClient, AccessListItem } from "src/api/backend"; import { AccessClientFields, BasicAuthFields, Button, Loading } from "src/components"; +import { FormModal } from "src/components/Form"; import { useAccessList, useSetAccessList } from "src/hooks"; import { intl, T } from "src/locale"; import { validateString } from "src/modules/Validations"; @@ -88,16 +88,21 @@ const AccessListModal = EasyModal.create(({ id, visible, remove }: Props) => { return ( <> - {!isLoading && error && ( - - {error?.message || "Unknown error"} - + {(isLoading || (error && !data)) && ( + + {!isLoading && error && ( + + {error?.message || "Unknown error"} + + )} + {isLoading && } + )} - {isLoading && } {!isLoading && data && ( }; return ( - <> - - {({ onHide }) => ( - - - - - - - - setError(null)} dismissible> - {error} - -
- - {({ field, form }: any) => ( -
- - - {form.errors.name ? ( -
- {form.errors.current && form.touched.current - ? form.errors.current - : null} -
- ) : null} -
- )} -
-
-
- - {({ field, form }: any) => ( -
- - - {form.errors.new ? ( -
- {form.errors.new && form.touched.new ? form.errors.new : null} -
- ) : null} -
- )} -
-
-
- - {({ field, form }: any) => ( -
- - {form.errors.confirm ? ( -
- {form.errors.confirm && form.touched.confirm - ? form.errors.confirm - : null} -
- ) : null} - -
- )} -
-
-
- - - - - - )} -
- + + {({ onHide }) => ( +
+ + + + + + + setError(null)} dismissible> + {error} + +
+ + {({ field, form }: any) => ( +
+ + + {form.errors.name ? ( +
+ {form.errors.current && form.touched.current + ? form.errors.current + : null} +
+ ) : null} +
+ )} +
+
+
+ + {({ field, form }: any) => ( +
+ + + {form.errors.new ? ( +
+ {form.errors.new && form.touched.new ? form.errors.new : null} +
+ ) : null} +
+ )} +
+
+
+ + {({ field, form }: any) => ( +
+ + {form.errors.confirm ? ( +
+ {form.errors.confirm && form.touched.confirm + ? form.errors.confirm + : null} +
+ ) : null} + +
+ )} +
+
+
+ + + + +
+ )} +
); }); diff --git a/frontend/src/modals/CustomCertificateModal.tsx b/frontend/src/modals/CustomCertificateModal.tsx index b77a56d4b..c373a8d36 100644 --- a/frontend/src/modals/CustomCertificateModal.tsx +++ b/frontend/src/modals/CustomCertificateModal.tsx @@ -2,12 +2,12 @@ import { IconAlertTriangle } from "@tabler/icons-react"; import { useQueryClient } from "@tanstack/react-query"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; 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"; import { type Certificate, createCertificate, uploadCertificate, validateCertificate } from "src/api/backend"; import { Button } from "src/components"; +import { FormModal } from "src/components/Form"; import { T } from "src/locale"; import { validateString } from "src/modules/Validations"; import { showObjectSuccess } from "src/notifications"; @@ -58,177 +58,176 @@ const CustomCertificateModal = EasyModal.create(({ visible, remove }: InnerModal }; return ( - <> - - {({ onHide }) => ( -
- - - - - - - setErrorMsg(null)} dismissible> - {errorMsg} - -
-
-

- - -

- - {({ field, form }: any) => ( -
- - - {form.errors.niceName ? ( -
- {form.errors.niceName && form.touched.niceName - ? form.errors.niceName - : null} -
- ) : null} -
- )} -
- - {({ field, form }: any) => ( -
- - { - form.setFieldValue( - field.name, - event.currentTarget.files?.length - ? event.currentTarget.files[0] - : null, - ); - }} - /> - {form.errors.certificateKey ? ( -
- {form.errors.certificateKey && form.touched.certificateKey - ? form.errors.certificateKey - : null} -
- ) : null} -
- )} -
- - {({ field, form }: any) => ( -
- - { - form.setFieldValue( - field.name, - event.currentTarget.files?.length - ? event.currentTarget.files[0] - : null, - ); - }} - /> - {form.errors.certificate ? ( -
- {form.errors.certificate && form.touched.certificate - ? form.errors.certificate - : null} -
- ) : null} -
- )} -
- - {({ field, form }: any) => ( -
- - { - form.setFieldValue( - field.name, - event.currentTarget.files?.length - ? event.currentTarget.files[0] - : null, - ); - }} - /> - {form.errors.intermediateCertificate ? ( -
- {form.errors.intermediateCertificate && - form.touched.intermediateCertificate - ? form.errors.intermediateCertificate - : null} -
- ) : null} -
- )} -
-
+ + {({ onHide }) => ( + + + + + + + + setErrorMsg(null)} dismissible> + {errorMsg} + +
+
+

+ + +

+ + {({ field, form }: any) => ( +
+ + + {form.errors.niceName ? ( +
+ {form.errors.niceName && form.touched.niceName + ? form.errors.niceName + : null} +
+ ) : null} +
+ )} +
+ + {({ field, form }: any) => ( +
+ + { + form.setFieldValue( + field.name, + event.currentTarget.files?.length + ? event.currentTarget.files[0] + : null, + ); + }} + /> + {form.errors.certificateKey ? ( +
+ {form.errors.certificateKey && form.touched.certificateKey + ? form.errors.certificateKey + : null} +
+ ) : null} +
+ )} +
+ + {({ field, form }: any) => ( +
+ + { + form.setFieldValue( + field.name, + event.currentTarget.files?.length + ? event.currentTarget.files[0] + : null, + ); + }} + /> + {form.errors.certificate ? ( +
+ {form.errors.certificate && form.touched.certificate + ? form.errors.certificate + : null} +
+ ) : null} +
+ )} +
+ + {({ field, form }: any) => ( +
+ + { + form.setFieldValue( + field.name, + event.currentTarget.files?.length + ? event.currentTarget.files[0] + : null, + ); + }} + /> + {form.errors.intermediateCertificate ? ( +
+ {form.errors.intermediateCertificate && + form.touched.intermediateCertificate + ? form.errors.intermediateCertificate + : null} +
+ ) : null} +
+ )} +
- - - - - - - )} - - +
+
+ + + + + + )} +
); }); diff --git a/frontend/src/modals/DNSCertificateModal.tsx b/frontend/src/modals/DNSCertificateModal.tsx index 134aef62c..be4cf3c22 100644 --- a/frontend/src/modals/DNSCertificateModal.tsx +++ b/frontend/src/modals/DNSCertificateModal.tsx @@ -1,12 +1,12 @@ import { useQueryClient } from "@tanstack/react-query"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; 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"; import { createCertificate } from "src/api/backend"; import { Button, DNSProviderFields, DomainNamesField } from "src/components"; +import { FormModal } from "src/components/Form"; import { T } from "src/locale"; import { showObjectSuccess } from "src/notifications"; @@ -37,79 +37,78 @@ const DNSCertificateModal = EasyModal.create(({ visible, remove }: InnerModalPro }; return ( - <> - - {({ onHide }) => ( -
- - - - - - - setErrorMsg(null)} dismissible> - {errorMsg} - -
-
- - - {({ field }: any) => ( -
- - - - - -
- )} -
- -
+ + {({ onHide }) => ( + + + + + + + + setErrorMsg(null)} dismissible> + {errorMsg} + +
+
+ + + {({ field }: any) => ( +
+ + + + + +
+ )} +
+
- - - - - - - )} - - +
+
+ + + + + + )} +
); }); diff --git a/frontend/src/modals/DeadHostModal.tsx b/frontend/src/modals/DeadHostModal.tsx index f0c0d96c0..8205936a5 100644 --- a/frontend/src/modals/DeadHostModal.tsx +++ b/frontend/src/modals/DeadHostModal.tsx @@ -1,7 +1,6 @@ import { IconSettings } from "@tabler/icons-react"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; import { 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"; @@ -13,6 +12,7 @@ import { SSLCertificateField, SSLOptionsFields, } from "src/components"; +import { FormModal } from "src/components/Form"; import { useDeadHost, useSetDeadHost } from "src/hooks"; import { T } from "src/locale"; import { showObjectSuccess } from "src/notifications"; @@ -55,16 +55,21 @@ const DeadHostModal = EasyModal.create(({ id, visible, remove }: Props) => { return ( <> - {!isLoading && error && ( - - {error?.message || "Unknown error"} - + {(isLoading || (error && !data)) && ( + + {!isLoading && error && ( + + {error?.message || "Unknown error"} + + )} + {isLoading && } + )} - {isLoading && } {!isLoading && data && ( - - {({ onHide }) => ( -
- - - - - - - setErrorMsg(null)} dismissible> - {errorMsg} - -
-
-

- - -

- { - setDomains(doms); - setTestResults(null); - }} - /> - - {({ field }: any) => ( -
- - - - - -
- )} -
+ + {({ onHide }) => ( + + + + + + + + setErrorMsg(null)} dismissible> + {errorMsg} + +
+
+

+ + +

+ { + setDomains(doms); + setTestResults(null); + }} + /> + + {({ field }: any) => ( +
+ + + + + +
+ )} +
+
+ {testResults ? ( +
+
+ +
+ {parseTestResults()}
- {testResults ? ( -
-
- -
- {parseTestResults()} -
- ) : null} -
-
- -
+ + + +
+ -
- - -
- - - )} - - + +
+
+ + )} + ); }); diff --git a/frontend/src/modals/PermissionsModal.tsx b/frontend/src/modals/PermissionsModal.tsx index 3c808abf0..4f6bc0fab 100644 --- a/frontend/src/modals/PermissionsModal.tsx +++ b/frontend/src/modals/PermissionsModal.tsx @@ -2,12 +2,12 @@ import { useQueryClient } from "@tanstack/react-query"; import cn from "classnames"; import EasyModal, { type InnerModalProps } from "ez-modal-react"; 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"; import { setPermissions } from "src/api/backend"; import { Button, Loading } from "src/components"; +import { FormModal } from "src/components/Form"; import { useUser } from "src/hooks"; import { T } from "src/locale"; import styles from "./PermissionsModal.module.css"; @@ -135,16 +135,21 @@ const PermissionsModal = EasyModal.create(({ id, visible, remove }: Props) => { return ( <> - {!isLoading && error && ( - - {error?.message || "Unknown error"} - + {(isLoading || (error && !data)) && ( + + {!isLoading && error && ( + + {error?.message || "Unknown error"} + + )} + {isLoading && } + )} - {isLoading && } {!isLoading && data && ( { return ( <> - {!isLoading && (error || userError) && ( - - {error?.message || userError?.message || "Unknown error"} - + {(isLoading || userIsLoading || ((error || userError) && (!data || !currentUser))) && ( + + {!isLoading && (error || userError) && ( + + {error?.message || userError?.message || "Unknown error"} + + )} + {(isLoading || userIsLoading) && } + )} - {isLoading || (userIsLoading && )} {!isLoading && !userIsLoading && data && currentUser && ( - {!isLoading && error && ( - - {error?.message || "Unknown error"} - + {(isLoading || (error && !data)) && ( + + {!isLoading && error && ( + + {error?.message || "Unknown error"} + + )} + {isLoading && } + )} - {isLoading && } {!isLoading && data && ( { }; return ( - <> - - {({ onHide }) => ( -
- - - - - - - setError(null)} dismissible> - {error} - -
- - {({ field, form }: any) => ( - <> -

- - { - e.preventDefault(); - form.setFieldValue( - field.name, - generate({ - length: 12, - numbers: true, - }), - ); - setShowPassword(true); - }} - > - - {" "} - —{" "} - { - e.preventDefault(); - setShowPassword(!showPassword); - }} - > - - - -

-
- - - {form.errors.new ? ( -
- {form.errors.new && form.touched.new ? form.errors.new : null} -
- ) : null} -
- - )} -
-
-
- - - - -
- )} -
- + + {({ onHide }) => ( +
+ + + + + + + setError(null)} dismissible> + {error} + +
+ + {({ field, form }: any) => ( + <> +

+ + { + e.preventDefault(); + form.setFieldValue( + field.name, + generate({ + length: 12, + numbers: true, + }), + ); + setShowPassword(true); + }} + > + + {" "} + —{" "} + { + e.preventDefault(); + setShowPassword(!showPassword); + }} + > + + + +

+
+ + + {form.errors.new ? ( +
+ {form.errors.new && form.touched.new ? form.errors.new : null} +
+ ) : null} +
+ + )} +
+
+
+ + + + +
+ )} +
); }); diff --git a/frontend/src/modals/StreamModal.tsx b/frontend/src/modals/StreamModal.tsx index b6f35622f..417ae3103 100644 --- a/frontend/src/modals/StreamModal.tsx +++ b/frontend/src/modals/StreamModal.tsx @@ -1,10 +1,10 @@ import EasyModal, { type InnerModalProps } from "ez-modal-react"; 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"; import { Button, Loading, SSLCertificateField, SSLOptionsFields } from "src/components"; +import { FormModal } from "src/components/Form"; import { useSetStream, useStream } from "src/hooks"; import { intl, T } from "src/locale"; import { validateNumber, validateString } from "src/modules/Validations"; @@ -48,16 +48,21 @@ const StreamModal = EasyModal.create(({ id, visible, remove }: Props) => { return ( <> - {!isLoading && error && ( - - {error?.message || "Unknown error"} - + {(isLoading || (error && !data)) && ( + + {!isLoading && error && ( + + {error?.message || "Unknown error"} + + )} + {isLoading && } + )} - {isLoading && } {!isLoading && data && ( { const [setupData, setSetupData] = useState<{ secret: string; otpauthUrl: string } | null>(null); const [backupCodes, setBackupCodes] = useState([]); const [isSubmitting, setIsSubmitting] = useState(false); + const [formDirty, setFormDirty] = useState(false); + + const handleHide = () => { + if (!isSubmitting && confirmDiscardChanges(formDirty)) { + remove(); + } + }; const loadStatus = useCallback(async () => { try { @@ -172,8 +180,9 @@ const TwoFactorModal = EasyModal.create(({ id, visible, remove }: Props) => { /> - {() => ( + {({ dirty }) => (
+ {({ field, form }: any) => (