diff --git a/frontend/package.json b/frontend/package.json index 35febb9..4524f38 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 f3d60e9..80e7280 100644 --- a/frontend/src/app/PackForm/PackForm.tsx +++ b/frontend/src/app/PackForm/PackForm.tsx @@ -19,16 +19,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) { @@ -63,11 +65,13 @@ const PackForm: React.FC = ({ history, packId, getPack, exp const addItem = (item: Item) => { const items = Object.assign([], [...packItems, { ...item, packItem: { notes: item.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); }; @@ -75,6 +79,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); }; @@ -134,7 +139,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' })); } }}> @@ -173,15 +181,15 @@ const PackForm: React.FC = ({ history, packId, getPack, exp error={wasSubmitted && !!errors.title} errorMsg={errors.title} value={values.title} - allowedLength={PackConstants.title} - onChange={v => setFieldValue('title', v)}/> + onChange={v => {setFieldValue('title', v); setHasPendingChanges(true);}} + allowedLength={PackConstants.title}/>