From 6391abb6eed563a33be1e06d8ca97971260fbadb Mon Sep 17 00:00:00 2001 From: Piv94165 <106757110+Piv94165@users.noreply.github.com> Date: Thu, 11 Apr 2024 10:40:50 +0200 Subject: [PATCH] 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 --- .../editentry/AccumulateAllComponents.tsx | 4 +++ .../project/editentry/ListEntryChildren.tsx | 36 +++++++++++++++++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/taxonomy-editor-frontend/src/pages/project/editentry/AccumulateAllComponents.tsx b/taxonomy-editor-frontend/src/pages/project/editentry/AccumulateAllComponents.tsx index a1d0dbab..cf4b8500 100644 --- a/taxonomy-editor-frontend/src/pages/project/editentry/AccumulateAllComponents.tsx +++ b/taxonomy-editor-frontend/src/pages/project/editentry/AccumulateAllComponents.tsx @@ -180,7 +180,11 @@ const AccumulateAllComponents = ({ { +const ListEntryChildren = ({ + url, + urlPrefix, + updateChildren, + setUpdateNodeChildren, + previousUpdateChildren, + setPreviousUpdateChildren, + hasChanges, +}) => { const [relations, setRelations] = useState([]); const [newChild, setNewChild] = useState(""); const [newLanguageCode, setNewLanguageCode] = useState(""); @@ -46,6 +56,9 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => { // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error setUpdateNodeChildren(incomingData.map((el) => el?.[0])); + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error + setPreviousUpdateChildren(incomingData.map((el) => el?.[0])); const arrayData: Relations[] = []; // eslint-disable-next-line @typescript-eslint/ban-ts-comment // @ts-expect-error @@ -54,7 +67,7 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => { ); setRelations(arrayData); } - }, [incomingData, setUpdateNodeChildren]); + }, [incomingData, setPreviousUpdateChildren, setUpdateNodeChildren]); // Helper functions for Dialog component const handleCloseDialog = () => { @@ -122,6 +135,17 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => { + {/* Renders warning message to save changes to be able to click on a child node */} + {(!equal(updateChildren, previousUpdateChildren) || hasChanges) && ( + + {!equal(updateChildren, previousUpdateChildren) && + "You've just created a new child. To navigate to it, please ensure your changes are saved first."} + {!equal(updateChildren, previousUpdateChildren) &&
} + {hasChanges && + "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."} +
+ )} + {/* Renders parents or children of the node */} {relations.map((relationObject) => ( @@ -131,7 +155,13 @@ const ListEntryChildren = ({ url, urlPrefix, setUpdateNodeChildren }) => { alignItems="center" >