@velion-la/onboarding-sdk-react-native 1.3.0-beta.24 → 1.3.0-beta.25
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/lib/commonjs/i18n/strings/en.js +1 -0
- package/lib/commonjs/i18n/strings/en.js.map +1 -1
- package/lib/commonjs/i18n/strings/es.js +1 -0
- package/lib/commonjs/i18n/strings/es.js.map +1 -1
- package/lib/commonjs/steps/AddressInfoStep.js +315 -275
- package/lib/commonjs/steps/AddressInfoStep.js.map +1 -1
- package/lib/commonjs/utils/addressForm.js +77 -0
- package/lib/commonjs/utils/addressForm.js.map +1 -0
- package/lib/module/i18n/strings/en.js +1 -0
- package/lib/module/i18n/strings/en.js.map +1 -1
- package/lib/module/i18n/strings/es.js +1 -0
- package/lib/module/i18n/strings/es.js.map +1 -1
- package/lib/module/steps/AddressInfoStep.js +317 -277
- package/lib/module/steps/AddressInfoStep.js.map +1 -1
- package/lib/module/utils/addressForm.js +72 -0
- package/lib/module/utils/addressForm.js.map +1 -0
- package/lib/typescript/i18n/strings/en.d.ts.map +1 -1
- package/lib/typescript/i18n/strings/es.d.ts +1 -0
- package/lib/typescript/i18n/strings/es.d.ts.map +1 -1
- package/lib/typescript/steps/AddressInfoStep.d.ts +2 -0
- package/lib/typescript/steps/AddressInfoStep.d.ts.map +1 -1
- package/lib/typescript/utils/addressForm.d.ts +51 -0
- package/lib/typescript/utils/addressForm.d.ts.map +1 -0
- package/package.json +1 -1
- package/src/i18n/strings/en.ts +1 -0
- package/src/i18n/strings/es.ts +1 -0
- package/src/steps/AddressInfoStep.tsx +395 -298
- package/src/utils/addressForm.ts +122 -0
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Form model shared by the two ADDRESS_INFO flows: the manual form and the
|
|
3
|
+
* "edit address" section of the assisted flow's details screen (OSDKR-43), so
|
|
4
|
+
* both show the same fields and apply the same required rules.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { AddressInfoStepConfig } from '../types';
|
|
8
|
+
import type { ParsedAddressFields } from './parseGoogleAddressComponents';
|
|
9
|
+
import { keepStateIfValid, suggestState } from './subdivisions';
|
|
10
|
+
|
|
11
|
+
export type AddressFieldKey =
|
|
12
|
+
| 'country'
|
|
13
|
+
| 'state'
|
|
14
|
+
| 'city'
|
|
15
|
+
| 'district'
|
|
16
|
+
| 'neighborhood'
|
|
17
|
+
| 'street'
|
|
18
|
+
| 'number'
|
|
19
|
+
| 'zip_code'
|
|
20
|
+
| 'floor'
|
|
21
|
+
| 'apartment'
|
|
22
|
+
| 'details';
|
|
23
|
+
|
|
24
|
+
export type AddressForm = Record<AddressFieldKey, string>;
|
|
25
|
+
|
|
26
|
+
export type AddressFormErrors = Partial<Record<AddressFieldKey, string>>;
|
|
27
|
+
|
|
28
|
+
/** Which fields the step config makes required. */
|
|
29
|
+
export interface AddressRequirements {
|
|
30
|
+
state: boolean;
|
|
31
|
+
city: boolean;
|
|
32
|
+
district: boolean;
|
|
33
|
+
neighborhood: boolean;
|
|
34
|
+
street: boolean;
|
|
35
|
+
number: boolean;
|
|
36
|
+
zip: boolean;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** The `required*` messages of `address_info`, one per validated field. */
|
|
40
|
+
export interface AddressRequiredMessages {
|
|
41
|
+
requiredCountry: string;
|
|
42
|
+
requiredState: string;
|
|
43
|
+
requiredCity: string;
|
|
44
|
+
requiredDistrict: string;
|
|
45
|
+
requiredNeighborhood: string;
|
|
46
|
+
requiredStreet: string;
|
|
47
|
+
requiredNumber: string;
|
|
48
|
+
requiredZip: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Required rules from the step config: the fields the backend always expects
|
|
53
|
+
* are required unless the config says otherwise, district and neighborhood
|
|
54
|
+
* only when it asks for them.
|
|
55
|
+
*/
|
|
56
|
+
export function addressRequirements(
|
|
57
|
+
fields: AddressInfoStepConfig['fields'],
|
|
58
|
+
): AddressRequirements {
|
|
59
|
+
return {
|
|
60
|
+
state: fields?.state?.required !== false,
|
|
61
|
+
city: fields?.city?.required !== false,
|
|
62
|
+
district: fields?.district?.required === true,
|
|
63
|
+
neighborhood: fields?.neighborhood?.required === true,
|
|
64
|
+
street: fields?.street?.required !== false,
|
|
65
|
+
number: fields?.number?.required !== false,
|
|
66
|
+
zip: fields?.postal_code?.required !== false,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* The form the assisted flow starts from once the user confirmed the pin: the
|
|
72
|
+
* address the geocoder read, the state it suggests (falling back to a valid one
|
|
73
|
+
* saved by a previous visit), and the details saved by a previous visit.
|
|
74
|
+
*/
|
|
75
|
+
export function addressFormFromParsed(
|
|
76
|
+
parsed: ParsedAddressFields,
|
|
77
|
+
prefill: Record<string, unknown> | undefined,
|
|
78
|
+
prefillString: (prefill: Record<string, unknown> | undefined, key: string) => string,
|
|
79
|
+
): AddressForm {
|
|
80
|
+
return {
|
|
81
|
+
country: parsed.country,
|
|
82
|
+
// The pin the user just confirmed wins over a state saved by a previous visit.
|
|
83
|
+
state:
|
|
84
|
+
suggestState(parsed.country, parsed.state) ||
|
|
85
|
+
keepStateIfValid(parsed.country, prefillString(prefill, 'state')),
|
|
86
|
+
city: parsed.city,
|
|
87
|
+
district: parsed.district ?? '',
|
|
88
|
+
neighborhood: parsed.neighborhood ?? '',
|
|
89
|
+
street: parsed.street,
|
|
90
|
+
number: parsed.number,
|
|
91
|
+
zip_code: parsed.zip_code,
|
|
92
|
+
floor: prefillString(prefill, 'floor'),
|
|
93
|
+
apartment: prefillString(prefill, 'apartment'),
|
|
94
|
+
details: prefillString(prefill, 'details'),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Required-field errors of the form, empty when it can be submitted.
|
|
100
|
+
*
|
|
101
|
+
* @param hasStates whether the country has subdivisions to pick from; a country
|
|
102
|
+
* without them is submitted with an empty state.
|
|
103
|
+
*/
|
|
104
|
+
export function validateAddressForm(
|
|
105
|
+
form: AddressForm,
|
|
106
|
+
requirements: AddressRequirements,
|
|
107
|
+
hasStates: boolean,
|
|
108
|
+
messages: AddressRequiredMessages,
|
|
109
|
+
): AddressFormErrors {
|
|
110
|
+
const errors: AddressFormErrors = {};
|
|
111
|
+
if (!form.country) errors.country = messages.requiredCountry;
|
|
112
|
+
if (requirements.state && hasStates && !form.state) errors.state = messages.requiredState;
|
|
113
|
+
if (requirements.city && !form.city.trim()) errors.city = messages.requiredCity;
|
|
114
|
+
if (requirements.district && !form.district.trim())
|
|
115
|
+
errors.district = messages.requiredDistrict;
|
|
116
|
+
if (requirements.neighborhood && !form.neighborhood.trim())
|
|
117
|
+
errors.neighborhood = messages.requiredNeighborhood;
|
|
118
|
+
if (requirements.street && !form.street.trim()) errors.street = messages.requiredStreet;
|
|
119
|
+
if (requirements.number && !form.number.trim()) errors.number = messages.requiredNumber;
|
|
120
|
+
if (requirements.zip && !form.zip_code.trim()) errors.zip_code = messages.requiredZip;
|
|
121
|
+
return errors;
|
|
122
|
+
}
|