Skip to content

Commit

Permalink
Pull state and snackbar up a level; pass to status update component too
Browse files Browse the repository at this point in the history
  • Loading branch information
mddilley committed Jan 30, 2025
1 parent 6e97cf5 commit aac358c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 39 deletions.
40 changes: 2 additions & 38 deletions moped-editor/src/views/dashboard/DashboardTimelineModal.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useState } from "react";
import React, { useState } from "react";
import {
Box,
CardContent,
Expand All @@ -15,7 +15,6 @@ import { useQuery } from "@apollo/client";
import ApolloErrorHandler from "src/components/ApolloErrorHandler";
import ProjectMilestones from "../projects/projectView/ProjectMilestones";
import ProjectPhases from "../projects/projectView/ProjectPhases";
import FeedbackSnackbar from "src/components/FeedbackSnackbar";

const useStyles = makeStyles((theme) => ({
clickableDiv: {
Expand All @@ -28,6 +27,7 @@ const DashboardTimelineModal = ({
projectId,
projectName,
dashboardRefetch,
handleSnackbar,
children,
}) => {
const classes = useStyles();
Expand All @@ -50,38 +50,6 @@ const DashboardTimelineModal = ({
dashboardRefetch();
};

/* Snackbar state and handler for phase and milestone update feedback */
const [snackbarState, setSnackbarState] = useState(false);

/**
* Wrapper around snackbar state setter
* @param {boolean} open - The new state of open
* @param {String} message - The message for the snackbar
* @param {String} severity - The severity color of the snackbar
* @param {Object} error - The error to be displayed and logged
*/
const handleSnackbar = useCallback(
(open, message, severity, error) => {
// if there is an error, render error message,
// otherwise, render success message
if (error) {
setSnackbarState({
open: open,
message: `${message}. Refresh the page to try again.`,
severity: severity,
});
console.error(error);
} else {
setSnackbarState({
open: open,
message: message,
severity: severity,
});
}
},
[setSnackbarState]
);

return (
<>
<div
Expand Down Expand Up @@ -140,10 +108,6 @@ const DashboardTimelineModal = ({
</ApolloErrorHandler>
</DialogContent>
</Dialog>
<FeedbackSnackbar
snackbarState={snackbarState}
handleSnackbar={handleSnackbar}
/>
</>
);
};
Expand Down
41 changes: 40 additions & 1 deletion moped-editor/src/views/dashboard/DashboardView.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useCallback, useState } from "react";
import { useQuery } from "@apollo/client";
import { format } from "date-fns";

Expand All @@ -24,6 +24,7 @@ import DashboardStatusModal from "./DashboardStatusModal";
import DashboardTimelineModal from "./DashboardTimelineModal";
import ProjectStatusBadge from "../projects/projectView/ProjectStatusBadge";
import MilestoneProgressMeter from "./MilestoneProgressMeter";
import FeedbackSnackbar from "src/components/FeedbackSnackbar";

import typography from "../../theme/typography";

Expand Down Expand Up @@ -111,6 +112,38 @@ const DashboardView = () => {
fetchPolicy: "no-cache",
});

/* Snackbar state and handler for phase and milestone update feedback */
const [snackbarState, setSnackbarState] = useState(false);

/**
* Wrapper around snackbar state setter
* @param {boolean} open - The new state of open
* @param {String} message - The message for the snackbar
* @param {String} severity - The severity color of the snackbar
* @param {Object} error - The error to be displayed and logged
*/
const handleSnackbar = useCallback(
(open, message, severity, error) => {
// if there is an error, render error message,
// otherwise, render success message
if (error) {
setSnackbarState({
open: open,
message: `${message}. Refresh the page to try again.`,
severity: severity,
});
console.error(error);
} else {
setSnackbarState({
open: open,
message: message,
severity: severity,
});
}
},
[setSnackbarState]
);

if (error) {
console.log(error);
}
Expand Down Expand Up @@ -204,6 +237,7 @@ const DashboardView = () => {
projectId={entry.project_id}
projectName={entry.project.project_name_full}
dashboardRefetch={refetch}
handleSnackbar={handleSnackbar}
>
<ProjectStatusBadge
phaseName={entry.phase_name}
Expand All @@ -227,6 +261,7 @@ const DashboardView = () => {
modalParent="dashboard"
statusUpdate={entry.status_update}
queryRefetch={refetch}
handleSnackbar={handleSnackbar}
classes={classes}
>
{parse(String(entry.status_update))}
Expand Down Expand Up @@ -333,6 +368,10 @@ const DashboardView = () => {
</Grid>
</Card>
</Container>
<FeedbackSnackbar
snackbarState={snackbarState}
handleSnackbar={handleSnackbar}
/>
</Page>
</ActivityMetrics>
);
Expand Down

0 comments on commit aac358c

Please sign in to comment.