Skip to content

Commit 6391abb

Browse files
Piv94165alice.juan
and
alice.juan
authored
fix(frontend): Prevent users from navigating to child with unsaved changes (#468)
Currently, users can navigate to a child node while editing a parent node without saving changes, resulting in a potential 404 error if the child node is newly created. Additionally, clicking on a child node before saving changes can lead to the loss of unsaved modifications. To prevent this, I have disabled the ability to navigate to a child node if this node has been newly created without saving. Furthermore, I have implemented a warning message to inform users about why they cannot proceed to the child node : "You've just created a new child. To navigate to it, please ensure your changes are saved first." There is another warning massage when whatever changes have not been saved : "Changes are pending and have not been saved. Please save your changes before navigating to any child node. If you prefer not to save your pending changes but wish to avoid losing them, you can navigate to a child node in a new window." --------- Co-authored-by: alice.juan <[email protected]>
1 parent b2a7b43 commit 6391abb

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

taxonomy-editor-frontend/src/pages/project/editentry/AccumulateAllComponents.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,11 @@ const AccumulateAllComponents = ({
180180
<ListEntryChildren
181181
url={url + "/children"}
182182
urlPrefix={urlPrefix}
183+
updateChildren={updateChildren}
183184
setUpdateNodeChildren={setUpdateChildren}
185+
previousUpdateChildren={previousUpdateChildren}
186+
setPreviousUpdateChildren={setPreviousUpdateChildren}
187+
hasChanges={hasChanges}
184188
/>
185189
<ListTranslations
186190
originalNodeObject={originalNodeObject}

taxonomy-editor-frontend/src/pages/project/editentry/ListEntryChildren.tsx

+33-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
Button,
77
IconButton,
88
Box,
9+
Alert,
910
} from "@mui/material";
1011
import { Link } from "react-router-dom";
1112
import { useEffect, useState } from "react";
@@ -20,13 +21,22 @@ import CircularProgress from "@mui/material/CircularProgress";
2021
import ISO6391 from "iso-639-1";
2122
import { ENTER_KEYCODE } from "@/constants";
2223
import { greyHexCode } from "@/constants";
24+
import equal from "fast-deep-equal";
2325

2426
interface Relations {
2527
index: string;
2628
child: string;
2729
}
2830

29-
const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => {
31+
const ListEntryChildren = ({
32+
url,
33+
urlPrefix,
34+
updateChildren,
35+
setUpdateNodeChildren,
36+
previousUpdateChildren,
37+
setPreviousUpdateChildren,
38+
hasChanges,
39+
}) => {
3040
const [relations, setRelations] = useState<Relations[]>([]);
3141
const [newChild, setNewChild] = useState("");
3242
const [newLanguageCode, setNewLanguageCode] = useState("");
@@ -46,6 +56,9 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => {
4656
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
4757
// @ts-expect-error
4858
setUpdateNodeChildren(incomingData.map((el) => el?.[0]));
59+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
60+
// @ts-expect-error
61+
setPreviousUpdateChildren(incomingData.map((el) => el?.[0]));
4962
const arrayData: Relations[] = [];
5063
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5164
// @ts-expect-error
@@ -54,7 +67,7 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => {
5467
);
5568
setRelations(arrayData);
5669
}
57-
}, [incomingData, setUpdateNodeChildren]);
70+
}, [incomingData, setPreviousUpdateChildren, setUpdateNodeChildren]);
5871

5972
// Helper functions for Dialog component
6073
const handleCloseDialog = () => {
@@ -122,6 +135,17 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => {
122135
</IconButton>
123136
</Stack>
124137

138+
{/* Renders warning message to save changes to be able to click on a child node */}
139+
{(!equal(updateChildren, previousUpdateChildren) || hasChanges) && (
140+
<Alert severity="warning" sx={{ mb: 1, ml: 4, width: "fit-content" }}>
141+
{!equal(updateChildren, previousUpdateChildren) &&
142+
"You've just created a new child. To navigate to it, please ensure your changes are saved first."}
143+
{!equal(updateChildren, previousUpdateChildren) && <br />}
144+
{hasChanges &&
145+
"Changes are pending and have not been saved. Please save your changes before navigating to any child node. If you prefer not to save your pending changes but wish to avoid losing them, you can navigate to a child node in a new window."}
146+
</Alert>
147+
)}
148+
125149
{/* Renders parents or children of the node */}
126150
<Stack direction="row" flexWrap="wrap">
127151
{relations.map((relationObject) => (
@@ -131,7 +155,13 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => {
131155
alignItems="center"
132156
>
133157
<Link
134-
to={`${urlPrefix}/entry/${relationObject["child"]}`}
158+
to={
159+
// Is this a newly created child node that hasn't been saved yet?
160+
updateChildren.includes(relationObject["child"]) &&
161+
!previousUpdateChildren.includes(relationObject["child"])
162+
? "#"
163+
: `${urlPrefix}/entry/${relationObject["child"]}`
164+
}
135165
style={{ color: "#0064c8", display: "inline-block" }}
136166
>
137167
<Typography sx={{ ml: 8 }} variant="h6">

0 commit comments

Comments
 (0)