Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EVG-16959: Reintroduce navigation warning modal #1851

Merged
merged 3 commits into from Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion cypress/integration/projectSettings/project_settings.ts
Expand Up @@ -603,9 +603,12 @@ describe(
cy.dataCy("display-name-input").should("not.have.attr", "placeholder");
});

it.skip("Shows a navigation warning modal when navigating away from project settings", () => {
it("Shows a navigation warning modal that lists the general page when navigating away from project settings", () => {
cy.contains("My Patches").click();
cy.dataCy("navigation-warning-modal").should("be.visible");
cy.dataCy("unsaved-pages").within(() => {
cy.get("li").should("have.length", 1);
});
cy.get("body").type("{esc}");
});

Expand Down
22 changes: 21 additions & 1 deletion src/pages/projectSettings/Context.tsx
Expand Up @@ -203,6 +203,21 @@ const usePopulateForm = <T extends WritableTabRoutes>(
}, [formData]); // eslint-disable-line react-hooks/exhaustive-deps
};

const useHasUnsavedTab = (): {
hasUnsaved: boolean;
unsavedTabs: ProjectSettingsTabRoutes[];
} => {
const { tabs } = useProjectSettingsContext();
const unsavedTabs = Object.entries(tabs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: memoizing unsavedTabs will prevent always returning a new reference

.filter(([, tabData]) => tabData.hasChanges)
.map(([tab]) => tab as ProjectSettingsTabRoutes);

return {
unsavedTabs,
hasUnsaved: !!unsavedTabs.length,
};
};

const getDefaultTabState = <T extends unknown>(
defaultValue: T
): Record<WritableTabRoutes, T> =>
Expand All @@ -213,4 +228,9 @@ const getDefaultTabState = <T extends unknown>(
}))
);

export { ProjectSettingsProvider, usePopulateForm, useProjectSettingsContext };
export {
ProjectSettingsProvider,
useHasUnsavedTab,
usePopulateForm,
useProjectSettingsContext,
};
40 changes: 40 additions & 0 deletions src/pages/projectSettings/NavigationModal.tsx
@@ -0,0 +1,40 @@
import { Body } from "@leafygreen-ui/typography";
import { matchPath, unstable_useBlocker as useBlocker } from "react-router-dom";
import { ConfirmationModal } from "components/ConfirmationModal";
import { routes } from "constants/routes";
import { useHasUnsavedTab } from "./Context";
import { getTabTitle } from "./getTabTitle";

export const NavigationModal: React.VFC = () => {
const { hasUnsaved, unsavedTabs } = useHasUnsavedTab();

const shouldConfirmNavigation = ({ nextLocation }): boolean => {
const isProjectSettingsRoute =
nextLocation &&
!!matchPath(`${routes.projectSettings}/*`, nextLocation.pathname);
return !isProjectSettingsRoute && hasUnsaved;
};

const blocker = useBlocker(shouldConfirmNavigation);

return (
blocker.state === "blocked" && (
<ConfirmationModal
buttonText="Leave"
data-cy="navigation-warning-modal"
open
onCancel={() => blocker.reset?.()}
onConfirm={() => blocker.proceed?.()}
title="You have unsaved changes that will be discarded. Are you sure you want to leave?"
variant="danger"
>
<Body>Unsaved changes are present on the following pages:</Body>
<ol data-cy="unsaved-pages">
{unsavedTabs.map((tab) => (
<li key={tab}>{getTabTitle(tab).title}</li>
))}
</ol>
</ConfirmationModal>
)
);
};
2 changes: 2 additions & 0 deletions src/pages/projectSettings/Tabs.tsx
Expand Up @@ -6,6 +6,7 @@ import { ProjectSettingsQuery, RepoSettingsQuery } from "gql/generated/types";
import { isProduction } from "utils/environmentVariables";
import { useProjectSettingsContext } from "./Context";
import { Header } from "./Header";
import { NavigationModal } from "./NavigationModal";
import {
AccessTab,
ContainersTab,
Expand Down Expand Up @@ -57,6 +58,7 @@ export const ProjectSettingsTabs: React.VFC<Props> = ({

return (
<Container>
<NavigationModal />
<Header
attachedRepoId={projectData?.projectRef?.repoRefId}
id={projectId || repoId}
Expand Down