summaryrefslogtreecommitdiff
path: root/src/features/auth/GuestLogin.tsx
blob: cbab494776f88ea118bf4366557172c93132a327 (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
import { useNavigate } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { Box, Button, Container, TextField, Typography } from '@mui/material';
import { useForm } from 'react-hook-form';
import { setCredentials } from './authSlice';
import { useLoginMutation, LoginRequest } from '../../apiSlice';

function GuestLogin() {
  const dispatch = useDispatch();
  const navigate = useNavigate();
  const [login] = useLoginMutation();

  const { register, handleSubmit, formState: { errors } } = useForm<LoginRequest>({
    defaultValues: {
      firstName: '',
      lastName: ''
    }
  });

  const onSubmit = async (data: LoginRequest) => {
      try {
        dispatch(setCredentials(await login(data).unwrap()));
        navigate('/rsvp');
      } catch (e) {
        console.log(e);
      }
  };

  return (
    <Container component="form" maxWidth="xs" onSubmit={handleSubmit(onSubmit)}>
      <Box 
        sx={{ mt: 8, 
        display: "flex", 
        flexDirection: "column", 
        alignItems: "center" }}
      >
          <Typography variant="h6">
            Guest Login
          </Typography>
          <TextField
            label="First Name"
            variant="outlined"
            margin="normal"
            fullWidth
            error={!!errors.firstName}
            helperText={errors.firstName?.message}
            {...register("firstName", { required: "Please enter your first name" })}
          />
          <TextField
            label="Last Name"
            variant="outlined"
            margin="normal"
            fullWidth
            error={!!errors.lastName}
            helperText={errors.lastName?.message}
            {...register("lastName", { required: "Please enter your last name" })}
          />
          <Button 
            type="submit" 
            variant="contained" 
            fullWidth
            sx={{ mt: 2 }}
          >
            Log in
          </Button>
      </Box>
    </Container>
  );
}

export default GuestLogin;