@vtex/faststore-plugin-buyer-portal 2.0.41 → 2.0.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/features/addresses/components/AddressForm/AddressForm.tsx +185 -130
- package/src/features/addresses/components/CreateAddressDrawer/CreateAddressDrawer.tsx +3 -24
- package/src/features/addresses/components/EditAddressDrawer/EditAddressDrawer.tsx +2 -21
- package/src/features/addresses/data/COL.ts +36 -0
- package/src/features/addresses/data/FRA.ts +1 -0
- package/src/features/addresses/data/countries.ts +3 -1
- package/src/features/addresses/data/states.ts +4 -0
- package/src/features/addresses/utils/countryRules.test.ts +262 -0
- package/src/features/addresses/utils/countryRules.ts +399 -0
- package/src/features/addresses/utils/index.ts +1 -0
- package/src/features/credit-cards/components/CreateCreditCardDrawer/CreateCreditCardDrawer.tsx +2 -14
- package/src/features/shared/utils/constants.ts +1 -1
- package/src/features/shared/utils/postalCode.ts +10 -70
package/package.json
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { useState } from "react";
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
2
|
|
|
3
3
|
import { ExistingAddress } from "../";
|
|
4
4
|
import {
|
|
@@ -9,11 +9,16 @@ import {
|
|
|
9
9
|
} from "../../../shared/components";
|
|
10
10
|
import { useBuyerPortal } from "../../../shared/hooks";
|
|
11
11
|
import { useLocalization } from "../../../shared/localization/LocalizationContext";
|
|
12
|
-
import { maskPostalCode } from "../../../shared/utils/postalCode";
|
|
13
12
|
import { CountryOptions, type CountryOption } from "../../data/countries";
|
|
14
13
|
import { useDebouncedSearchAddress } from "../../hooks/useDebouncedSearchAddress";
|
|
15
14
|
import { AddressData, AddressInput } from "../../types";
|
|
16
15
|
import {
|
|
16
|
+
applyHiddenNumberDefault,
|
|
17
|
+
applyNumberInput,
|
|
18
|
+
applyZipInput,
|
|
19
|
+
collectAddressIssues,
|
|
20
|
+
fieldsToClearOnCountryChange,
|
|
21
|
+
getCountryRule,
|
|
17
22
|
getStateDisplayValue,
|
|
18
23
|
getStateSaveValue,
|
|
19
24
|
isStateSelected,
|
|
@@ -65,11 +70,19 @@ export const AddressForm = ({
|
|
|
65
70
|
currentOrgUnit?.id ?? ""
|
|
66
71
|
);
|
|
67
72
|
|
|
73
|
+
const countryKey = address.countryCode || address.country;
|
|
74
|
+
const countryRule = getCountryRule(countryKey);
|
|
75
|
+
const issues = isTouched ? collectAddressIssues(address, t) : [];
|
|
76
|
+
const issueFor = (path: string) =>
|
|
77
|
+
issues.find((issue) => issue.path === path);
|
|
78
|
+
|
|
68
79
|
const countryTranslations: Record<string, string> = {
|
|
69
80
|
BRA: t("addresses.forms.country.brazil"),
|
|
70
81
|
USA: t("addresses.forms.country.usa"),
|
|
71
82
|
CAN: t("addresses.forms.country.canada"),
|
|
72
83
|
MEX: t("addresses.forms.country.mexico"),
|
|
84
|
+
FRA: t("addresses.forms.country.france"),
|
|
85
|
+
COL: t("addresses.forms.country.colombia"),
|
|
73
86
|
};
|
|
74
87
|
|
|
75
88
|
const cleanCompletedAddress = () => {
|
|
@@ -79,25 +92,76 @@ export const AddressForm = ({
|
|
|
79
92
|
}
|
|
80
93
|
};
|
|
81
94
|
|
|
95
|
+
useEffect(() => {
|
|
96
|
+
const nextNumber = applyHiddenNumberDefault(
|
|
97
|
+
countryKey,
|
|
98
|
+
address.streetNumber
|
|
99
|
+
);
|
|
100
|
+
if (nextNumber !== (address.streetNumber ?? "")) {
|
|
101
|
+
setAddress({ ...address, streetNumber: nextNumber });
|
|
102
|
+
}
|
|
103
|
+
}, [countryKey]);
|
|
104
|
+
|
|
82
105
|
const handleChangePostalCode = (postalCode: string) => {
|
|
83
106
|
setAddress({
|
|
84
107
|
...address,
|
|
85
|
-
zip:
|
|
108
|
+
zip: applyZipInput(countryKey, postalCode),
|
|
86
109
|
});
|
|
87
110
|
};
|
|
88
111
|
|
|
89
112
|
const hideLabel = isEdit || fixedType;
|
|
90
113
|
|
|
91
114
|
const handleCountrySelect = (option: CountryOption) => {
|
|
92
|
-
|
|
115
|
+
const previousCountry = countryKey;
|
|
116
|
+
const fieldsToClear = fieldsToClearOnCountryChange(
|
|
117
|
+
previousCountry,
|
|
118
|
+
option.id,
|
|
119
|
+
address.streetNumber
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
const nextAddress: AddressInput = {
|
|
93
123
|
...address,
|
|
94
124
|
country: option.name,
|
|
95
125
|
countryCode: option.id,
|
|
96
|
-
state: "",
|
|
97
|
-
|
|
126
|
+
state: fieldsToClear.includes("state") ? "" : address.state,
|
|
127
|
+
streetNumber: fieldsToClear.includes("streetNumber")
|
|
128
|
+
? ""
|
|
129
|
+
: address.streetNumber,
|
|
130
|
+
neighborhood: fieldsToClear.includes("neighborhood")
|
|
131
|
+
? ""
|
|
132
|
+
: address.neighborhood,
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
nextAddress.streetNumber = applyHiddenNumberDefault(
|
|
136
|
+
option.id,
|
|
137
|
+
nextAddress.streetNumber
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
setAddress(nextAddress);
|
|
98
141
|
setSelectedState("");
|
|
99
142
|
};
|
|
100
143
|
|
|
144
|
+
const streetLabel = countryRule.streetAddress.separated
|
|
145
|
+
? t("addresses.forms.streetName")
|
|
146
|
+
: t("addresses.forms.streetAddress");
|
|
147
|
+
|
|
148
|
+
const postalCodeField = (
|
|
149
|
+
<div key="zipCode">
|
|
150
|
+
<InputText
|
|
151
|
+
label={t("addresses.forms.postalCode")}
|
|
152
|
+
value={address.zip}
|
|
153
|
+
className="address-postal-code"
|
|
154
|
+
hasError={Boolean(issueFor("zip"))}
|
|
155
|
+
onChange={(event) => handleChangePostalCode(event.target.value)}
|
|
156
|
+
disabled={!address.countryCode && !address.country}
|
|
157
|
+
/>
|
|
158
|
+
<ErrorMessage
|
|
159
|
+
show={Boolean(issueFor("zip"))}
|
|
160
|
+
message={issueFor("zip")?.message ?? ""}
|
|
161
|
+
/>
|
|
162
|
+
</div>
|
|
163
|
+
);
|
|
164
|
+
|
|
101
165
|
return (
|
|
102
166
|
<>
|
|
103
167
|
{!useExistingAddress ? (
|
|
@@ -134,22 +198,7 @@ export const AddressForm = ({
|
|
|
134
198
|
/>
|
|
135
199
|
</div>
|
|
136
200
|
|
|
137
|
-
{
|
|
138
|
-
<div key="zipCode">
|
|
139
|
-
<InputText
|
|
140
|
-
label={t("addresses.forms.postalCode")}
|
|
141
|
-
value={address.zip}
|
|
142
|
-
className="address-postal-code"
|
|
143
|
-
hasError={isTouched && !address.zip?.trim()}
|
|
144
|
-
onChange={(event) => handleChangePostalCode(event.target.value)}
|
|
145
|
-
disabled={!address.countryCode}
|
|
146
|
-
/>
|
|
147
|
-
<ErrorMessage
|
|
148
|
-
show={isTouched && !address.zip?.trim()}
|
|
149
|
-
message={t("addresses.validation.postalCodeRequired")}
|
|
150
|
-
/>
|
|
151
|
-
</div>
|
|
152
|
-
)}
|
|
201
|
+
{countryRule.zip.renderFirst && postalCodeField}
|
|
153
202
|
|
|
154
203
|
<div key="addressName">
|
|
155
204
|
{isEdit ? (
|
|
@@ -157,7 +206,7 @@ export const AddressForm = ({
|
|
|
157
206
|
label={t("addresses.forms.addressName")}
|
|
158
207
|
value={address.name}
|
|
159
208
|
className="address-name"
|
|
160
|
-
hasError={
|
|
209
|
+
hasError={Boolean(issueFor("name"))}
|
|
161
210
|
onChange={(event) =>
|
|
162
211
|
setAddress({ ...address, name: event.target.value })
|
|
163
212
|
}
|
|
@@ -203,59 +252,68 @@ export const AddressForm = ({
|
|
|
203
252
|
)}
|
|
204
253
|
/>
|
|
205
254
|
)}
|
|
255
|
+
<ErrorMessage
|
|
256
|
+
show={Boolean(issueFor("name"))}
|
|
257
|
+
message={issueFor("name")?.message ?? ""}
|
|
258
|
+
/>
|
|
206
259
|
</div>
|
|
207
260
|
|
|
208
261
|
<div key="streetAddress">
|
|
209
262
|
<InputText
|
|
210
|
-
label={
|
|
263
|
+
label={streetLabel}
|
|
211
264
|
value={address.streetAddress}
|
|
212
265
|
className="street-address"
|
|
213
|
-
hasError={
|
|
266
|
+
hasError={Boolean(issueFor("streetAddress"))}
|
|
214
267
|
onChange={(event) =>
|
|
215
268
|
setAddress({ ...address, streetAddress: event.target.value })
|
|
216
269
|
}
|
|
217
270
|
/>
|
|
218
271
|
<ErrorMessage
|
|
219
|
-
show={
|
|
220
|
-
message={
|
|
272
|
+
show={Boolean(issueFor("streetAddress"))}
|
|
273
|
+
message={issueFor("streetAddress")?.message ?? ""}
|
|
221
274
|
/>
|
|
222
275
|
</div>
|
|
223
276
|
|
|
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
|
-
|
|
277
|
+
{countryRule.streetNumber.visible && (
|
|
278
|
+
<div key="streetNumber">
|
|
279
|
+
<InputText
|
|
280
|
+
label={t("addresses.forms.number")}
|
|
281
|
+
value={address.streetNumber || ""}
|
|
282
|
+
className="address-number"
|
|
283
|
+
hasError={Boolean(issueFor("streetNumber"))}
|
|
284
|
+
onChange={(event) =>
|
|
285
|
+
setAddress({
|
|
286
|
+
...address,
|
|
287
|
+
streetNumber: applyNumberInput(
|
|
288
|
+
countryKey,
|
|
289
|
+
event.target.value
|
|
290
|
+
),
|
|
291
|
+
})
|
|
292
|
+
}
|
|
293
|
+
/>
|
|
294
|
+
<ErrorMessage
|
|
295
|
+
show={Boolean(issueFor("streetNumber"))}
|
|
296
|
+
message={issueFor("streetNumber")?.message ?? ""}
|
|
297
|
+
/>
|
|
298
|
+
</div>
|
|
299
|
+
)}
|
|
300
|
+
|
|
301
|
+
{countryRule.neighborhood.visible && (
|
|
302
|
+
<div key="neighborhood">
|
|
303
|
+
<InputText
|
|
304
|
+
label={t(countryRule.neighborhood.labelKey)}
|
|
305
|
+
value={address.neighborhood || ""}
|
|
306
|
+
className="address-neighborhood"
|
|
307
|
+
hasError={Boolean(issueFor("neighborhood"))}
|
|
308
|
+
onChange={(event) =>
|
|
309
|
+
setAddress({ ...address, neighborhood: event.target.value })
|
|
310
|
+
}
|
|
311
|
+
/>
|
|
312
|
+
<ErrorMessage
|
|
313
|
+
show={Boolean(issueFor("neighborhood"))}
|
|
314
|
+
message={issueFor("neighborhood")?.message ?? ""}
|
|
315
|
+
/>
|
|
316
|
+
</div>
|
|
259
317
|
)}
|
|
260
318
|
|
|
261
319
|
<div key="streetAddress2">
|
|
@@ -274,89 +332,82 @@ export const AddressForm = ({
|
|
|
274
332
|
label={t("addresses.forms.city")}
|
|
275
333
|
className="address-city"
|
|
276
334
|
value={address.city}
|
|
277
|
-
hasError={
|
|
335
|
+
hasError={Boolean(issueFor("city"))}
|
|
278
336
|
onChange={(event) =>
|
|
279
337
|
setAddress({ ...address, city: event.target.value })
|
|
280
338
|
}
|
|
281
339
|
/>
|
|
282
340
|
<ErrorMessage
|
|
283
|
-
show={
|
|
284
|
-
message={
|
|
341
|
+
show={Boolean(issueFor("city"))}
|
|
342
|
+
message={issueFor("city")?.message ?? ""}
|
|
285
343
|
/>
|
|
286
344
|
</div>
|
|
287
345
|
|
|
288
|
-
|
|
289
|
-
<
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
renderOption={(option, index) => {
|
|
311
|
-
const stateData = findStateByDisplayValue(
|
|
312
|
-
address.countryCode || "",
|
|
313
|
-
option
|
|
314
|
-
);
|
|
315
|
-
const isSelected = stateData
|
|
316
|
-
? isStateSelected(address.state, stateData)
|
|
317
|
-
: false;
|
|
318
|
-
|
|
319
|
-
return (
|
|
320
|
-
<AutocompleteDropdown.Item
|
|
321
|
-
key={`${option}-${index}`}
|
|
322
|
-
closeOnClick
|
|
323
|
-
index={index}
|
|
324
|
-
isSelected={isSelected}
|
|
325
|
-
onClick={() => {
|
|
326
|
-
if (stateData) {
|
|
327
|
-
setSelectedState(getStateDisplayValue(stateData));
|
|
328
|
-
setAddress({
|
|
329
|
-
...address,
|
|
330
|
-
state: getStateSaveValue(stateData),
|
|
331
|
-
});
|
|
332
|
-
}
|
|
333
|
-
}}
|
|
334
|
-
>
|
|
335
|
-
{option}
|
|
336
|
-
{isSelected && <Icon name="Check" width={12} height={12} />}
|
|
337
|
-
</AutocompleteDropdown.Item>
|
|
338
|
-
);
|
|
339
|
-
}}
|
|
340
|
-
/>
|
|
341
|
-
</div>
|
|
342
|
-
|
|
343
|
-
{address.countryCode !== "BRA" && (
|
|
344
|
-
<div key="zipCode">
|
|
345
|
-
<InputText
|
|
346
|
-
label={t("addresses.forms.postalCode")}
|
|
347
|
-
value={address.zip}
|
|
348
|
-
className="address-postal-code"
|
|
349
|
-
hasError={isTouched && !address.zip?.trim()}
|
|
350
|
-
onChange={(event) => handleChangePostalCode(event.target.value)}
|
|
346
|
+
{countryRule.state.visible && (
|
|
347
|
+
<div key="state">
|
|
348
|
+
<AutocompleteDropdown
|
|
349
|
+
data-fs-bp-create-address-state-selector
|
|
350
|
+
label={t("addresses.forms.state")}
|
|
351
|
+
className="address-state"
|
|
352
|
+
value={selectedState}
|
|
353
|
+
options={getStateDisplayOptions(address.countryCode || "")}
|
|
354
|
+
shouldCloseOnSelect
|
|
355
|
+
onConfirmKeyPress={(option) => {
|
|
356
|
+
const stateData = findStateByDisplayValue(
|
|
357
|
+
address.countryCode || "",
|
|
358
|
+
option
|
|
359
|
+
);
|
|
360
|
+
if (stateData) {
|
|
361
|
+
setSelectedState(option);
|
|
362
|
+
setAddress({
|
|
363
|
+
...address,
|
|
364
|
+
state: getStateSaveValue(stateData),
|
|
365
|
+
});
|
|
366
|
+
}
|
|
367
|
+
}}
|
|
351
368
|
disabled={!address.countryCode}
|
|
369
|
+
renderOption={(option, index) => {
|
|
370
|
+
const stateData = findStateByDisplayValue(
|
|
371
|
+
address.countryCode || "",
|
|
372
|
+
option
|
|
373
|
+
);
|
|
374
|
+
const isSelected = stateData
|
|
375
|
+
? isStateSelected(address.state, stateData)
|
|
376
|
+
: false;
|
|
377
|
+
|
|
378
|
+
return (
|
|
379
|
+
<AutocompleteDropdown.Item
|
|
380
|
+
key={`${option}-${index}`}
|
|
381
|
+
closeOnClick
|
|
382
|
+
index={index}
|
|
383
|
+
isSelected={isSelected}
|
|
384
|
+
onClick={() => {
|
|
385
|
+
if (stateData) {
|
|
386
|
+
setSelectedState(getStateDisplayValue(stateData));
|
|
387
|
+
setAddress({
|
|
388
|
+
...address,
|
|
389
|
+
state: getStateSaveValue(stateData),
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
}}
|
|
393
|
+
>
|
|
394
|
+
{option}
|
|
395
|
+
{isSelected && (
|
|
396
|
+
<Icon name="Check" width={12} height={12} />
|
|
397
|
+
)}
|
|
398
|
+
</AutocompleteDropdown.Item>
|
|
399
|
+
);
|
|
400
|
+
}}
|
|
352
401
|
/>
|
|
353
402
|
<ErrorMessage
|
|
354
|
-
show={
|
|
355
|
-
message={
|
|
403
|
+
show={Boolean(issueFor("state"))}
|
|
404
|
+
message={issueFor("state")?.message ?? ""}
|
|
356
405
|
/>
|
|
357
406
|
</div>
|
|
358
407
|
)}
|
|
359
408
|
|
|
409
|
+
{!countryRule.zip.renderFirst && postalCodeField}
|
|
410
|
+
|
|
360
411
|
{isEdit && (
|
|
361
412
|
<div key="geoCoordinates">
|
|
362
413
|
<InputText
|
|
@@ -399,6 +450,10 @@ export const AddressForm = ({
|
|
|
399
450
|
</AutocompleteDropdown.Item>
|
|
400
451
|
)}
|
|
401
452
|
/>
|
|
453
|
+
<ErrorMessage
|
|
454
|
+
show={Boolean(issueFor("types"))}
|
|
455
|
+
message={issueFor("types")?.message ?? ""}
|
|
456
|
+
/>
|
|
402
457
|
</div>
|
|
403
458
|
)}
|
|
404
459
|
</div>
|
|
@@ -22,6 +22,7 @@ import { useLocalization } from "../../../shared/localization/LocalizationContex
|
|
|
22
22
|
import { ANALYTICS_EVENTS } from "../../../shared/services/logger/analytics/constants";
|
|
23
23
|
import { useCreateNewAddress } from "../../hooks";
|
|
24
24
|
import { LocationInput } from "../../types/AddressData";
|
|
25
|
+
import { collectAddressIssues } from "../../utils";
|
|
25
26
|
|
|
26
27
|
import type { RecipientInput, AddressInput, AddressData } from "../../types";
|
|
27
28
|
|
|
@@ -155,30 +156,8 @@ export const CreateAddressDrawer = ({
|
|
|
155
156
|
}
|
|
156
157
|
};
|
|
157
158
|
|
|
158
|
-
const
|
|
159
|
-
|
|
160
|
-
const hasRequiredFields =
|
|
161
|
-
address.name &&
|
|
162
|
-
address.streetAddress &&
|
|
163
|
-
address.country &&
|
|
164
|
-
address.zip &&
|
|
165
|
-
address.state &&
|
|
166
|
-
address.city &&
|
|
167
|
-
address.types.length > 0;
|
|
168
|
-
|
|
169
|
-
const hasBrazilianNumber =
|
|
170
|
-
!isBrazil || (isBrazil && address.streetNumber?.trim());
|
|
171
|
-
|
|
172
|
-
const hasBrazilianNeighborhood =
|
|
173
|
-
!isBrazil || (isBrazil && address.neighborhood?.trim());
|
|
174
|
-
|
|
175
|
-
return (
|
|
176
|
-
(hasRequiredFields && hasBrazilianNumber && hasBrazilianNeighborhood) ||
|
|
177
|
-
useExistingAddress
|
|
178
|
-
);
|
|
179
|
-
};
|
|
180
|
-
|
|
181
|
-
const isConfirmButtonEnabled = hasMinimumAddressInformation();
|
|
159
|
+
const isConfirmButtonEnabled =
|
|
160
|
+
useExistingAddress || collectAddressIssues(address, t).length === 0;
|
|
182
161
|
|
|
183
162
|
return (
|
|
184
163
|
<BasicDrawer data-fs-bp-create-address-drawer close={close} {...props}>
|
|
@@ -8,6 +8,7 @@ import { useAnalytics, useBuyerPortal } from "../../../shared/hooks";
|
|
|
8
8
|
import { useLocalization } from "../../../shared/localization/LocalizationContext";
|
|
9
9
|
import { ANALYTICS_EVENTS } from "../../../shared/services/logger/analytics/constants";
|
|
10
10
|
import { useEditAddress } from "../../hooks";
|
|
11
|
+
import { collectAddressIssues } from "../../utils";
|
|
11
12
|
|
|
12
13
|
import type { AddressInput, AddressData } from "../../types";
|
|
13
14
|
|
|
@@ -91,27 +92,7 @@ export const EditAddressDrawer = ({
|
|
|
91
92
|
});
|
|
92
93
|
};
|
|
93
94
|
|
|
94
|
-
const
|
|
95
|
-
const isBrazil = address.countryCode === "BRA";
|
|
96
|
-
const hasRequiredFields =
|
|
97
|
-
address.name &&
|
|
98
|
-
address.streetAddress &&
|
|
99
|
-
address.country &&
|
|
100
|
-
address.zip &&
|
|
101
|
-
address.state &&
|
|
102
|
-
address.city &&
|
|
103
|
-
address.types.length > 0;
|
|
104
|
-
|
|
105
|
-
const hasBrazilianNumber =
|
|
106
|
-
!isBrazil || (isBrazil && address.streetNumber?.trim());
|
|
107
|
-
|
|
108
|
-
const hasBrazilianNeighborhood =
|
|
109
|
-
!isBrazil || (isBrazil && address.neighborhood?.trim());
|
|
110
|
-
|
|
111
|
-
return hasRequiredFields && hasBrazilianNumber && hasBrazilianNeighborhood;
|
|
112
|
-
};
|
|
113
|
-
|
|
114
|
-
const isConfirmButtonEnabled = hasMinimumAddressInformation();
|
|
95
|
+
const isConfirmButtonEnabled = collectAddressIssues(address, t).length === 0;
|
|
115
96
|
|
|
116
97
|
return (
|
|
117
98
|
<BasicDrawer data-fs-bp-edit-address-drawer close={close} {...props}>
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/** 32 departments + Bogotá D.C. */
|
|
2
|
+
export const states = [
|
|
3
|
+
"Amazonas",
|
|
4
|
+
"Antioquia",
|
|
5
|
+
"Arauca",
|
|
6
|
+
"Atlántico",
|
|
7
|
+
"Bogotá D.C.",
|
|
8
|
+
"Bolívar",
|
|
9
|
+
"Boyacá",
|
|
10
|
+
"Caldas",
|
|
11
|
+
"Caquetá",
|
|
12
|
+
"Casanare",
|
|
13
|
+
"Cauca",
|
|
14
|
+
"Cesar",
|
|
15
|
+
"Chocó",
|
|
16
|
+
"Córdoba",
|
|
17
|
+
"Cundinamarca",
|
|
18
|
+
"Guainía",
|
|
19
|
+
"Guaviare",
|
|
20
|
+
"Huila",
|
|
21
|
+
"La Guajira",
|
|
22
|
+
"Magdalena",
|
|
23
|
+
"Meta",
|
|
24
|
+
"Nariño",
|
|
25
|
+
"Norte de Santander",
|
|
26
|
+
"Putumayo",
|
|
27
|
+
"Quindío",
|
|
28
|
+
"Risaralda",
|
|
29
|
+
"Archipiélago de San Andrés, Providencia y Santa Catalina",
|
|
30
|
+
"Santander",
|
|
31
|
+
"Sucre",
|
|
32
|
+
"Tolima",
|
|
33
|
+
"Valle del Cauca",
|
|
34
|
+
"Vaupés",
|
|
35
|
+
"Vichada",
|
|
36
|
+
];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const states: string[] = [];
|
|
@@ -8,6 +8,8 @@ export const CountryOptions: CountryOption[] = [
|
|
|
8
8
|
{ id: "USA", name: "United States of America" },
|
|
9
9
|
{ id: "CAN", name: "Canada" },
|
|
10
10
|
{ id: "MEX", name: "Mexico" },
|
|
11
|
+
{ id: "FRA", name: "France" },
|
|
12
|
+
{ id: "COL", name: "Colombia" },
|
|
11
13
|
];
|
|
12
14
|
|
|
13
|
-
export type CountryCodes = "USA" | "MEX" | "BRA" | "CAN";
|
|
15
|
+
export type CountryCodes = "USA" | "MEX" | "BRA" | "CAN" | "FRA" | "COL";
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { states as BRAStates } from "./BRA";
|
|
2
2
|
import { states as CANStates } from "./CAN";
|
|
3
|
+
import { states as COLStates } from "./COL";
|
|
4
|
+
import { states as FRAStates } from "./FRA";
|
|
3
5
|
import { states as MEXStates } from "./MEX";
|
|
4
6
|
import { states as USAStates, StateOption } from "./USA";
|
|
5
7
|
|
|
@@ -10,4 +12,6 @@ export const states: Record<string, StateData[]> = {
|
|
|
10
12
|
BRA: BRAStates,
|
|
11
13
|
USA: USAStates,
|
|
12
14
|
CAN: CANStates,
|
|
15
|
+
FRA: FRAStates,
|
|
16
|
+
COL: COLStates,
|
|
13
17
|
};
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
|
|
3
|
+
import { states } from "../data/states";
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
applyHiddenNumberDefault,
|
|
7
|
+
applyNumberInput,
|
|
8
|
+
applyZipInput,
|
|
9
|
+
collectAddressIssues,
|
|
10
|
+
fieldsToClearOnCountryChange,
|
|
11
|
+
getCountryRule,
|
|
12
|
+
resolveCountryCode,
|
|
13
|
+
} from "./countryRules";
|
|
14
|
+
|
|
15
|
+
const t = (key: string) => key;
|
|
16
|
+
|
|
17
|
+
const baseValues = {
|
|
18
|
+
name: "HQ",
|
|
19
|
+
streetAddress: "Main St",
|
|
20
|
+
streetAddress2: "",
|
|
21
|
+
streetNumber: "100",
|
|
22
|
+
country: "Brazil",
|
|
23
|
+
countryCode: "BRA",
|
|
24
|
+
zip: "01310-100",
|
|
25
|
+
state: "SP",
|
|
26
|
+
city: "São Paulo",
|
|
27
|
+
types: ["Shipping"],
|
|
28
|
+
neighborhood: "Bela Vista",
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
describe("resolveCountryCode / getCountryRule", () => {
|
|
32
|
+
it("matches country code case-insensitively", () => {
|
|
33
|
+
expect(resolveCountryCode("bra")).toBe("BRA");
|
|
34
|
+
expect(getCountryRule("BRA").zip.renderFirst).toBe(true);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("matches legacy display name Brazil", () => {
|
|
38
|
+
expect(resolveCountryCode("Brazil")).toBe("BRA");
|
|
39
|
+
expect(getCountryRule("Brazil").streetNumber.required).toBe(true);
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("returns fallback for unrecognized country", () => {
|
|
43
|
+
const rule = getCountryRule("Narnia");
|
|
44
|
+
expect(rule.zip.pattern).toBeNull();
|
|
45
|
+
expect(rule.streetNumber.visible).toBe(false);
|
|
46
|
+
expect(rule.state.required).toBe(true);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("never returns null from getCountryRule", () => {
|
|
50
|
+
expect(getCountryRule(undefined)).toBeTruthy();
|
|
51
|
+
expect(getCountryRule("")).toBeTruthy();
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
describe("applyZipInput", () => {
|
|
56
|
+
it("formats Brazil CEP and keeps separators deletable", () => {
|
|
57
|
+
expect(applyZipInput("BRA", "01310")).toBe("01310");
|
|
58
|
+
expect(applyZipInput("BRA", "013101")).toBe("01310-1");
|
|
59
|
+
expect(applyZipInput("BRA", "01310100")).toBe("01310-100");
|
|
60
|
+
expect(applyZipInput("BRA", "01310-")).toBe("01310");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("formats US ZIP and ZIP+4 without truncating the extension", () => {
|
|
64
|
+
expect(applyZipInput("USA", "12345")).toBe("12345");
|
|
65
|
+
expect(applyZipInput("USA", "123456789")).toBe("12345-6789");
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("formats Canada with space only after the third character", () => {
|
|
69
|
+
expect(applyZipInput("CAN", "A1A")).toBe("A1A");
|
|
70
|
+
expect(applyZipInput("CAN", "A1A1A1")).toBe("A1A 1A1");
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("truncates Mexico/France at 5 and Colombia at 6", () => {
|
|
74
|
+
expect(applyZipInput("MEX", "123456")).toBe("12345");
|
|
75
|
+
expect(applyZipInput("FRA", "750011")).toBe("75001");
|
|
76
|
+
expect(applyZipInput("COL", "1101112")).toBe("110111");
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("leaves unresolved countries unchanged", () => {
|
|
80
|
+
expect(applyZipInput("ZZZ", "ab-12")).toBe("ab-12");
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("applyNumberInput", () => {
|
|
85
|
+
it("keeps alphanumeric values for Mexico", () => {
|
|
86
|
+
expect(applyNumberInput("MEX", "12A")).toBe("12A");
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("keeps letters for Brazil", () => {
|
|
90
|
+
expect(applyNumberInput("BRA", "12A")).toBe("12A");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
describe("fieldsToClearOnCountryChange", () => {
|
|
95
|
+
it("always clears state; keeps typed number when both countries show it", () => {
|
|
96
|
+
expect(fieldsToClearOnCountryChange("MEX", "BRA", "10")).toEqual(["state"]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("clears number when next country hides it", () => {
|
|
100
|
+
expect(fieldsToClearOnCountryChange("MEX", "USA", "10")).toEqual([
|
|
101
|
+
"state",
|
|
102
|
+
"streetNumber",
|
|
103
|
+
]);
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it("clears auto-filled Colombia N/A when switching to Mexico", () => {
|
|
107
|
+
expect(fieldsToClearOnCountryChange("COL", "MEX", "N/A")).toEqual([
|
|
108
|
+
"state",
|
|
109
|
+
"streetNumber",
|
|
110
|
+
]);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("clears neighborhood when switching to France", () => {
|
|
114
|
+
expect(fieldsToClearOnCountryChange("MEX", "FRA", "12")).toEqual([
|
|
115
|
+
"state",
|
|
116
|
+
"streetNumber",
|
|
117
|
+
"neighborhood",
|
|
118
|
+
]);
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("collectAddressIssues", () => {
|
|
123
|
+
it("accepts a complete Brazilian address", () => {
|
|
124
|
+
expect(collectAddressIssues(baseValues, t)).toEqual([]);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("accepts unformatted Brazilian CEP on edit", () => {
|
|
128
|
+
expect(collectAddressIssues({ ...baseValues, zip: "01310100" }, t)).toEqual(
|
|
129
|
+
[]
|
|
130
|
+
);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
it("accepts US ZIP+4", () => {
|
|
134
|
+
expect(
|
|
135
|
+
collectAddressIssues(
|
|
136
|
+
{
|
|
137
|
+
...baseValues,
|
|
138
|
+
country: "United States of America",
|
|
139
|
+
countryCode: "USA",
|
|
140
|
+
zip: "12345-6789",
|
|
141
|
+
state: "CA",
|
|
142
|
+
streetNumber: "",
|
|
143
|
+
neighborhood: "",
|
|
144
|
+
},
|
|
145
|
+
t
|
|
146
|
+
)
|
|
147
|
+
).toEqual([]);
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
it("allows France without state or neighborhood", () => {
|
|
151
|
+
expect(
|
|
152
|
+
collectAddressIssues(
|
|
153
|
+
{
|
|
154
|
+
...baseValues,
|
|
155
|
+
country: "France",
|
|
156
|
+
countryCode: "FRA",
|
|
157
|
+
zip: "75001",
|
|
158
|
+
state: "",
|
|
159
|
+
neighborhood: "",
|
|
160
|
+
streetNumber: "",
|
|
161
|
+
},
|
|
162
|
+
t
|
|
163
|
+
)
|
|
164
|
+
).toEqual([]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it("allows Colombia without postal code and requires state", () => {
|
|
168
|
+
expect(
|
|
169
|
+
collectAddressIssues(
|
|
170
|
+
{
|
|
171
|
+
...baseValues,
|
|
172
|
+
country: "Colombia",
|
|
173
|
+
countryCode: "COL",
|
|
174
|
+
zip: "",
|
|
175
|
+
streetNumber: "N/A",
|
|
176
|
+
neighborhood: "",
|
|
177
|
+
state: "Antioquia",
|
|
178
|
+
},
|
|
179
|
+
t
|
|
180
|
+
)
|
|
181
|
+
).toEqual([]);
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it("requires Mexico colonia and flags invalid short zip", () => {
|
|
185
|
+
const issues = collectAddressIssues(
|
|
186
|
+
{
|
|
187
|
+
...baseValues,
|
|
188
|
+
country: "Mexico",
|
|
189
|
+
countryCode: "MEX",
|
|
190
|
+
zip: "1234",
|
|
191
|
+
streetNumber: "12",
|
|
192
|
+
neighborhood: "",
|
|
193
|
+
state: "Jalisco",
|
|
194
|
+
},
|
|
195
|
+
t
|
|
196
|
+
);
|
|
197
|
+
expect(issues.map((issue) => issue.path).sort()).toEqual([
|
|
198
|
+
"neighborhood",
|
|
199
|
+
"zip",
|
|
200
|
+
]);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("flags Brazilian postal format when incomplete", () => {
|
|
204
|
+
const issues = collectAddressIssues({ ...baseValues, zip: "0131" }, t);
|
|
205
|
+
expect(issues.some((issue) => issue.path === "zip")).toBe(true);
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it("reports country rules even when another required field is empty", () => {
|
|
209
|
+
const issues = collectAddressIssues(
|
|
210
|
+
{
|
|
211
|
+
...baseValues,
|
|
212
|
+
name: "",
|
|
213
|
+
country: "Mexico",
|
|
214
|
+
countryCode: "MEX",
|
|
215
|
+
zip: "1234",
|
|
216
|
+
streetNumber: "",
|
|
217
|
+
neighborhood: "",
|
|
218
|
+
state: "Jalisco",
|
|
219
|
+
},
|
|
220
|
+
t
|
|
221
|
+
);
|
|
222
|
+
const paths = issues.map((issue) => issue.path);
|
|
223
|
+
expect(paths).toContain("name");
|
|
224
|
+
expect(paths).toContain("zip");
|
|
225
|
+
expect(paths).toContain("neighborhood");
|
|
226
|
+
expect(paths).toContain("streetNumber");
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it("does not require hidden number for Colombia", () => {
|
|
230
|
+
const issues = collectAddressIssues(
|
|
231
|
+
{
|
|
232
|
+
...baseValues,
|
|
233
|
+
country: "Colombia",
|
|
234
|
+
countryCode: "COL",
|
|
235
|
+
zip: "",
|
|
236
|
+
streetNumber: "",
|
|
237
|
+
neighborhood: "",
|
|
238
|
+
state: "Antioquia",
|
|
239
|
+
},
|
|
240
|
+
t
|
|
241
|
+
);
|
|
242
|
+
expect(issues.map((issue) => issue.path)).not.toContain("streetNumber");
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe("applyHiddenNumberDefault", () => {
|
|
247
|
+
it("writes Colombia N/A only over an empty value", () => {
|
|
248
|
+
expect(applyHiddenNumberDefault("COL", "")).toBe("N/A");
|
|
249
|
+
expect(applyHiddenNumberDefault("COL", "42")).toBe("42");
|
|
250
|
+
});
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
describe("Colombia state list", () => {
|
|
254
|
+
it("includes 33 departments including Bogotá and the archipelago", () => {
|
|
255
|
+
expect(states.COL).toHaveLength(33);
|
|
256
|
+
expect(states.COL).toContain("Bogotá D.C.");
|
|
257
|
+
expect(states.COL).toContain(
|
|
258
|
+
"Archipiélago de San Andrés, Providencia y Santa Catalina"
|
|
259
|
+
);
|
|
260
|
+
expect(states.FRA).toEqual([]);
|
|
261
|
+
});
|
|
262
|
+
});
|
|
@@ -0,0 +1,399 @@
|
|
|
1
|
+
import { CountryOptions, type CountryCodes } from "../data/countries";
|
|
2
|
+
|
|
3
|
+
import type { AddressInput } from "../types";
|
|
4
|
+
|
|
5
|
+
export type SupportedCountryCode = CountryCodes;
|
|
6
|
+
|
|
7
|
+
export type CountryRule = {
|
|
8
|
+
zip: {
|
|
9
|
+
required: boolean;
|
|
10
|
+
pattern: RegExp | null;
|
|
11
|
+
renderFirst: boolean;
|
|
12
|
+
};
|
|
13
|
+
streetNumber: {
|
|
14
|
+
visible: boolean;
|
|
15
|
+
required: boolean;
|
|
16
|
+
digitsOnly: boolean;
|
|
17
|
+
defaultValue: string | null;
|
|
18
|
+
};
|
|
19
|
+
neighborhood: {
|
|
20
|
+
visible: boolean;
|
|
21
|
+
required: boolean;
|
|
22
|
+
labelKey: string;
|
|
23
|
+
};
|
|
24
|
+
state: {
|
|
25
|
+
visible: boolean;
|
|
26
|
+
required: boolean;
|
|
27
|
+
};
|
|
28
|
+
streetAddress: {
|
|
29
|
+
separated: boolean;
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type AddressIssue = {
|
|
34
|
+
path: string;
|
|
35
|
+
message: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type AddressFormValues = Pick<
|
|
39
|
+
AddressInput,
|
|
40
|
+
| "name"
|
|
41
|
+
| "streetAddress"
|
|
42
|
+
| "streetAddress2"
|
|
43
|
+
| "streetNumber"
|
|
44
|
+
| "country"
|
|
45
|
+
| "countryCode"
|
|
46
|
+
| "zip"
|
|
47
|
+
| "state"
|
|
48
|
+
| "city"
|
|
49
|
+
| "types"
|
|
50
|
+
| "neighborhood"
|
|
51
|
+
>;
|
|
52
|
+
|
|
53
|
+
export type ClearableAddressField = "state" | "streetNumber" | "neighborhood";
|
|
54
|
+
|
|
55
|
+
const NEIGHBORHOOD_LABEL = "addresses.forms.neighborhood";
|
|
56
|
+
const COLONIA_LABEL = "addresses.forms.colonia";
|
|
57
|
+
|
|
58
|
+
const FALLBACK_RULE: CountryRule = {
|
|
59
|
+
zip: { required: true, pattern: null, renderFirst: false },
|
|
60
|
+
streetNumber: {
|
|
61
|
+
visible: false,
|
|
62
|
+
required: false,
|
|
63
|
+
digitsOnly: false,
|
|
64
|
+
defaultValue: null,
|
|
65
|
+
},
|
|
66
|
+
neighborhood: {
|
|
67
|
+
visible: true,
|
|
68
|
+
required: false,
|
|
69
|
+
labelKey: NEIGHBORHOOD_LABEL,
|
|
70
|
+
},
|
|
71
|
+
state: { visible: true, required: true },
|
|
72
|
+
streetAddress: { separated: false },
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const COUNTRY_RULES: Record<SupportedCountryCode, CountryRule> = {
|
|
76
|
+
BRA: {
|
|
77
|
+
zip: {
|
|
78
|
+
required: true,
|
|
79
|
+
pattern: /^\d{5}-?\d{3}$/,
|
|
80
|
+
renderFirst: true,
|
|
81
|
+
},
|
|
82
|
+
streetNumber: {
|
|
83
|
+
visible: true,
|
|
84
|
+
required: true,
|
|
85
|
+
digitsOnly: false,
|
|
86
|
+
defaultValue: null,
|
|
87
|
+
},
|
|
88
|
+
neighborhood: {
|
|
89
|
+
visible: true,
|
|
90
|
+
required: true,
|
|
91
|
+
labelKey: NEIGHBORHOOD_LABEL,
|
|
92
|
+
},
|
|
93
|
+
state: { visible: true, required: true },
|
|
94
|
+
streetAddress: { separated: true },
|
|
95
|
+
},
|
|
96
|
+
USA: {
|
|
97
|
+
zip: {
|
|
98
|
+
required: true,
|
|
99
|
+
pattern: /^\d{5}(-\d{4})?$/,
|
|
100
|
+
renderFirst: false,
|
|
101
|
+
},
|
|
102
|
+
streetNumber: {
|
|
103
|
+
visible: false,
|
|
104
|
+
required: false,
|
|
105
|
+
digitsOnly: false,
|
|
106
|
+
defaultValue: null,
|
|
107
|
+
},
|
|
108
|
+
neighborhood: {
|
|
109
|
+
visible: true,
|
|
110
|
+
required: false,
|
|
111
|
+
labelKey: NEIGHBORHOOD_LABEL,
|
|
112
|
+
},
|
|
113
|
+
state: { visible: true, required: true },
|
|
114
|
+
streetAddress: { separated: false },
|
|
115
|
+
},
|
|
116
|
+
CAN: {
|
|
117
|
+
zip: {
|
|
118
|
+
required: true,
|
|
119
|
+
pattern: /^[A-Za-z]\d[A-Za-z] ?\d[A-Za-z]\d$/,
|
|
120
|
+
renderFirst: false,
|
|
121
|
+
},
|
|
122
|
+
streetNumber: {
|
|
123
|
+
visible: false,
|
|
124
|
+
required: false,
|
|
125
|
+
digitsOnly: false,
|
|
126
|
+
defaultValue: null,
|
|
127
|
+
},
|
|
128
|
+
neighborhood: {
|
|
129
|
+
visible: true,
|
|
130
|
+
required: false,
|
|
131
|
+
labelKey: NEIGHBORHOOD_LABEL,
|
|
132
|
+
},
|
|
133
|
+
state: { visible: true, required: true },
|
|
134
|
+
streetAddress: { separated: false },
|
|
135
|
+
},
|
|
136
|
+
MEX: {
|
|
137
|
+
zip: {
|
|
138
|
+
required: true,
|
|
139
|
+
pattern: /^\d{5}$/,
|
|
140
|
+
renderFirst: false,
|
|
141
|
+
},
|
|
142
|
+
streetNumber: {
|
|
143
|
+
visible: true,
|
|
144
|
+
required: true,
|
|
145
|
+
digitsOnly: false,
|
|
146
|
+
defaultValue: null,
|
|
147
|
+
},
|
|
148
|
+
neighborhood: {
|
|
149
|
+
visible: true,
|
|
150
|
+
required: true,
|
|
151
|
+
labelKey: COLONIA_LABEL,
|
|
152
|
+
},
|
|
153
|
+
state: { visible: true, required: true },
|
|
154
|
+
streetAddress: { separated: true },
|
|
155
|
+
},
|
|
156
|
+
FRA: {
|
|
157
|
+
zip: {
|
|
158
|
+
required: true,
|
|
159
|
+
pattern: /^\d{5}$/,
|
|
160
|
+
renderFirst: false,
|
|
161
|
+
},
|
|
162
|
+
streetNumber: {
|
|
163
|
+
visible: false,
|
|
164
|
+
required: false,
|
|
165
|
+
digitsOnly: false,
|
|
166
|
+
defaultValue: null,
|
|
167
|
+
},
|
|
168
|
+
neighborhood: {
|
|
169
|
+
visible: false,
|
|
170
|
+
required: false,
|
|
171
|
+
labelKey: NEIGHBORHOOD_LABEL,
|
|
172
|
+
},
|
|
173
|
+
state: { visible: false, required: false },
|
|
174
|
+
streetAddress: { separated: false },
|
|
175
|
+
},
|
|
176
|
+
COL: {
|
|
177
|
+
zip: {
|
|
178
|
+
required: false,
|
|
179
|
+
pattern: /^\d{6}$/,
|
|
180
|
+
renderFirst: false,
|
|
181
|
+
},
|
|
182
|
+
streetNumber: {
|
|
183
|
+
visible: false,
|
|
184
|
+
required: false,
|
|
185
|
+
digitsOnly: false,
|
|
186
|
+
defaultValue: "N/A",
|
|
187
|
+
},
|
|
188
|
+
neighborhood: {
|
|
189
|
+
visible: true,
|
|
190
|
+
required: false,
|
|
191
|
+
labelKey: NEIGHBORHOOD_LABEL,
|
|
192
|
+
},
|
|
193
|
+
state: { visible: true, required: true },
|
|
194
|
+
streetAddress: { separated: false },
|
|
195
|
+
},
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
export function resolveCountryCode(
|
|
199
|
+
storedCountryValue: string | null | undefined
|
|
200
|
+
): SupportedCountryCode | null {
|
|
201
|
+
const trimmed = storedCountryValue?.trim();
|
|
202
|
+
if (!trimmed) return null;
|
|
203
|
+
|
|
204
|
+
const byCode = CountryOptions.find(
|
|
205
|
+
(option) => option.id.toLowerCase() === trimmed.toLowerCase()
|
|
206
|
+
);
|
|
207
|
+
if (byCode) return byCode.id;
|
|
208
|
+
|
|
209
|
+
const byName = CountryOptions.find(
|
|
210
|
+
(option) => option.name.toLowerCase() === trimmed.toLowerCase()
|
|
211
|
+
);
|
|
212
|
+
if (byName) return byName.id;
|
|
213
|
+
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function getCountryRule(
|
|
218
|
+
storedCountryValue: string | null | undefined
|
|
219
|
+
): CountryRule {
|
|
220
|
+
const code = resolveCountryCode(storedCountryValue);
|
|
221
|
+
if (!code) return FALLBACK_RULE;
|
|
222
|
+
return COUNTRY_RULES[code];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
function digitsOnly(value: string): string {
|
|
226
|
+
return value.replace(/\D/g, "");
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export function applyZipInput(
|
|
230
|
+
country: string | null | undefined,
|
|
231
|
+
typedValue: string
|
|
232
|
+
): string {
|
|
233
|
+
const code = resolveCountryCode(country);
|
|
234
|
+
if (!code) return typedValue;
|
|
235
|
+
|
|
236
|
+
switch (code) {
|
|
237
|
+
case "BRA": {
|
|
238
|
+
const digits = digitsOnly(typedValue).slice(0, 8);
|
|
239
|
+
if (digits.length > 5) {
|
|
240
|
+
return `${digits.slice(0, 5)}-${digits.slice(5)}`;
|
|
241
|
+
}
|
|
242
|
+
return digits;
|
|
243
|
+
}
|
|
244
|
+
case "USA": {
|
|
245
|
+
const digits = digitsOnly(typedValue).slice(0, 9);
|
|
246
|
+
if (digits.length > 5) {
|
|
247
|
+
return `${digits.slice(0, 5)}-${digits.slice(5)}`;
|
|
248
|
+
}
|
|
249
|
+
return digits;
|
|
250
|
+
}
|
|
251
|
+
case "CAN": {
|
|
252
|
+
const raw = typedValue
|
|
253
|
+
.replace(/[^A-Za-z0-9]/g, "")
|
|
254
|
+
.toUpperCase()
|
|
255
|
+
.slice(0, 6);
|
|
256
|
+
if (raw.length > 3) {
|
|
257
|
+
return `${raw.slice(0, 3)} ${raw.slice(3)}`;
|
|
258
|
+
}
|
|
259
|
+
return raw;
|
|
260
|
+
}
|
|
261
|
+
case "MEX":
|
|
262
|
+
case "FRA":
|
|
263
|
+
return digitsOnly(typedValue).slice(0, 5);
|
|
264
|
+
case "COL":
|
|
265
|
+
return digitsOnly(typedValue).slice(0, 6);
|
|
266
|
+
default:
|
|
267
|
+
return typedValue;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function applyNumberInput(
|
|
272
|
+
country: string | null | undefined,
|
|
273
|
+
typedValue: string
|
|
274
|
+
): string {
|
|
275
|
+
const rule = getCountryRule(country);
|
|
276
|
+
if (rule.streetNumber.digitsOnly) {
|
|
277
|
+
return digitsOnly(typedValue);
|
|
278
|
+
}
|
|
279
|
+
return typedValue;
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
export function fieldsToClearOnCountryChange(
|
|
283
|
+
previousCountry: string | null | undefined,
|
|
284
|
+
nextCountry: string | null | undefined,
|
|
285
|
+
currentStreetNumber: string | null | undefined
|
|
286
|
+
): ClearableAddressField[] {
|
|
287
|
+
const previousRule = getCountryRule(previousCountry);
|
|
288
|
+
const nextRule = getCountryRule(nextCountry);
|
|
289
|
+
const fields: ClearableAddressField[] = ["state"];
|
|
290
|
+
|
|
291
|
+
const previousDefault = previousRule.streetNumber.defaultValue;
|
|
292
|
+
const shouldClearNumber =
|
|
293
|
+
!nextRule.streetNumber.visible ||
|
|
294
|
+
(previousDefault != null &&
|
|
295
|
+
(currentStreetNumber ?? "").trim() === previousDefault);
|
|
296
|
+
|
|
297
|
+
if (shouldClearNumber) {
|
|
298
|
+
fields.push("streetNumber");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
if (!nextRule.neighborhood.visible) {
|
|
302
|
+
fields.push("neighborhood");
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return fields;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
function isBlank(value: string | null | undefined): boolean {
|
|
309
|
+
return !value?.trim();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
export function collectAddressIssues(
|
|
313
|
+
values: AddressFormValues,
|
|
314
|
+
t: (key: string) => string
|
|
315
|
+
): AddressIssue[] {
|
|
316
|
+
const issues: AddressIssue[] = [];
|
|
317
|
+
const countryKey = values.countryCode || values.country;
|
|
318
|
+
const rule = getCountryRule(countryKey);
|
|
319
|
+
|
|
320
|
+
const pushRequired = (path: string, key: string) => {
|
|
321
|
+
issues.push({ path, message: t(key) });
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
if (isBlank(values.name)) {
|
|
325
|
+
pushRequired("name", "addresses.validation.addressNameRequired");
|
|
326
|
+
}
|
|
327
|
+
if (isBlank(values.city)) {
|
|
328
|
+
pushRequired("city", "addresses.validation.cityRequired");
|
|
329
|
+
}
|
|
330
|
+
if (isBlank(values.country) && isBlank(values.countryCode)) {
|
|
331
|
+
pushRequired("country", "addresses.validation.countryRequired");
|
|
332
|
+
}
|
|
333
|
+
if (!values.types?.length) {
|
|
334
|
+
pushRequired("types", "addresses.validation.addressTypeRequired");
|
|
335
|
+
}
|
|
336
|
+
if (isBlank(values.streetAddress)) {
|
|
337
|
+
pushRequired("streetAddress", "addresses.validation.streetAddressRequired");
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
if (rule.state.visible && rule.state.required && isBlank(values.state)) {
|
|
341
|
+
pushRequired("state", "addresses.validation.stateRequired");
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
if (rule.zip.required && isBlank(values.zip)) {
|
|
345
|
+
pushRequired("zip", "addresses.validation.postalCodeRequired");
|
|
346
|
+
} else if (
|
|
347
|
+
!isBlank(values.zip) &&
|
|
348
|
+
rule.zip.pattern &&
|
|
349
|
+
!rule.zip.pattern.test(values.zip!.trim())
|
|
350
|
+
) {
|
|
351
|
+
issues.push({
|
|
352
|
+
path: "zip",
|
|
353
|
+
message: t("addresses.validation.postalCodeInvalid"),
|
|
354
|
+
});
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
if (
|
|
358
|
+
rule.streetNumber.visible &&
|
|
359
|
+
rule.streetNumber.required &&
|
|
360
|
+
isBlank(values.streetNumber)
|
|
361
|
+
) {
|
|
362
|
+
pushRequired("streetNumber", "addresses.validation.numberRequired");
|
|
363
|
+
} else if (
|
|
364
|
+
rule.streetNumber.digitsOnly &&
|
|
365
|
+
!isBlank(values.streetNumber) &&
|
|
366
|
+
/\D/.test(values.streetNumber!.trim())
|
|
367
|
+
) {
|
|
368
|
+
issues.push({
|
|
369
|
+
path: "streetNumber",
|
|
370
|
+
message: t("addresses.validation.numberInvalid"),
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
if (
|
|
375
|
+
rule.neighborhood.visible &&
|
|
376
|
+
rule.neighborhood.required &&
|
|
377
|
+
isBlank(values.neighborhood)
|
|
378
|
+
) {
|
|
379
|
+
pushRequired(
|
|
380
|
+
"neighborhood",
|
|
381
|
+
rule.neighborhood.labelKey === COLONIA_LABEL
|
|
382
|
+
? "addresses.validation.coloniaRequired"
|
|
383
|
+
: "addresses.validation.neighborhoodRequired"
|
|
384
|
+
);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
return issues;
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
export function applyHiddenNumberDefault(
|
|
391
|
+
country: string | null | undefined,
|
|
392
|
+
currentStreetNumber: string | null | undefined
|
|
393
|
+
): string {
|
|
394
|
+
const rule = getCountryRule(country);
|
|
395
|
+
if (rule.streetNumber.defaultValue && isBlank(currentStreetNumber)) {
|
|
396
|
+
return rule.streetNumber.defaultValue;
|
|
397
|
+
}
|
|
398
|
+
return currentStreetNumber ?? "";
|
|
399
|
+
}
|
package/src/features/credit-cards/components/CreateCreditCardDrawer/CreateCreditCardDrawer.tsx
CHANGED
|
@@ -6,6 +6,7 @@ import { useRouter } from "next/router";
|
|
|
6
6
|
import { useUI } from "@faststore/ui";
|
|
7
7
|
|
|
8
8
|
import { AddressForm } from "../../../addresses/components";
|
|
9
|
+
import { collectAddressIssues } from "../../../addresses/utils";
|
|
9
10
|
import {
|
|
10
11
|
type BasicDrawerProps,
|
|
11
12
|
BasicDrawer,
|
|
@@ -178,20 +179,7 @@ export const CreateCreditCardDrawer = ({
|
|
|
178
179
|
};
|
|
179
180
|
|
|
180
181
|
const hasMinimumAddressInformation = () => {
|
|
181
|
-
|
|
182
|
-
const hasRequiredFields =
|
|
183
|
-
address.name &&
|
|
184
|
-
address.streetAddress &&
|
|
185
|
-
address.country &&
|
|
186
|
-
address.zip &&
|
|
187
|
-
address.state &&
|
|
188
|
-
address.city &&
|
|
189
|
-
address.types.length > 0;
|
|
190
|
-
|
|
191
|
-
const hasBrazilianNumber =
|
|
192
|
-
!isBrazil || (isBrazil && address.streetNumber?.trim());
|
|
193
|
-
|
|
194
|
-
return (hasRequiredFields && hasBrazilianNumber) || useExistingAddress;
|
|
182
|
+
return useExistingAddress || collectAddressIssues(address, t).length === 0;
|
|
195
183
|
};
|
|
196
184
|
|
|
197
185
|
const hasCardInformation = () => {
|
|
@@ -1,76 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
applyZipInput,
|
|
3
|
+
getCountryRule,
|
|
4
|
+
} from "../../addresses/utils/countryRules";
|
|
2
5
|
|
|
3
6
|
export function maskPostalCode(postalCode: string, country?: string) {
|
|
4
|
-
if (postalCode)
|
|
5
|
-
|
|
6
|
-
case "USA":
|
|
7
|
-
return mask(postalCode, "99999").substring(0, 5);
|
|
8
|
-
case "MEX":
|
|
9
|
-
return mask(postalCode, "99999").substring(0, 5);
|
|
10
|
-
case "BRA":
|
|
11
|
-
return mask(postalCode, "99999-999").substring(0, 9);
|
|
12
|
-
case "CAN":
|
|
13
|
-
return formatCanadaPostalCode(postalCode);
|
|
14
|
-
default:
|
|
15
|
-
return postalCode;
|
|
16
|
-
}
|
|
17
|
-
} else {
|
|
18
|
-
return postalCode;
|
|
19
|
-
}
|
|
7
|
+
if (!postalCode) return postalCode;
|
|
8
|
+
return applyZipInput(country, postalCode);
|
|
20
9
|
}
|
|
21
10
|
|
|
22
11
|
export function validatePostalCode(postalCode: string, country?: string) {
|
|
23
|
-
if (postalCode)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
case "MEX":
|
|
28
|
-
return /^[0-9]{5}$/.test(postalCode.replace("-", ""));
|
|
29
|
-
case "BRA":
|
|
30
|
-
return /^[0-9]{5}-?[0-9]{3}$/.test(postalCode.replace("-", ""));
|
|
31
|
-
case "CAN":
|
|
32
|
-
return validateCanadianPostalCode(postalCode);
|
|
33
|
-
default:
|
|
34
|
-
return true;
|
|
35
|
-
}
|
|
36
|
-
} else {
|
|
37
|
-
return true;
|
|
38
|
-
}
|
|
12
|
+
if (!postalCode) return true;
|
|
13
|
+
const rule = getCountryRule(country);
|
|
14
|
+
if (!rule.zip.pattern) return true;
|
|
15
|
+
return rule.zip.pattern.test(postalCode.trim());
|
|
39
16
|
}
|
|
40
|
-
|
|
41
|
-
const formatCanadaPostalCode = (postalCode: string) => {
|
|
42
|
-
postalCode = postalCode.replace(/[^\w]+/g, "").toUpperCase();
|
|
43
|
-
|
|
44
|
-
if (postalCode.length === 6) {
|
|
45
|
-
return `${postalCode.substring(0, 3)} ${postalCode.substring(
|
|
46
|
-
3
|
|
47
|
-
)}`.toUpperCase();
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
return postalCode;
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
const unformatPostalCode = (postalCode: string) => {
|
|
54
|
-
if (!postalCode || typeof postalCode !== "string") return "";
|
|
55
|
-
|
|
56
|
-
postalCode = postalCode.replace(/[^\w]+/g, "").toUpperCase();
|
|
57
|
-
|
|
58
|
-
return postalCode;
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
const validateCanadianPostalCode = (postalCode: string) => {
|
|
62
|
-
if (!postalCode) return false;
|
|
63
|
-
|
|
64
|
-
postalCode = unformatPostalCode(postalCode);
|
|
65
|
-
const postalCodePattern = /^[A-z][0-9][A-z][0-9][A-z][0-9]$/;
|
|
66
|
-
|
|
67
|
-
if (
|
|
68
|
-
postalCode.length > 5 &&
|
|
69
|
-
postalCode.length < 8 &&
|
|
70
|
-
postalCodePattern.test(postalCode)
|
|
71
|
-
) {
|
|
72
|
-
return true;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return false;
|
|
76
|
-
};
|