diff options
author | Michael Hunteman <michael@huntm.net> | 2024-02-25 13:46:41 -0600 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2024-02-25 13:46:41 -0600 |
commit | 064c11060f7a8e1ec5e1a128a7beabd7635991ca (patch) | |
tree | cf7a8e99f595efb8b296ecda66d694479b21f49a /src/features | |
parent | 68a86b2f9c41717767443b6b9e1860cb73b2aa30 (diff) |
Use react-hook-form
Diffstat (limited to 'src/features')
-rw-r--r-- | src/features/auth/GuestLogin.tsx | 43 |
1 files changed, 25 insertions, 18 deletions
diff --git a/src/features/auth/GuestLogin.tsx b/src/features/auth/GuestLogin.tsx index fb305f7..2100a94 100644 --- a/src/features/auth/GuestLogin.tsx +++ b/src/features/auth/GuestLogin.tsx @@ -2,6 +2,7 @@ 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 { useForm } from 'react-hook-form'; import { setCredentials } from './authSlice'; import { useLoginMutation } from '../../apiSlice'; @@ -10,23 +11,19 @@ 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 { register, handleSubmit, + formState: { isDirty, isValid } } = useForm<LoginRequest>({ + defaultValues: { + firstName: '', + lastName: '' + } + }); - const handleSubmit = async () => { + const onSubmit = async (data: LoginRequest) => { try { - const user = await login(formState).unwrap(); + const user = await login(data).unwrap(); dispatch(setCredentials(user)); navigate('/rsvp'); } catch (e) { @@ -35,7 +32,7 @@ function GuestLogin() { }; return ( - <Paper> + <form onSubmit={handleSubmit(onSubmit)}> <Grid container spacing={2}> <Grid item xs={12} md={12} lg={12}> <Typography variant="h6"> @@ -43,18 +40,28 @@ function GuestLogin() { </Typography> </Grid> <Grid item xs={12} md={6} lg={6}> - <TextField label="First Name" variant="outlined" onChange={handleChange} /> + <TextField + label="First Name" + variant="outlined" + required + {...register("firstName", { required: true })} + /> </Grid> <Grid item xs={12} md={6} lg={6}> - <TextField label="Last Name" variant="outlined" onChange={handleChange} /> + <TextField + label="Last Name" + variant="outlined" + required + {...register("lastName", { required: true })} + /> </Grid> <Grid item> - <Button onClick={handleSubmit} variant="contained"> + <Button type="submit" variant="contained" disabled={!isDirty || !isValid}> Login </Button> </Grid> </Grid> - </Paper> + </form> ); } |