-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormSection.tsx
322 lines (292 loc) · 11.1 KB
/
FormSection.tsx
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import React, { useState, useEffect }from 'react';
import { Link, useHistory } from 'react-router-dom';
import { useFormikContext } from 'formik';
import { useToasts } from 'react-toast-notifications';
import * as Yup from 'yup';
import Loader from 'react-loader-spinner'
import "react-loader-spinner/dist/loader/css/react-spinner-loader.css"
import { RemoteCallContainer, UploadImageType } from '../../../provider/RemoteCallContainer';
import Button from '../../../components/Button';
import FormikWizard, { WizardStep } from '../../../components/FormikWizard';
import InputField from '../../../components/InputField';
import UploadField from '../../../components/UploadField';
import { TermsOfUseView } from '../../home/TermsOfUseView';
import useWindowSize from '../../../hooks/useWindowSize';
import { useAsyncState } from '../../../hooks/useAsyncState';
import { Routes } from '../../../utils/routes';
import { APPLY_DINER, TailWindBreakpoints } from '../../../utils/constants';
import OpenMealLogo from '../../../images/logo_and_wordmark.svg';
interface DinerSignUpProps {
name: string;
email: string;
phone: string;
password: string;
passwordConfirm: string;
uploadedFiles: File | Array<File>;
}
export default function FormSection() {
const [isSaveInProgress, setIsSaveInProgress] = useState(false);
const [isDisplayForm, setIsDisplayForm] = useState(true);
const [dinerResponse, setDinerResponse] = useAsyncState({});
const [totalFormSteps, setTotalFormSteps] = useState(0);
// useEffect( () => {
// if (Object.keys(dinerResponse).length > 0) {
// if (dinerResponse.token) {
// }
// }
// }, [dinerResponse]);
const windowSize = useWindowSize();
const history = useHistory();
const { addToast } = useToasts();
const APICaller = RemoteCallContainer.useContainer();
const initialValues: DinerSignUpProps = {
name: '',
email: '',
phone: '',
password: '',
passwordConfirm: '',
uploadedFiles: null
};
const validationSchemaBasicInfo = Yup.object().shape({
name: Yup.string().required('Please enter full name')
.max(72),
email: Yup.string().required('Please enter email')
.email('Please enter a valid email'),
phone: Yup.string().required("Please enter phone")
.matches(/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/, 'Please enter a valild phone number'),
password: Yup.string()
.required('Please enter password')
.min(8)
.max(32)
.matches(/^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[\@\#\$%\^\&\+\=])/,
'Requires 1 lower, 1 upper, 1 number, and 1 of @#$%^&+='),
passwordConfirm: Yup.string()
.required('Please confirm password')
.test('passwords-match', 'Passwords must match', function (value) {
return this.parent.password === value;
})
});
const validationSchemaUpload = Yup.object().shape({
uploadedFiles: Yup.mixed().required('Please upload a photo ID')
});
const inputFieldClassName = 'w-full sm:w-80 md:w-88 lg:w-96 xl:w-104';
const successFailWrapperClassName = 'w-72 sm:w-auto sm:max-w-sm md:max-w-md lg:max-w-lg xl:max-w-2xl'
const successFailHeaderClassName = '\
font-extrabold text-center \
text-base md:text-lg lg:text-xl xl:text-2xl \
w-72 sm:w-auto mb-2 mt-6 \
';
const scrollToTop = () => window.scrollTo({ top: 0, behavior: 'smooth' });
const resetState = () => {
setDinerResponse({});
setIsSaveInProgress(false);
setIsDisplayForm(true);
setTotalFormSteps(0);
}
/*
* form submit handler
*/
const handleSignUpDiner = async (values, bag, numberOfFormSteps) => {
// values from all form steps (uploadedFiles is an array of JS File objects)
const {name, email, phone, password, uploadedFiles} = values;
// make sure valid image has been specified before proceeding with API calls
if (uploadedFiles && uploadedFiles.length > 0) {
setIsSaveInProgress(true);
// keep only numbers from the phone (Yup validation ensures there can only be 10 numbers plus (-.))
const phoneNumbersOnly = phone.replace(/\D/g, '');
// get uploaded file's extension from its MIME type: assumes only one file can be uploaded
const fileExtension = (uploadedFiles[0].type).replace(/^.*\//, '').toLowerCase();
/*
* upload diner image
*/
const fileData = new Blob(uploadedFiles);
const reader = new FileReader();
reader.readAsArrayBuffer(fileData);
reader.onload = async () => {
const arrayBuffer = reader.result;
const bytes = new Uint8Array(arrayBuffer as any);
let responseData = await APICaller.uploadImage(bytes, UploadImageType.Item, fileExtension, setDinerResponse);
if (responseData.file_name) {
/*
* save diner info
*/
const data = {
name: name,
email: email,
phone: phoneNumbersOnly,
password: password,
imageURL: responseData.file_name
}
responseData = await APICaller.signUpDiner(data, setDinerResponse);
}
if (responseData.errorMessage) {
addToast(responseData.errorMessage, { appearance: 'error' })
} else {
addToast('You\'ve been signed up! Please follow the directions above to complete the process.', {
appearance: 'success'
});
setIsDisplayForm(false);
setTotalFormSteps(numberOfFormSteps);
}
setIsSaveInProgress(false);
scrollToTop();
};
}
};
const handleGotoGoogleApplication = () => {
resetState();
};
const handleGoBackToBeginning = () => {
resetState();
}
return (
<div className="
flex flex-col items-center justify-center
mx-auto
my-24 md:my-32
px-4 sm:px-16 md:px-24 xl:px-32
py-8 sm:py-10 md:py-12
bg-white rounded-md border border-solid border-gray-400 shadow"
>
{ isSaveInProgress && <Loader
type="Watch"
color="#ff6027"
height={ windowSize.width < TailWindBreakpoints.SM ? '2rem' :
windowSize.width < TailWindBreakpoints.MD ? '3rem' : '4rem' }
width={ windowSize.width < TailWindBreakpoints.SM ? "2rem" :
windowSize.width < TailWindBreakpoints.MD ? '3rem' : '4rem' }
/> }
{ isDisplayForm ? ( /* form view */
<FormikWizard
initialValues={initialValues}
onSubmit={handleSignUpDiner}
buttonClassName="my-4"
numberOfStepsToAddToDisplay={1}
>
{/*
* Step 1. Basic Info
*/}
<WizardStep
onSubmit={ () => {/* console.log('Step1 onSubmit'); */} }
validationSchema={validationSchemaBasicInfo}
buttonNextLabel="Next"
buttonPreviousLabel="Previous"
contentUnderButton={<span>Already have an account? Sign in <Link className="text-pop underline" to={Routes.SignInPage}>here</Link></span>}
header="Create a Diner Account"
>
<InputField
className={inputFieldClassName}
type="text"
name="name"
label="Full Name"
/>
<InputField
className={`${inputFieldClassName} mt-1`}
type="email"
name="email"
label="Email"
/>
<InputField
className={`${inputFieldClassName} mt-1`}
type="text"
name="phone"
label="Phone Number"
tooltip="Examples:<ul><li>1112223333</li><li>111.222.3333</li><li>111-222-3333</li><li>(111) 222-3333</li></ul>"
/>
<InputField
className={`${inputFieldClassName} mt-1`}
type="passwordShowHide"
name="password"
label="Password"
/>
<InputField
className={`${inputFieldClassName} mt-1`}
type="passwordShowHide"
name="passwordConfirm"
label="Confirm Password"
/>
</WizardStep>
{/*
* Terms of Use agreement
*/}
<WizardStep
onSubmit={ () => {/* console.log('Step2 onSubmit') */} }
buttonNextLabel="I Agree"
buttonPreviousLabel="Previous"
header="Terms of Use"
>
<div className="
h-64 sm:h-112
mb-6 sm:mb-8 mt-6 sm:mt-8
border border-solid border-gray-400 rounded-sm
overflow-x-hidden overflow-y-scroll
">
<TermsOfUseView isContentOnly={true} />
</div>
</WizardStep>
{/*
* Upload ID
*/}
<WizardStep
onSubmit={ () => {/*console.log("Step3 onSubmit") */} }
buttonSubmitLabel="Sign Up"
buttonPreviousLabel="Previous"
validationSchema={validationSchemaUpload}
header="Upload Your ID"
>
<div className="text-left w-72 sm:w-auto">
Our team is working hard to ensure that the donations make their
way to the right people. Please upload a picture ID (i.e. driver’s
license, state ID, etc.) as proof of your identity so we may assist
you.
</div>
<UploadField fieldName="uploadedFiles" useFormikContext={useFormikContext} />
</WizardStep>
</FormikWizard>
) : ( /* Success View */
<>
<img
className="w-48 sm:w-40 md:w-48 lg:w-56 xl:w-64"
src={OpenMealLogo}
alt="OpenMeal"
/>
<div className={`${successFailWrapperClassName} text-left`}>
<p className={successFailHeaderClassName}>
Thank You for Your Interest in Becoming an OpenMeal Diner!
</p>
<p className="text-base lg:text-lg xl:text-xl text-center mb-4">
{`Step ${totalFormSteps + 1 } of ${totalFormSteps + 1}`}
</p>
<p className="mb-4">
Next, we'd like to ask you to complete the following application.
</p>
<p className="mb-4">
It will take about 20 minutes to complete, and we will get back to you within 48 hours.
</p>
<p className="mb-4">
Once you've applied and approved as a requester, you will be able to place orders.
</p>
<p className='mb-4'>
If you've already applied, we'll be in touch shortly. Thank you for your patience!
</p>
<div className="text-center">
<a
href={APPLY_DINER}
target="_blank"
rel="noopener noreferrer">
<Button
type="primary"
size={windowSize.width < TailWindBreakpoints.SM ? 'ml' : 'md'}
onClick={handleGotoGoogleApplication}
>
Go to application
</Button>
</a>
</div>
</div>
</>
)
}
</div>
);
}