From a54a93b724a6104213b17271fc298e37adedc1c5 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Fri, 20 Sep 2024 11:19:34 -0700 Subject: Use redux for updating snackbar --- client/src/components/AdminLogin.tsx | 41 +++++++++++++++++++++--------------- 1 file changed, 24 insertions(+), 17 deletions(-) (limited to 'client/src/components/AdminLogin.tsx') diff --git a/client/src/components/AdminLogin.tsx b/client/src/components/AdminLogin.tsx index 1881b6b..25f2063 100644 --- a/client/src/components/AdminLogin.tsx +++ b/client/src/components/AdminLogin.tsx @@ -1,18 +1,20 @@ -import React, { useState } from 'react'; +import React from 'react'; import { useNavigate } from 'react-router-dom'; import { useDispatch } from 'react-redux'; -import { Button, Paper, Snackbar, TextField, Typography } from '@mui/material'; +import { Button, Paper, TextField, Typography } from '@mui/material'; import { useForm } from 'react-hook-form'; import { setAdmin } from '../slices/auth/adminSlice'; import { useLoginAdminMutation } from '../slices/api/adminSlice'; -import type { Credentials, StatusProps } from '../models'; import Status from './Status'; +import { open } from '../slices/snackbarSlice'; +import { isFetchBaseQueryError } from '../error'; +import type { Credentials } from '../models'; +import type { Data } from '../error'; function GuestLogin() { const dispatch = useDispatch(); const navigate = useNavigate(); - const [login, { isLoading, error }] = useLoginAdminMutation(); - const [open, setOpen] = useState(false); + const [login] = useLoginAdminMutation(); const { register, @@ -29,8 +31,22 @@ function GuestLogin() { try { dispatch(setAdmin(await login(data).unwrap())); navigate('/dashboard'); - } catch (e) { - setOpen(true); + } catch (error) { + if (isFetchBaseQueryError(error)) { + dispatch( + open({ + message: (error.data as Data).message, + severity: 'error', + }) + ); + } else { + dispatch( + open({ + message: 'No response', + severity: 'error', + }) + ); + } } }; @@ -57,7 +73,6 @@ function GuestLogin() { alignItems: 'center', mt: 16, p: 2, - borderRadius: '8px', }} > Admin Login @@ -85,15 +100,7 @@ function GuestLogin() { Log in - setOpen(false)} - autoHideDuration={5000} - > -
- -
-
+ ); } -- cgit v1.2.3