Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Display prompt before leaving unsaved form

dominik-zeglen1d72ee2feb66
diff --git a/package-lock.json b/package-lock.json
index c01ae0e1bc..6259135e2d 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -12153,14 +12153,12 @@
"balanced-match": {
"version": "1.0.0",
"bundled": true,
- "dev": true,
- "optional": true
+ "dev": true
},
"brace-expansion": {
"version": "1.1.11",
"bundled": true,
"dev": true,
- "optional": true,
"requires": {
"balanced-match": "^1.0.0",
"concat-map": "0.0.1"
@@ -12175,20 +12173,17 @@
"code-point-at": {
"version": "1.1.0",
"bundled": true,
- "dev": true,
- "optional": true
+ "dev": true
},
"concat-map": {
"version": "0.0.1",
"bundled": true,
- "dev": true,
- "optional": true
+ "dev": true
},
"console-control-strings": {
"version": "1.1.0",
"bundled": true,
- "dev": true,
- "optional": true
+ "dev": true
},
"core-util-is": {
"version": "1.0.2",
@@ -12305,8 +12300,7 @@
"inherits": {
"version": "2.0.3",
"bundled": true,
- "dev": true,
- "optional": true
+ "dev": true
},
"ini": {
"version": "1.3.5",
@@ -12318,7 +12312,6 @@
"version": "1.0.0",
"bundled": true,
"dev": true,
- "optional": true,
"requires": {
"number-is-nan": "^1.0.0"
}
@@ -12333,7 +12326,6 @@
"version": "3.0.4",
"bundled": true,
"dev": true,
- "optional": true,
"requires": {
"brace-expansion": "^1.1.7"
}
@@ -12341,14 +12333,12 @@
"minimist": {
"version": "0.0.8",
"bundled": true,
- "dev": true,
- "optional": true
+ "dev": true
},
"minipass": {
"version": "2.2.4",
"bundled": true,
"dev": true,
- "optional": true,
"requires": {
"safe-buffer": "^5.1.1",
"yallist": "^3.0.0"
@@ -12367,7 +12357,6 @@
"version": "0.5.1",
"bundled": true,
"dev": true,
- "optional": true,
"requires": {
"minimist": "0.0.8"
}
@@ -12448,8 +12437,7 @@
"number-is-nan": {
"version": "1.0.1",
"bundled": true,
- "dev": true,
- "optional": true
+ "dev": true
},
"object-assign": {
"version": "4.1.1",
@@ -12461,7 +12449,6 @@
"version": "1.4.0",
"bundled": true,
"dev": true,
- "optional": true,
"requires": {
"wrappy": "1"
}
@@ -12583,7 +12570,6 @@
"version": "1.0.2",
"bundled": true,
"dev": true,
- "optional": true,
"requires": {
"code-point-at": "^1.0.0",
"is-fullwidth-code-point": "^1.0.0",
@@ -21876,6 +21862,11 @@
"warning": "^3.0.0"
}
},
+ "react-router-navigation-prompt": {
+ "version": "1.8.2",
+ "resolved": "https://registry.npmjs.org/react-router-navigation-prompt/-/react-router-navigation-prompt-1.8.2.tgz",
+ "integrity": "sha512-jUdoU4Ri3ONhdoAqMW4knCiO4Ql9wy3IRu62BugORFMW5L2Fb3VyE4QHj0puPzVZ5lEeLCddU2vQDZwXiVUZ+g=="
+ },
"react-side-effect": {
"version": "1.1.5",
"resolved": "https://registry.npmjs.org/react-side-effect/-/react-side-effect-1.1.5.tgz",
diff --git a/package.json b/package.json
index d09bb7aebe..c2c1d334f7 100644
--- a/package.json
+++ b/package.json
@@ -71,6 +71,7 @@
"react-moment": "^0.7.7",
"react-router": "^4.2.0",
"react-router-dom": "^4.2.2",
+ "react-router-navigation-prompt": "^1.8.2",
"react-sortable-hoc": "^0.6.8",
"react-svg": "^2.2.11",
"select2": "^4.0.5",
diff --git a/saleor/static/dashboard-next/components/ConfirmFormLeaveDialog.tsx b/saleor/static/dashboard-next/components/ConfirmFormLeaveDialog.tsx
new file mode 100644
index 0000000000..a57027dfcc
--- /dev/null
+++ b/saleor/static/dashboard-next/components/ConfirmFormLeaveDialog.tsx
@@ -0,0 +1,33 @@
+import * as React from "react";
+import NavigationPrompt from "react-router-navigation-prompt";
+
+import DialogContentText from "@material-ui/core/DialogContentText";
+import i18n from "../i18n";
+import ActionDialog from "./ActionDialog";
+import { FormContext } from "./Form";
+
+export const ConfirmFormLeaveDialog: React.StatelessComponent<{}> = () => (
+ <FormContext.Consumer>
+ {({ hasChanged: hasFormChanged }) => (
+ <NavigationPrompt renderIfNotActive={true} when={hasFormChanged}>
+ {({ isActive, onCancel, onConfirm }) => (
+ <ActionDialog
+ open={isActive}
+ onClose={onCancel}
+ onConfirm={onConfirm}
+ confirmButtonState="default"
+ title={i18n.t("Leaving form", {
+ context: "modal title"
+ })}
+ >
+ <DialogContentText>
+ {i18n.t("Are you sure you want to leave unsaved changes?", {
+ context: "form leave confirmation"
+ })}
+ </DialogContentText>
+ </ActionDialog>
+ )}
+ </NavigationPrompt>
+ )}
+ </FormContext.Consumer>
+);
diff --git a/saleor/static/dashboard-next/index.tsx b/saleor/static/dashboard-next/index.tsx
index a05b9ab8c2..915bf5c8c6 100644
--- a/saleor/static/dashboard-next/index.tsx
+++ b/saleor/static/dashboard-next/index.tsx
@@ -20,6 +20,7 @@ import Login from "./auth/views/Login";
import CategorySection from "./categories";
import CollectionSection from "./collections";
import { AppProgressProvider } from "./components/AppProgress";
+import { ConfirmFormLeaveDialog } from "./components/ConfirmFormLeaveDialog";
import { DateProvider } from "./components/DateFormatter";
import { FormProvider } from "./components/Form";
import { LocaleProvider } from "./components/Locale";
@@ -100,8 +101,9 @@ render(
<MessageManager>
<AppProgressProvider>
<ShopProvider>
- <CssBaseline />
<WindowTitle title={i18n.t("Dashboard")} />
+ <ConfirmFormLeaveDialog />
+ <CssBaseline />
<AuthProvider>
{({
hasToken,