-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathEmailModal.tsx
More file actions
179 lines (171 loc) · 7.09 KB
/
Copy pathEmailModal.tsx
File metadata and controls
179 lines (171 loc) · 7.09 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import React, { FormEvent, useState } from 'react';
import {
Grid2 as Grid,
Checkbox,
TextField,
FormControl,
FormControlLabel,
FormHelperText,
Stack,
useMediaQuery,
Theme,
} from '@mui/material';
import { modalStyle } from 'components/common';
import Modal from '@mui/material/Modal';
import { ModalProps } from './types';
import { BodyText, Heading1 } from '../Typography';
import { Button } from '../Input';
/**
* A modal component for collecting user email and agreement to terms.
* It includes fields for email input, terms and conditions checkbox, and submit button.
* @param {Object} props - The properties for the EmailModal component.
* @param {boolean} props.open - Controls the visibility of the modal.
* @param {string} props.email - The email address input by the user.
* @param {Function} props.updateEmail - Function to update the email state.
* @param {Function} props.updateModal - Function to update the modal visibility state.
* @param {string} props.header - The main title of the modal.
* @param {Array} props.subText - An array of objects containing text to display in the modal, with optional bold styling.
* @param {JSX.Element} props.signupoptions - JSX element containing additional signup options.
* @param {JSX.Element} props.termsOfService - JSX element containing the terms of service text.
* @param {Function} props.handleConfirm - Function to call when the user confirms the action.
* @param {boolean} props.isSaving - Indicates if the form is currently being submitted.
* @returns {JSX.Element} A modal component with email input and terms agreement.
* @example
* <EmailModal
* open={true}
* email={email}
* updateEmail={setEmail}
* updateModal={setOpen}
* header="Sign Up"
* subText={[{ text: 'Please enter your email to sign up.', bold: false }]}
* signupoptions={<div>Additional signup options here</div>}
* termsOfService={<div>Terms of Service text here</div>}
* handleConfirm={() => console.log('Confirmed')}
* isSaving={false}
* />
*/
const EmailModal = ({
open,
email,
updateEmail,
updateModal,
header,
subText,
signupoptions,
termsOfService,
handleConfirm,
isSaving,
}: ModalProps) => {
const [checked, setChecked] = useState(false);
const isSmallScreen: boolean = useMediaQuery((theme: Theme) => theme.breakpoints.down('sm'));
const [emailFormError, setEmailFormError] = useState({
terms: false,
email: false,
});
const validateForm = () => {
const errors = {
terms: !checked,
email: !email,
};
setEmailFormError(errors);
return Object.values(errors).some((isError: unknown) => isError);
};
const handleSubmit = (e: FormEvent) => {
e.preventDefault();
const hasErrors = validateForm();
if (!hasErrors && handleConfirm) {
handleConfirm();
}
};
return (
<Modal aria-labelledby="modal-title" open={open} onClose={() => updateModal(false)}>
<form onSubmit={(e) => handleSubmit(e)}>
<Grid
container
direction="row"
sx={{
...modalStyle,
overflowY: 'auto',
}}
alignItems="flex-start"
justifyContent="flex-start"
rowSpacing={2}
>
<Grid size={12}>
<Heading1 bold sx={{ mb: 2 }}>
{header}
</Heading1>
</Grid>
{subText.map((subtext) => (
<Grid size={12}>
<BodyText bold={subtext.bold}>{subtext.text}</BodyText>
</Grid>
))}
{signupoptions}
<Grid size={12}>{termsOfService}</Grid>
<Grid
container
direction="row"
width="100%"
size={12}
alignItems="flex-start"
justifyContent="flex-start"
rowSpacing={1}
>
<Grid size={12}>
<FormControl required error={emailFormError.terms} component="fieldset" variant="standard">
<FormControlLabel
control={
<Checkbox
onChange={(event) => {
setChecked(event.target.checked);
setEmailFormError({ ...emailFormError, terms: false });
}}
/>
}
label="I agree to the terms and conditions above."
/>
<FormHelperText>
{emailFormError.terms ? 'Please accept the terms and conditions' : ''}
</FormHelperText>
</FormControl>
</Grid>
<Grid size={12}>
<BodyText bold>Email Address</BodyText>
<TextField
onChange={(e) => {
updateEmail(e.target.value);
setEmailFormError({ ...emailFormError, email: false });
}}
label=" "
InputLabelProps={{
shrink: false,
}}
variant="outlined"
error={emailFormError.email}
helperText={emailFormError.email ? 'Please enter an email' : ''}
fullWidth
/>
</Grid>
</Grid>
<Grid container size={12} direction="row" justifyContent="flex-end" spacing={1} sx={{ mt: '1em' }}>
<Stack
direction={{ md: 'column-reverse', lg: 'row' }}
spacing={1}
width="100%"
justifyContent="flex-end"
>
<Button sx={{ mb: isSmallScreen ? 2 : 0 }} onClick={() => updateModal(false)}>
Cancel
</Button>
<Button variant="primary" loading={isSaving} type="submit">
Submit
</Button>
</Stack>
</Grid>
</Grid>
</form>
</Modal>
);
};
export default EmailModal;