summaryrefslogtreecommitdiff
path: root/client/src/components/AdminLogin.tsx
diff options
context:
space:
mode:
authorMichael Hunteman <michael@huntm.net>2024-08-25 12:44:32 -0700
committerMichael Hunteman <michael@huntm.net>2024-08-25 12:44:32 -0700
commit096a08708e2310becba56a237ef63b5cf6e3c4c4 (patch)
tree2924f9aecdcf035599558552cfdb20c2cc18f7d1 /client/src/components/AdminLogin.tsx
parent6aee47e76d7e25206b3778aeebcc341d7b705035 (diff)
Add admin dashboard
Diffstat (limited to 'client/src/components/AdminLogin.tsx')
-rw-r--r--client/src/components/AdminLogin.tsx90
1 files changed, 90 insertions, 0 deletions
diff --git a/client/src/components/AdminLogin.tsx b/client/src/components/AdminLogin.tsx
new file mode 100644
index 0000000..d9c1260
--- /dev/null
+++ b/client/src/components/AdminLogin.tsx
@@ -0,0 +1,90 @@
+import React from 'react';
+import { useNavigate } from 'react-router-dom';
+import { useDispatch } from 'react-redux';
+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 { AdminLoginRequest } from '../slices/api/adminSlice';
+
+function GuestLogin() {
+ const dispatch = useDispatch();
+ const navigate = useNavigate();
+ const [login] = useLoginAdminMutation();
+
+ const {
+ register,
+ handleSubmit,
+ formState: { errors },
+ } = useForm<AdminLoginRequest>({
+ defaultValues: {
+ username: '',
+ password: '',
+ },
+ });
+
+ const onSubmit = async (data: AdminLoginRequest) => {
+ try {
+ dispatch(setAdmin(await login(data).unwrap()));
+ navigate('/dashboard');
+ } catch (e) {
+ console.log(e);
+ }
+ };
+
+ return (
+ <form
+ style={{
+ height: '100%',
+ width: '100%',
+ display: 'flex',
+ justifyContent: 'center',
+ alignItems: 'start',
+ }}
+ noValidate
+ onSubmit={handleSubmit(onSubmit)}
+ >
+ <Paper
+ elevation={3}
+ sx={{
+ '&:hover': { boxShadow: 8 },
+ width: { xs: '90%', md: 400 },
+ display: 'flex',
+ flexDirection: 'column',
+ justifyContent: 'center',
+ alignItems: 'center',
+ mt: 16,
+ p: 2,
+ borderRadius: '8px',
+ }}
+ >
+ <Typography variant="h6">Admin Login</Typography>
+ <TextField
+ label="Username"
+ variant="outlined"
+ margin="normal"
+ fullWidth
+ error={!!errors.username}
+ helperText={errors.username?.message}
+ required
+ {...register('username', { required: 'This field is required' })}
+ />
+ <TextField
+ label="Password"
+ variant="outlined"
+ margin="normal"
+ fullWidth
+ error={!!errors.password}
+ helperText={errors.password?.message}
+ required
+ {...register('password', { required: 'This field is required' })}
+ />
+ <Button type="submit" variant="contained" fullWidth sx={{ mt: 2 }}>
+ Log in
+ </Button>
+ </Paper>
+ </form>
+ );
+}
+
+export default GuestLogin;