summaryrefslogtreecommitdiff
path: root/src/features/auth/GuestLogin.tsx
blob: fb305f7876a8e8f7dd28cd9b01c18425427b12c6 (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
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { useDispatch } from 'react-redux';
import { Button, Grid, Paper, TextField, Typography } from '@mui/material';

import { setCredentials } from './authSlice';
import { useLoginMutation } from '../../apiSlice';
import type { LoginRequest } from './authSlice';

function GuestLogin() {
  const dispatch = useDispatch();
  const navigate = useNavigate();

  // TODO: use react-hook-form
  const [formState, setFormState] = useState<LoginRequest>({
    firstName: '',
    lastName: ''
  });

  const [login] = useLoginMutation();

  const handleChange = ({
    target: { name, value },
  }: React.ChangeEvent<HTMLInputElement>) =>
    setFormState(prev => ({ ...prev, [name]: value }));

  const handleSubmit = async () => {
      try {
        const user = await login(formState).unwrap();
        dispatch(setCredentials(user));
        navigate('/rsvp');
      } catch (e) {
        console.log(e);
      }
  };

  return (
    <Paper>
      <Grid container spacing={2}>
        <Grid item xs={12} md={12} lg={12}>
          <Typography variant="h6">
            Guest Login
          </Typography>
        </Grid>
        <Grid item xs={12} md={6} lg={6}>
          <TextField label="First Name" variant="outlined" onChange={handleChange} />
        </Grid>
        <Grid item xs={12} md={6} lg={6}>
          <TextField label="Last Name" variant="outlined" onChange={handleChange} />
        </Grid>
        <Grid item>
          <Button onClick={handleSubmit} variant="contained">
            Login
          </Button>
        </Grid>
      </Grid>
    </Paper>
  );
}

export default GuestLogin;