summaryrefslogtreecommitdiff
path: root/client/src/components/AdminLogin.tsx
blob: f1f676f3c355f9e2742b8cb8cbde6412efe4f1ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import {
  Button,
  Paper,
  TextField,
  Typography,
  IconButton,
  InputAdornment,
} from '@mui/material';
import { Visibility, VisibilityOff } from '@mui/icons-material';
import { useForm } from 'react-hook-form';
import { useAppDispatch } from '../hooks';
import { setAdmin } from '../slices/auth/adminSlice';
import { useLoginAdminMutation } from '../slices/api/adminSlice';
import { showSnackbar } from '../slices/uiSlice';
import { isFetchBaseQueryError } from '../error';
import type { Credentials } from '../models';
import type { Data } from '../error';

function GuestLogin() {
  const dispatch = useAppDispatch();
  const navigate = useNavigate();
  const [login] = useLoginAdminMutation();
  const [showPassword, setShowPassword] = useState(false);

  const {
    register,
    handleSubmit,
    formState: { errors },
  } = useForm<Credentials>({
    defaultValues: {
      username: '',
      password: '',
    },
  });

  const onSubmit = async (data: Credentials) => {
    try {
      dispatch(setAdmin(await login(data).unwrap()));
      navigate('/dashboard');
    } catch (error) {
      if (isFetchBaseQueryError(error)) {
        dispatch(
          showSnackbar({
            message: (error.data as Data).message,
            severity: 'error',
          })
        );
      } else {
        dispatch(
          showSnackbar({
            message: 'No response',
            severity: 'error',
          })
        );
      }
    }
  };

  const handleClickShowPassword = () => setShowPassword((show) => !show);

  return (
    <form
      style={{
        height: '100%',
        width: '100%',
        display: 'flex',
        justifyContent: 'center',
        alignItems: 'start',
      }}
      noValidate
      onSubmit={handleSubmit(onSubmit)}
    >
      <Paper
        elevation={3}
        sx={{
          '&:hover': { boxShadow: 5 },
          width: { xs: '90%', md: 400 },
          display: 'flex',
          flexDirection: 'column',
          justifyContent: 'center',
          alignItems: 'center',
          mt: 16,
          p: 2,
          borderRadius: 2,
        }}
      >
        <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"
          type={showPassword ? 'text' : 'password'}
          margin="normal"
          fullWidth
          InputProps={{
            endAdornment: (
              <InputAdornment position="end">
                <IconButton onClick={handleClickShowPassword}>
                  {showPassword ? <VisibilityOff /> : <Visibility />}
                </IconButton>
              </InputAdornment>
            ),
          }}
          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;