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

Setup Snackbar Provider #24

Merged
merged 6 commits into from
Aug 6, 2024
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
2 changes: 2 additions & 0 deletions submit-web/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { OidcConfig } from "@/utils/config";
import { theme } from "@/styles/theme";
import RouterProviderWithAuthContext from "@/router";
import ModalProvider from "./components/Shared/Modals/ModalProvider";
import SnackBarProvider from "./components/Shared/Popups/SnackBarProvider";
const queryClient = new QueryClient();

function App() {
Expand All @@ -16,6 +17,7 @@ function App() {
<ThemeProvider theme={theme}>
<AuthProvider {...OidcConfig}>
<ModalProvider />
<SnackBarProvider />
<RouterProviderWithAuthContext />
</AuthProvider>
</ThemeProvider>
Expand Down
40 changes: 40 additions & 0 deletions submit-web/src/components/Shared/Modals/ConfirmationModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import {
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
Button,
} from "@mui/material";
import { useModal } from "./modalStore";

type ConfirmationModalProps = {
title: string;
description: string;
onConfirm: () => void;
};

const ConfirmationModal: React.FC<ConfirmationModalProps> = ({
title,
description,
onConfirm,
}) => {
const { setClose } = useModal();

Check warning on line 21 in submit-web/src/components/Shared/Modals/ConfirmationModal.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/components/Shared/Modals/ConfirmationModal.tsx#L20-L21

Added lines #L20 - L21 were not covered by tests
return (
<>
<DialogTitle>{title}</DialogTitle>
<DialogContent>
<DialogContentText>{description}</DialogContentText>
</DialogContent>
<DialogActions sx={{ padding: "1rem" }}>
<Button onClick={setClose} color="primary">
Cancel
</Button>
<Button variant="contained" onClick={onConfirm} color="error">
Confirm
</Button>
</DialogActions>
</>
);
};

export default ConfirmationModal;
26 changes: 0 additions & 26 deletions submit-web/src/components/Shared/Popups/ConfirmationDialog.tsx

This file was deleted.

44 changes: 0 additions & 44 deletions submit-web/src/components/Shared/Popups/SnackBarMessage.tsx

This file was deleted.

27 changes: 27 additions & 0 deletions submit-web/src/components/Shared/Popups/SnackBarProvider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { Snackbar, Alert } from "@mui/material";
import { useSnackbar } from "./snackbarStore";

const SnackBarProvider: React.FC = () => {
const { isOpen, setClose, severity, message } = useSnackbar();

return (
<Snackbar
open={isOpen}
autoHideDuration={3000}
onClose={setClose}
anchorOrigin={{ vertical: "bottom", horizontal: "left" }}
>
<Alert
onClose={setClose}
severity={severity}
variant="filled"
sx={{ width: "100%" }}
>
{message}
</Alert>
</Snackbar>
);
};

export default SnackBarProvider;
31 changes: 31 additions & 0 deletions submit-web/src/components/Shared/Popups/snackbarStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { create } from "zustand";

interface SnackbarState {
isOpen: boolean;
severity: "success" | "error" | "warning" | "info";
message: string;
setOpen: (
message: string,
severity?: "success" | "error" | "warning" | "info",
) => void;
setClose: () => void;
}

export const useSnackbar = create<SnackbarState>((set) => ({
isOpen: false,
severity: "success", // default severity
message: "",
setOpen: (message, severity = "success") =>
set({ isOpen: true, message, severity }),
setClose: () => set({ isOpen: false }),

Check warning on line 20 in submit-web/src/components/Shared/Popups/snackbarStore.ts

View check run for this annotation

Codecov / codecov/patch

submit-web/src/components/Shared/Popups/snackbarStore.ts#L19-L20

Added lines #L19 - L20 were not covered by tests
}));

// Helper function to notify with different severities
export const notify = {
Copy link
Collaborator

Choose a reason for hiding this comment

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

beautiful

success: (message: string) =>
useSnackbar.getState().setOpen(message, "success"),
error: (message: string) => useSnackbar.getState().setOpen(message, "error"),

Check warning on line 27 in submit-web/src/components/Shared/Popups/snackbarStore.ts

View check run for this annotation

Codecov / codecov/patch

submit-web/src/components/Shared/Popups/snackbarStore.ts#L26-L27

Added lines #L26 - L27 were not covered by tests
warning: (message: string) =>
useSnackbar.getState().setOpen(message, "warning"),
info: (message: string) => useSnackbar.getState().setOpen(message, "info"),

Check warning on line 30 in submit-web/src/components/Shared/Popups/snackbarStore.ts

View check run for this annotation

Codecov / codecov/patch

submit-web/src/components/Shared/Popups/snackbarStore.ts#L29-L30

Added lines #L29 - L30 were not covered by tests
};
55 changes: 17 additions & 38 deletions submit-web/src/routes/_authenticated/users/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@
import { useDeleteUser, useUsersData } from "@/hooks/useUsers";
import { useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
import ConfirmationDialog from "@/components/Shared/Popups/ConfirmationDialog";
import CustomSnackbar, {
SnackBarMessageProps,
} from "@/components/Shared/Popups/SnackBarMessage";
import UserModal from "@/components/App/Users/UserModal";
import { useModal } from "@/components/Shared/Modals/modalStore";
import { notify } from "@/components/Shared/Popups/snackbarStore";
import ConfirmationModal from "@/components/Shared/Modals/ConfirmationModal";

export const Route = createFileRoute("/_authenticated/users/")({
component: UsersPage,
Expand All @@ -33,19 +31,15 @@
const queryClient = useQueryClient();
const { setOpen } = useModal();
const [selectedUser, setSelectedUser] = useState<User | null>(null);
const [isConfirmationOpen, setIsConfirmationOpen] = useState(false);
const [userIdToDelete, setUserIdToDelete] = useState<number | null>(null);
const [snackbarConfig, setSnackbarConfig] =
useState<SnackBarMessageProps | null>(null);

const { isLoading, data, isError, error } = useUsersData();

const handleOnSubmit = () => {
queryClient.invalidateQueries({ queryKey: ["users"] });
if (selectedUser) {
setSnackbarConfig({ message: "User updated successfully!" });
notify.success("User updated successfully!");

Check warning on line 40 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L40

Added line #L40 was not covered by tests
} else {
setSnackbarConfig({ message: "User added successfully!" });
notify.success("User created successfully!");

Check warning on line 42 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L42

Added line #L42 was not covered by tests
}
};

Expand All @@ -57,37 +51,35 @@
/** Delete user START */

const onDeleteSuccess = () => {
setSnackbarConfig({ message: "User deleted successfully!" });
notify.success("User deleted successfully!");

Check warning on line 54 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L54

Added line #L54 was not covered by tests
queryClient.invalidateQueries({
queryKey: ["users"],
});
};

const onDeleteError = (error: AxiosError) => {
setSnackbarConfig({
message: `User deletion failed! ${error.message}`,
severity: "error",
});
notify.error(`User deletion failed! ${error.message}`);

Check warning on line 61 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L61

Added line #L61 was not covered by tests
};

const { mutate: deleteUser } = useDeleteUser(onDeleteSuccess, onDeleteError);

const handleDeleteUser = () => {
setSnackbarConfig({ message: "" });
notify.success("User deleted successfully!");

Check warning on line 67 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L67

Added line #L67 was not covered by tests
if (userIdToDelete !== null) {
deleteUser(userIdToDelete);
setIsConfirmationOpen(false);
setUserIdToDelete(null);

Check warning on line 70 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L70

Added line #L70 was not covered by tests
}
};

const handleOpenConfirmationDialog = (userId: number) => {
const handleOpenConfirmationModal = (userId: number) => {

Check warning on line 74 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L74

Added line #L74 was not covered by tests
setUserIdToDelete(userId);
setIsConfirmationOpen(true);
};

const handleCloseConfirmationDialog = () => {
setIsConfirmationOpen(false);
setUserIdToDelete(null);
setOpen(
<ConfirmationModal
title="Delete User"
description="Are you sure you want to delete this user?"
onConfirm={handleDeleteUser}
/>
);
};

/** Delete user END */
Expand Down Expand Up @@ -155,7 +147,7 @@
<IconButton
aria-label="delete"
color="error"
onClick={() => handleOpenConfirmationDialog(row.id)}
onClick={() => handleOpenConfirmationModal(row.id)}

Check warning on line 150 in submit-web/src/routes/_authenticated/users/index.tsx

View check run for this annotation

Codecov / codecov/patch

submit-web/src/routes/_authenticated/users/index.tsx#L150

Added line #L150 was not covered by tests
>
<Delete />
</IconButton>
Expand All @@ -165,19 +157,6 @@
</TableBody>
</Table>
</TableContainer>
<ConfirmationDialog
isOpen={isConfirmationOpen}
title="Delete User"
description="Are you sure you want to delete this user?"
onConfirm={handleDeleteUser}
onCancel={handleCloseConfirmationDialog}
/>
{snackbarConfig?.message && (
<CustomSnackbar
message={snackbarConfig.message}
severity={snackbarConfig.severity}
/>
)}
</>
);
}
Loading