summaryrefslogtreecommitdiff
path: root/client/src/components/GuestLogin.tsx
blob: 0d3e8b161b652676744cd3bcd903d4b0d70d440e (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
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 { setGuest } from '../slices/auth/guestSlice';
import { useLoginGuestMutation } from '../slices/api/guestSlice';
import Status from './Status';
import { open } from '../slices/snackbarSlice';
import { isFetchBaseQueryError } from '../error';
import type { Data } from '../error';
import type { Name } from '../models';

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

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

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

  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,
        }}
      >
        <Typography variant="h6">Guest Login</Typography>
        <TextField
          label="First Name"
          variant="outlined"
          margin="normal"
          fullWidth
          error={!!errors.firstName}
          helperText={errors.firstName?.message}
          required
          {...register('firstName', { required: 'This field is required' })}
        />
        <TextField
          label="Last Name"
          variant="outlined"
          margin="normal"
          fullWidth
          error={!!errors.lastName}
          helperText={errors.lastName?.message}
          required
          {...register('lastName', { required: 'This field is required' })}
        />
        <Button type="submit" variant="contained" fullWidth sx={{ mt: 2 }}>
          Log in
        </Button>
        <Status />
      </Paper>
    </form>
  );
}

export default GuestLogin;