mirror of
https://github.com/NginxProxyManager/nginx-proxy-manager.git
synced 2026-11-25 16:11:27 +00:00
Merge pull request #5977 from mirfalltnixein/fix/unsaved-modal-changes
Prevent data loss when dismissing unsaved forms
This commit is contained in:
21 files changed
+950
-659
No files matched your search
@@ -38,8 +38,7 @@ export function BasicAuthFields({ initialValues, name = "items" }: Props) {
|
||||
};
|
||||
|
||||
const setFormField = (newValues: AccessListItem[]) => {
|
||||
const filtered = newValues.filter((v: AccessListItem) => v?.username?.trim() !== "");
|
||||
setFieldValue(name, filtered);
|
||||
setFieldValue(name, newValues);
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
import { cleanup, fireEvent, render, screen } from "@testing-library/react";
|
||||
import { Form } from "formik";
|
||||
import type { ReactNode } from "react";
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { BasicAuthFields } from "./BasicAuthFields";
|
||||
import { FormModal } from "./FormModal";
|
||||
import { LocationsFields } from "./LocationsFields";
|
||||
|
||||
vi.mock("./AccessField", () => ({ AccessField: () => null }));
|
||||
|
||||
const renderDraft = (initialValues: Record<string, unknown>, children: ReactNode) => {
|
||||
const onHide = vi.fn();
|
||||
render(
|
||||
<FormModal show onHide={onHide} initialValues={initialValues} onSubmit={vi.fn()}>
|
||||
{({ onHide: hide }) => (
|
||||
<Form>
|
||||
{children}
|
||||
<button type="button" onClick={hide}>
|
||||
Cancel
|
||||
</button>
|
||||
</Form>
|
||||
)}
|
||||
</FormModal>,
|
||||
);
|
||||
return onHide;
|
||||
};
|
||||
|
||||
describe("FormModal local drafts", () => {
|
||||
beforeEach(() => {
|
||||
vi.stubGlobal(
|
||||
"confirm",
|
||||
vi.fn(() => false),
|
||||
);
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
cleanup();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
it("protects a location draft without a path", () => {
|
||||
const onHide = renderDraft({ locations: [] }, <LocationsFields initialValues={[]} />);
|
||||
fireEvent.click(screen.getByRole("button", { name: "Add Location" }));
|
||||
fireEvent.change(screen.getByPlaceholderText("eg: 10.0.0.1/path/"), { target: { value: "example.com" } });
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
|
||||
|
||||
expect(window.confirm).toHaveBeenCalledOnce();
|
||||
expect(onHide).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("protects a basic auth password draft without a username", () => {
|
||||
const onHide = renderDraft({ items: [] }, <BasicAuthFields initialValues={[]} />);
|
||||
fireEvent.change(document.querySelector('input[type="password"]') as Element, {
|
||||
target: { value: "secret" },
|
||||
});
|
||||
|
||||
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
|
||||
|
||||
expect(window.confirm).toHaveBeenCalledOnce();
|
||||
expect(onHide).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
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(), preventHide = false) => {
|
||||
render(
|
||||
<FormModal show onHide={onHide} preventHide={preventHide} initialValues={{ name: "" }} onSubmit={vi.fn()}>
|
||||
{({ onHide: hide }) => (
|
||||
<Form>
|
||||
<Modal.Header closeButton />
|
||||
<label htmlFor="name">Name</label>
|
||||
<Field id="name" name="name" />
|
||||
<button type="button" onClick={hide}>
|
||||
Cancel
|
||||
</button>
|
||||
</Form>
|
||||
)}
|
||||
</FormModal>,
|
||||
);
|
||||
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();
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,55 @@
|
||||
import { Formik, type FormikConfig, type FormikProps, type FormikValues } from "formik";
|
||||
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<Values extends FormikValues> extends Omit<FormikConfig<Values>, "children"> {
|
||||
show: boolean;
|
||||
onHide: () => void;
|
||||
preventHide?: boolean;
|
||||
children: (props: FormikProps<Values> & { onHide: () => void }) => ReactNode;
|
||||
}
|
||||
|
||||
const FormModal = <Values extends FormikValues>({
|
||||
show,
|
||||
onHide,
|
||||
preventHide = false,
|
||||
children,
|
||||
...formikProps
|
||||
}: FormModalProps<Values>) => (
|
||||
<Formik {...formikProps}>
|
||||
{(formikProps) => {
|
||||
const handleHide = () => {
|
||||
if (!preventHide && confirmDiscardChanges(formikProps.dirty)) {
|
||||
onHide();
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<Modal show={show} onHide={handleHide}>
|
||||
{children({ ...formikProps, onHide: handleHide })}
|
||||
</Modal>
|
||||
);
|
||||
}}
|
||||
</Formik>
|
||||
);
|
||||
|
||||
export { confirmDiscardChanges, FormDirtyState, FormModal };
|
||||
@@ -88,8 +88,10 @@ export function LocationsFields({ initialValues, name = "locations" }: Props) {
|
||||
};
|
||||
|
||||
const setFormField = (newRows: Row[]) => {
|
||||
const filtered = newRows.map((r: Row) => r.value).filter((v: ProxyLocation) => v?.path?.trim() !== "");
|
||||
setFieldValue(name, filtered);
|
||||
setFieldValue(
|
||||
name,
|
||||
newRows.map((r: Row) => r.value),
|
||||
);
|
||||
};
|
||||
|
||||
const forwardSummary = (item: ProxyLocation) => {
|
||||
|
||||
@@ -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";
|
||||
|
||||
Reference in new issue
Block a user