Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: new UX for submitting changes #224

Merged
merged 8 commits into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions taxonomy-editor-frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions taxonomy-editor-frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@types/node": "^18.11.18",
"@types/react": "^18.0.27",
"@types/react-dom": "^18.0.10",
"fast-deep-equal": "^3.1.3",
"iso-639-1": "^2.1.15",
"react": "^18.2.0",
"react-dom": "^18.2.0",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Alert, Box, Button, Snackbar, Typography } from "@mui/material";
import { Alert, Box, Snackbar, Typography, Button } from "@mui/material";
import SaveIcon from "@mui/icons-material/Save";
import { useEffect, useState } from "react";
import useFetch from "../../components/useFetch";
import ListEntryParents from "./ListEntryParents";
import ListEntryChildren from "./ListEntryChildren";
import ListTranslations from "./ListTranslations";
import ListAllEntryProperties from "./ListAllEntryProperties";
import ListAllNonEntryInfo from "./ListAllNonEntryInfo";
import equal from "fast-deep-equal";
import { createURL, getNodeType } from "../../utils";

/**
Expand All @@ -29,8 +31,19 @@ const AccumulateAllComponents = ({ id, taxonomyName, branchName }) => {
errorMessage,
} = useFetch(url);
const [nodeObject, setNodeObject] = useState(null); // Storing updates to node
const [originalNodeObject, setOriginalNodeObject] = useState(null); // For tracking changes
const [updateChildren, setUpdateChildren] = useState([]); // Storing updates of children in node
const [open, setOpen] = useState(false); // Used for Dialog component
const [hasChanges, sethasChanges] = useState(false); // Used for displaying Fab

// Tracking changes
useEffect(() => {
if (nodeObject && originalNodeObject) {
const hasChanges =
!equal(nodeObject, originalNodeObject) || updateChildren.length !== 0;
sethasChanges(hasChanges);
}
}, [nodeObject, originalNodeObject, updateChildren]);

// Setting state of node after fetch
useEffect(() => {
Expand All @@ -50,6 +63,7 @@ const AccumulateAllComponents = ({ id, taxonomyName, branchName }) => {
});
}
setNodeObject(duplicateNode);
setOriginalNodeObject(JSON.parse(JSON.stringify(duplicateNode))); // Deep copy
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need the parse and stringify?

}, [node]);

// Displaying error messages if any
Expand Down Expand Up @@ -103,6 +117,7 @@ const AccumulateAllComponents = ({ id, taxonomyName, branchName }) => {
)
.then(() => {
setOpen(true);
sethasChanges(false);
})
.catch(() => {});
};
Expand Down Expand Up @@ -130,6 +145,32 @@ const AccumulateAllComponents = ({ id, taxonomyName, branchName }) => {
nodeObject={nodeObject}
setNodeObject={setNodeObject}
/>
{/* Sticky button for submitting edits */}
{hasChanges && (
<div
style={{
backgroundColor: "white",
position: "sticky",
bottom: 0,
zIndex: 1,
}}
>
<Button
onClick={handleSubmit}
variant="contained"
sx={{
minHeight: "3rem",
borderRadius: "2rem",
marginTop: 2,
marginBottom: 2,
marginLeft: 4,
}}
>
<SaveIcon sx={{ mr: 1 }} />
Save Changes
</Button>
</div>
)}
</>
)}
</Box>
Expand All @@ -142,14 +183,6 @@ const AccumulateAllComponents = ({ id, taxonomyName, branchName }) => {
/>
</>
)}
{/* Button for submitting edits */}
<Button
variant="contained"
onClick={handleSubmit}
sx={{ ml: 4, mt: 2, width: 130, mb: 2 }}
>
Submit
</Button>
{/* Snackbar for acknowledgment of update */}
<Snackbar
anchorOrigin={{ vertical: "top", horizontal: "right" }}
Expand Down