-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
473 lines (438 loc) · 15.6 KB
/
index.jsx
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
import { yupResolver } from "@hookform/resolvers/yup";
import PropTypes from "prop-types";
import React, { useEffect } from "react";
import { useForm } from "react-hook-form";
import { toast } from "sonner";
import * as yup from "yup";
import styles from "./index.module.scss";
import TextControl from "../../../components/form-fields/text-control";
import Translate from "../../../components/translate";
import { getOriginalErrors } from "../../../utils/errorHandler";
// A functional component for rendering a first name input field
// Props:
// - `register`: React Hook Form's `register` function for connecting the input to the form
// - `error`: Error message to display if there's a validation error
function FirstNameInput({ register, error }) {
return (
<div>
<TextControl
label="components.user.first_name"
placeholder="John"
name="firstName"
register={register}
/>
{error && <div className={`${styles.errorLabel}`}>{error}</div>}
</div>
);
}
// A functional component for rendering a last name input field
// Props:
// - `register`: React Hook Form's `register` function for connecting the input to the form
// - `error`: Error message to display if there's a validation error
function LastNameInput({ register, error }) {
return (
<div>
<TextControl
label="components.user.last_name"
placeholder="Doe"
name="lastName"
register={register}
/>
{error && <div className={`${styles.errorLabel}`}>{error}</div>}
</div>
);
}
// A functional component for rendering an email input field
// Props:
// - `register`: React Hook Form's `register` function for connecting the input to the form
// - `error`: Error message to display if there's a validation error
function EmailInput({ register, error }) {
return (
<div>
<TextControl
label="components.user.email"
type="email"
placeholder="johndoe@example.com"
name="email"
register={register}
/>
{error && <div className={`${styles.errorLabel}`}>{error}</div>}
</div>
);
}
// A functional component for rendering a phone number input field
// Props:
// - `register`: React Hook Form's `register` function for connecting the input to the form
// - `error`: Error message to display if there's a validation error
function PhoneInput({ register, error }) {
return (
<div>
<TextControl
label="components.user.phone"
placeholder="+000XXXXXXX"
name="phone"
register={register}
/>
{error && <div className={`${styles.errorLabel}`}>{error}</div>}
</div>
);
}
// A functional component for rendering an address input field
// Props:
// - `register`: React Hook Form's `register` function for connecting the input to the form
// - `name`: Name attribute for the input field
// - `error`: Error message to display if there's a validation error
function AddressInput({ register, name, error }) {
return (
<div>
<TextControl
label="components.address.address"
placeholder="22 Corona St. New Baltimore, MI 48047"
name={name}
register={register}
/>
{error && <div className={`${styles.errorLabel}`}>{error}</div>}
</div>
);
}
// A functional component for rendering a zip code input field
// Props:
// - `register`: React Hook Form's `register` function for connecting the input to the form
// - `name`: Name attribute for the input field
// - `error`: Error message to display if there's a validation error
function ZipCodeInput({ register, name, error }) {
return (
<div>
<TextControl
label="components.address.zip_code"
placeholder="R45 5D5"
name={name}
register={register}
/>
{error && <div className={`${styles.errorLabel}`}>{error}</div>}
</div>
);
}
// A functional component for rendering a city input field
// Props:
// - `register`: React Hook Form's `register` function for connecting the input to the form
// - `name`: Name attribute for the input field
// - `error`: Error message to display if there's a validation error
function CityInput({ register, name, error }) {
return (
<div>
<TextControl
label="components.address.city"
placeholder="Helsinki"
name={name}
register={register}
/>
{error && <div className={`${styles.errorLabel}`}>{error}</div>}
</div>
);
}
// A functional component for rendering a set of address-related input fields (Address, Zip Code, City)
// Props:
// - `register`: React Hook Form's `register` function for connecting the inputs to the form
// - `addressInputName`: Name attribute for the address input field
// - `zipCodeInputName`: Name attribute for the zip code input field
// - `cityInputName`: Name attribute for the city input field
// - `errors`: Object containing validation error messages for each input
function AddressControl({
register,
addressInputName,
zipCodeInputName,
cityInputName,
errors,
}) {
return (
<div
className={`d-md-flex flex-md-wrap justify-content-between ${styles.addressColumn}`}
>
<AddressInput
register={register}
name={addressInputName}
error={errors[addressInputName]?.message}
/>
<ZipCodeInput
register={register}
name={zipCodeInputName}
error={errors[zipCodeInputName]?.message}
/>
<CityInput
register={register}
name={cityInputName}
error={errors[cityInputName]?.message}
/>
</div>
);
}
// A functional component for rendering a toggle switch input to indicate if the installation address is different
// Props:
// - `value`: Current value of the switch (true/false)
// - `setValue`: Function to set the value of the switch
function IsTargetInstallationAddressDifferent({ value, setValue }) {
return (
<label
className="switch"
htmlFor="is_the_address_of_the_installation_target_different"
>
<Translate id="offer.request.is_the_address_of_the_installation_target_different" />
<input
className="switchInput"
type="checkbox"
checked={value}
id="is_the_address_of_the_installation_target_different"
onChange={(event) =>
setValue("isTargetInstallationAddressDifferent", event.target.checked)
}
/>
<span className="slider round">
<Translate id="common.yes" />
</span>
</label>
);
}
// A React component for rendering customer information and address details
// Props:
// - `customerDetails`: Object containing customer details
// - `onUpdateCustomerDetails`: Function to update customer details
// - `offerRequest`: Object containing offer request details
// - `onUpdateOfferRequest`: Function to update offer request details
// - `apiError`: Error message from the API
function CustomerInformation({
customerDetails,
onUpdateCustomerDetails,
offerRequest,
onUpdateOfferRequest,
apiError,
}) {
const customerSchema = yup.object({
// Define schema for various customer fields
firstName: yup.string(), // Validation for first name as a string
lastName: yup.string(), // Validation for last name as a string
email: yup.string(), // Validation for email as a string
phone: yup.string(), // Validation for phone as a string
address: yup.string(), // Validation for address as a string
zipCode: yup.string(), // Validation for zip code as a string
city: yup.string(), // Validation for city as a string
installationTargetAddress: yup.string(), // Validation for installation target address as a string
installationTargetCity: yup.string(), // Validation for installation target city as a string
installationTargetZipCode: yup.string(), // Validation for installation target zip code as a string
isTargetInstallationAddressDifferent: yup.boolean().default(false), // Validation for a boolean field with a default value of 'false'
});
const {
register,
formState: { errors },
watch,
reset,
setError,
clearErrors,
setValue,
} = useForm({
resolver: yupResolver(customerSchema),
});
watch(
(
{
firstName,
lastName,
email,
phone,
address,
zipCode,
city,
installationTargetAddress,
installationTargetCity,
installationTargetZipCode,
isTargetInstallationAddressDifferent,
},
name
) => {
onUpdateCustomerDetails((prev) => ({
...prev,
firstName,
lastName,
email,
phone,
address,
zipCode,
city,
}));
onUpdateOfferRequest((prev) => ({
...prev,
installationTargetAddress: isTargetInstallationAddressDifferent
? installationTargetAddress
: address,
installationTargetCity: isTargetInstallationAddressDifferent
? installationTargetCity
: city,
installationTargetZipCode: isTargetInstallationAddressDifferent
? installationTargetZipCode
: zipCode,
isTargetInstallationAddressDifferent,
}));
clearErrors(name.name);
}
);
useEffect(() => {
reset({
...customerDetails,
...offerRequest,
firstName: customerDetails.firstName || "",
lastName: customerDetails.lastName || "",
email: customerDetails.email || "",
phone: customerDetails.phone || "",
address: customerDetails.address || "",
zipCode: customerDetails.zipCode || "",
city: customerDetails.city || "",
});
}, []);
useEffect(() => {
if (apiError) {
const originalErrors = getOriginalErrors(apiError);
originalErrors.forEach((error) => {
if (error.field) {
setError(error.field, { message: error.message });
} else {
toast.error(error.message);
}
});
}
}, [apiError]);
return (
<div className={`${styles.customerInformationContainer}`}>
<div className={`${styles.headerContainer}`}>
<h1 className={`${styles.headerTitle}`}>
<Translate id="offer.request.customer_information" />
</h1>
</div>
<form>
<div
className={`d-md-flex flex-md-wrap justify-content-between ${styles.contentContainer}`}
>
<FirstNameInput
register={register}
error={errors.firstName?.message}
/>
<LastNameInput register={register} error={errors.lastName?.message} />
<EmailInput register={register} error={errors.email?.message} />
<PhoneInput register={register} error={errors.phone?.message} />
<AddressControl
register={register}
addressInputName="address"
zipCodeInputName="zipCode"
cityInputName="city"
errors={errors}
/>
</div>
<div className={`${styles.addressContainer}`}>
<div className={`${styles.sliderBtnContainer}`}>
<IsTargetInstallationAddressDifferent
value={offerRequest.isTargetInstallationAddressDifferent}
setValue={setValue}
/>
</div>
{offerRequest.isTargetInstallationAddressDifferent && (
<div className={`${styles.addressContent}`}>
<h2 className={`${styles.headerTitle}`}>
<Translate id="offer.request.enter_the_address_of_the_installation_target" />
</h2>
<AddressControl
register={register}
addressInputName="installationTargetAddress"
zipCodeInputName="installationTargetCity"
cityInputName="installationTargetZipCode"
errors={errors}
/>
</div>
)}
</div>
</form>
</div>
);
}
// PropTypes definitions for component props
FirstNameInput.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input field with React Hook Form
error: PropTypes.string, // Optional error message string
};
FirstNameInput.defaultProps = {
error: "", // Default error message is an empty string
};
LastNameInput.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input field with React Hook Form
error: PropTypes.string, // Optional error message string
};
LastNameInput.defaultProps = {
error: "", // Default error message is an empty string
};
EmailInput.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input field with React Hook Form
error: PropTypes.string, // Optional error message string
};
EmailInput.defaultProps = {
error: "", // Default error message is an empty string
};
PhoneInput.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input field with React Hook Form
error: PropTypes.string, // Optional error message string
};
PhoneInput.defaultProps = {
error: "", // Default error message is an empty string
};
AddressInput.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input field with React Hook Form
name: PropTypes.string.isRequired, // Name attribute for the input field
error: PropTypes.string, // Optional error message string
};
AddressInput.defaultProps = {
error: "", // Default error message is an empty string
};
ZipCodeInput.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input field with React Hook Form
name: PropTypes.string.isRequired, // Name attribute for the input field
error: PropTypes.string, // Optional error message string
};
ZipCodeInput.defaultProps = {
error: "", // Default error message is an empty string
};
CityInput.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input field with React Hook Form
name: PropTypes.string.isRequired, // Name attribute for the input field
error: PropTypes.string, // Optional error message string
};
CityInput.defaultProps = {
error: "", // Default error message is an empty string
};
AddressControl.propTypes = {
register: PropTypes.func.isRequired, // Function to register the input fields with React Hook Form
addressInputName: PropTypes.string.isRequired, // Name attribute for the address input field
zipCodeInputName: PropTypes.string.isRequired, // Name attribute for the zip code input field
cityInputName: PropTypes.string.isRequired, // Name attribute for the city input field
errors: PropTypes.instanceOf(Object), // Object containing validation errors
};
AddressControl.defaultProps = {
errors: {}, // Default value for errors is an empty object
};
CustomerInformation.propTypes = {
customerDetails: PropTypes.instanceOf(Object), // Object containing customer details
onUpdateCustomerDetails: PropTypes.func, // Function to update customer details
offerRequest: PropTypes.instanceOf(Object), // Object containing offer request details
onUpdateOfferRequest: PropTypes.func, // Function to update offer request details
apiError: PropTypes.oneOfType([
PropTypes.instanceOf(Array), // Either an array or object containing API errors
PropTypes.instanceOf(Object),
]).isRequired, // Required API error(s)
};
CustomerInformation.defaultProps = {
customerDetails: {}, // Default customer details are an empty object
onUpdateCustomerDetails: () => {}, // Default update function is a no-op function
offerRequest: {}, // Default offer request details are an empty object
onUpdateOfferRequest: () => {}, // Default update function is a no-op function
};
IsTargetInstallationAddressDifferent.propTypes = {
value: PropTypes.bool.isRequired, // Boolean value indicating the state of the toggle switch
setValue: PropTypes.func.isRequired, // Function to set the toggle switch value
};
export default CustomerInformation;