summaryrefslogtreecommitdiff
path: root/src/components/RsvpForm.tsx
blob: d7efecfb4883e82f0beff2dd295b7bc47644e461 (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
129
130
131
132
133
134
135
import {
  Button,
  Container,
  FormControl,
  FormControlLabel,
  FormLabel,
  Grid,
  Radio,
  RadioGroup,
  TextField,
} from '@mui/material';
import { useForm, Controller } from 'react-hook-form';
import { useOutletContext } from "react-router-dom";
import { useUpdateGuestMutation, Guest } from '../apiSlice';

function RsvpForm() {
  const [updateGuest] = useUpdateGuestMutation();
  const guest: Guest = useOutletContext();

  const { register, handleSubmit, control, formState: { errors } } = useForm({
    defaultValues: {
      id: guest?.id,
      firstName: guest?.firstName,
      lastName: guest?.lastName,
      attendance: '',
      email: '',
      partySize: 1,
      message: ''
    }
  });

  const onSubmit = async (data: Guest) => {
    updateGuest({...data});
  };

  return (
    <Container component="form" maxWidth="sm" noValidate onSubmit={handleSubmit(onSubmit)}>
      <Grid container spacing={2} sx={{ mt: 8 }}>
        <Grid item xs={12}>
          <p>
            Please RSVP for the wedding by March 10, 2025.
            The ceremony will commence at 3 pm on April 26 in ...
            The reception will follow at 5 pm in A Venue on the Ridge.
          </p>
        </Grid>
        <Grid item xs={12}>
          <div style={{ display: 'flex', justifyContent: 'center' }}>
            <FormControl>
              <div style={{ display: 'flex', alignItems: 'center' }}>
                <FormLabel sx={{ mr: 2 }} error={!!errors.attendance} required>
                  Will you attend?
                </FormLabel>
                <Controller
                  name="attendance"
                  control={control}
                  rules={{ required: true }}
                  render={({ field }) => (
                    <RadioGroup {...field} row>
                      <FormControlLabel
                        value="yes"
                        control={<Radio />}
                        label="Yes"
                      />
                      <FormControlLabel
                        value="no"
                        control={<Radio />}
                        label="No"
                      />
                    </RadioGroup>
                  )}
                />
              </div>
            </FormControl>
          </div>
        </Grid>
        <Grid item xs={12} md={6} lg={6}>
          <TextField
            label="Email"
            variant="outlined"
            fullWidth
            error={!!errors.email}
            helperText={errors.email?.message}
            required
            {...register('email',
              { required: 'This field is required',
                pattern: { value: /\S+@\S+\.\S+/,
                  message: 'Please enter a valid email address' }
              }
            )}
          />
        </Grid>
        <Grid item xs={12} md={6} lg={6}>
          <TextField
            label="Party Size"
            type="number"
            variant="outlined"
            fullWidth
            error={!!errors.partySize}
            helperText={errors.partySize?.message}
            required
            {...register('partySize',
              { required: 'This field is required',
                min: { value: 1, message: 'Please enter a positive integer' },
                max: { value: 9, message: 'Please enter an integer less than 10' }
              }
            )}
          />
        </Grid>
        <Grid item xs={12}>
          <TextField
            label="Message to the couple"
            variant="outlined"
            fullWidth
            multiline
            rows={3}
            {...register('message')}
          />
        </Grid>
        <Grid item xs={12}>
          <div
            style={{ display: 'flex',
            flexDirection: 'column',
            alignItems: 'center' }}
          >
            <Button type="submit" variant="contained">
              RSVP
            </Button>
          </div>
        </Grid>
      </Grid>
    </Container>
  );
}

export default RsvpForm;