From 820eca4c27701657992273f5076fe1cf0245e21c Mon Sep 17 00:00:00 2001 From: Darin Alleman Date: Sun, 10 Jan 2021 22:05:38 -0500 Subject: [PATCH] Add unsaved changes alert for pack changes --- frontend/package.json | 1 + frontend/src/app/PackForm/PackForm.tsx | 39 +++++++++++++++++++------- frontend/src/index.tsx | 5 +++- frontend/src/styles/style.less | 14 +++++++++ 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/frontend/package.json b/frontend/package.json index 2082fdb..72350fd 100644 --- a/frontend/package.json +++ b/frontend/package.json @@ -63,6 +63,7 @@ "react-ga": "^2.5.6", "react-linkify": "^1.0.0-alpha", "react-router-dom": "^5.0.1", + "react-router-navigation-confirm": "^1.1.7", "react-select": "^3.1.0", "recharts": "^1.7.1", "resolve": "1.10.0", diff --git a/frontend/src/app/PackForm/PackForm.tsx b/frontend/src/app/PackForm/PackForm.tsx index ddd5b44..626ab93 100644 --- a/frontend/src/app/PackForm/PackForm.tsx +++ b/frontend/src/app/PackForm/PackForm.tsx @@ -20,16 +20,18 @@ import PackItems from "app/components/PackItems"; import { alertError, alertSuccess } from "app/components/Notifications"; import Loading from "app/components/Loading"; import { useSidebar } from "app/components/Sidebar/Context"; +import { NavigationConfirmModal } from 'react-router-navigation-confirm'; -import { PageTitle, Controls, Box, Grid } from "styles/common"; +import { PageTitle, Controls, Box, Grid, PageDescription } from "styles/common"; const PackForm: React.FC = ({ history, packId, getPack, exportItems, getItems, createPack, updatePack, user }) => { const [loading, setLoading] = React.useState(true); const [inventory, setInventory] = React.useState([]); const [packItems, setPackItems] = React.useState([]); const [packData, setPackData] = React.useState(null); + const [hasPendingChanges, setHasPendingChanges] = React.useState(false); const { dispatch } = useSidebar(); - + React.useEffect(() => { setLoading(true); async function fetchData(id: number) { @@ -64,11 +66,13 @@ const PackForm: React.FC = ({ history, packId, getPack, exp const addItem = (item: Item) => { const items = Object.assign([], [...packItems, { ...item, packItem: { notes: '', quantity: 1, worn: false } }]); + setHasPendingChanges(true); setPackItems(items); }; const removeItem = (itemId: number) => { const items = packItems.filter(i => i.id !== itemId); + setHasPendingChanges(true); setPackItems(items); }; @@ -76,6 +80,7 @@ const PackForm: React.FC = ({ history, packId, getPack, exp const items: PackItem[] = Object.assign([], packItems); const idx = items.findIndex(item => item.id === itemId); items[idx].packItem[field] = value; + setHasPendingChanges(true); setPackItems(items); }; @@ -135,7 +140,10 @@ const PackForm: React.FC = ({ history, packId, getPack, exp id: packId }); updatePack(payload) - .then(() => alertSuccess({ message: 'Packing list saved' })) + .then(() => { + alertSuccess({ message: 'Packing list saved' }); + setHasPendingChanges(false); + }) .catch(() => alertError({ message: 'Error saving packing list' })); } }}> @@ -174,12 +182,12 @@ const PackForm: React.FC = ({ history, packId, getPack, exp error={wasSubmitted && !!errors.title} errorMsg={errors.title} value={values.title} - onChange={v => setFieldValue('title', v)}/> + onChange={v => {setFieldValue('title', v); setHasPendingChanges(true);}}/>