From a54a93b724a6104213b17271fc298e37adedc1c5 Mon Sep 17 00:00:00 2001
From: Michael Hunteman <michael@huntm.net>
Date: Fri, 20 Sep 2024 11:19:34 -0700
Subject: Use redux for updating snackbar

---
 client/src/components/Status.tsx | 36 +++++++++++++++++++++++-------------
 1 file changed, 23 insertions(+), 13 deletions(-)

(limited to 'client/src/components/Status.tsx')

diff --git a/client/src/components/Status.tsx b/client/src/components/Status.tsx
index 4bb60ff..bdd7fb2 100644
--- a/client/src/components/Status.tsx
+++ b/client/src/components/Status.tsx
@@ -1,18 +1,28 @@
 import React from 'react';
-import { Alert } from '@mui/material';
-import { isFetchBaseQueryError } from '../error';
-import type { StatusProps } from '../models';
-import type { Data } from '../error';
+import { useDispatch, useSelector } from 'react-redux';
+import { Alert, Snackbar } from '@mui/material';
+import { close, selectSnackbarState } from '../slices/snackbarSlice';
 
-const Status = ({ error, setOpen, type }: StatusProps) => {
-  return isFetchBaseQueryError(error) ? (
-    <Alert severity="error" onClose={() => setOpen(false)}>
-      {(error.data as Data).message}
-    </Alert>
-  ) : (
-    <Alert severity="error" onClose={() => setOpen(false)}>
-      {`${type} login failed`}
-    </Alert>
+const Status = () => {
+  const dispatch = useDispatch();
+  const snackbarState = useSelector(selectSnackbarState);
+
+  const closeSnackbar = () => {
+    dispatch(close());
+  };
+
+  return (
+    <Snackbar
+      open={snackbarState.open}
+      onClose={closeSnackbar}
+      autoHideDuration={5000}
+    >
+      <div>
+        <Alert severity={snackbarState.severity} onClose={closeSnackbar}>
+          {snackbarState.message}
+        </Alert>
+      </div>
+    </Snackbar>
   );
 };
 
-- 
cgit v1.2.3

