-
Notifications
You must be signed in to change notification settings - Fork 3k
/
AdditionalDetailsStep.tsx
244 lines (228 loc) · 11.3 KB
/
AdditionalDetailsStep.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
import {subYears} from 'date-fns';
import React from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import DatePicker from '@components/DatePicker';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import TextInput from '@components/TextInput';
import TextLink from '@components/TextLink';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils';
import {parsePhoneNumber} from '@libs/PhoneNumber';
import * as ValidationUtils from '@libs/ValidationUtils';
import AddressFormFields from '@pages/ReimbursementAccount/AddressFormFields';
import * as Wallet from '@userActions/Wallet';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import INPUT_IDS from '@src/types/form/AdditionalDetailStepForm';
import type {WalletAdditionalDetails} from '@src/types/onyx';
import IdologyQuestions from './IdologyQuestions';
const DEFAULT_WALLET_ADDITIONAL_DETAILS = {
errorFields: {},
isLoading: false,
errors: {},
questions: [],
idNumber: '',
errorCode: '',
};
type AdditionalDetailsStepOnyxProps = {
/** Stores additional information about the additional details step e.g. loading state and errors with fields */
walletAdditionalDetails: OnyxEntry<WalletAdditionalDetails>;
};
type AdditionalDetailsStepProps = AdditionalDetailsStepOnyxProps & WithCurrentUserPersonalDetailsProps;
const fieldNameTranslationKeys = {
legalFirstName: 'additionalDetailsStep.legalFirstNameLabel',
legalLastName: 'additionalDetailsStep.legalLastNameLabel',
addressStreet: 'common.personalAddress',
phoneNumber: 'common.phoneNumber',
dob: 'common.dob',
ssn: 'common.ssnLast4',
ssnFull9: 'common.ssnFull9',
} as const;
const STEP_FIELDS = [
INPUT_IDS.LEGAL_FIRST_NAME,
INPUT_IDS.LEGAL_LAST_NAME,
INPUT_IDS.ADDRESS_STREET,
INPUT_IDS.ADDRESS_CITY,
INPUT_IDS.ADDRESS_ZIP_CODE,
INPUT_IDS.PHONE_NUMBER,
INPUT_IDS.DOB,
INPUT_IDS.ADDRESS_STATE,
INPUT_IDS.SSN,
];
function AdditionalDetailsStep({walletAdditionalDetails = DEFAULT_WALLET_ADDITIONAL_DETAILS, currentUserPersonalDetails}: AdditionalDetailsStepProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
const currentDate = new Date();
const minDate = subYears(currentDate, CONST.DATE_BIRTH.MAX_AGE);
const maxDate = subYears(currentDate, CONST.DATE_BIRTH.MIN_AGE_FOR_PAYMENT);
const shouldAskForFullSSN = walletAdditionalDetails?.errorCode === CONST.WALLET.ERROR.SSN;
const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WALLET_ADDITIONAL_DETAILS>): FormInputErrors<typeof ONYXKEYS.FORMS.WALLET_ADDITIONAL_DETAILS> => {
const errors = ValidationUtils.getFieldRequiredErrors(values, STEP_FIELDS);
if (values.dob) {
if (!ValidationUtils.isValidPastDate(values.dob) || !ValidationUtils.meetsMaximumAgeRequirement(values.dob)) {
errors.dob = translate('bankAccount.error.dob');
} else if (!ValidationUtils.meetsMinimumAgeRequirement(values.dob)) {
errors.dob = translate('bankAccount.error.age');
}
}
if (values.addressStreet && !ValidationUtils.isValidAddress(values.addressStreet)) {
errors.addressStreet = translate('bankAccount.error.addressStreet');
}
if (values.addressZipCode && !ValidationUtils.isValidZipCode(values.addressZipCode)) {
errors.addressZipCode = translate('bankAccount.error.zipCode');
}
if (values.phoneNumber && !ValidationUtils.isValidUSPhone(values.phoneNumber, true)) {
errors.phoneNumber = translate('bankAccount.error.phoneNumber');
}
// walletAdditionalDetails stores errors returned by the server. If the server returns an SSN error
// then the user needs to provide the full 9 digit SSN.
if (walletAdditionalDetails?.errorCode === CONST.WALLET.ERROR.SSN) {
if (values.ssn && !ValidationUtils.isValidSSNFullNine(values.ssn)) {
errors.ssn = translate('additionalDetailsStep.ssnFull9Error');
}
} else if (values.ssn && !ValidationUtils.isValidSSNLastFour(values.ssn)) {
errors.ssn = translate('bankAccount.error.ssnLast4');
}
return errors;
};
const activateWallet = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.WALLET_ADDITIONAL_DETAILS>) => {
const personalDetails = {
phoneNumber: (values.phoneNumber && parsePhoneNumber(values.phoneNumber, {regionCode: CONST.COUNTRY.US}).number?.significant) ?? '',
legalFirstName: values.legalFirstName ?? '',
legalLastName: values.legalLastName ?? '',
addressStreet: values.addressStreet ?? '',
addressCity: values.addressCity ?? '',
addressState: values.addressState ?? '',
addressZip: values.addressZipCode ?? '',
dob: values.dob ?? '',
ssn: values.ssn ?? '',
};
// Attempt to set the personal details
Wallet.updatePersonalDetails(personalDetails);
};
if (walletAdditionalDetails?.questions && walletAdditionalDetails.questions.length > 0) {
return (
<ScreenWrapper
shouldShowOfflineIndicator={false}
style={[styles.flex1, styles.pt0]}
keyboardAvoidingViewBehavior="height"
testID={AdditionalDetailsStep.displayName}
>
<HeaderWithBackButton
title={translate('additionalDetailsStep.headerTitle')}
onBackButtonPress={() => Wallet.setAdditionalDetailsQuestions(null)}
/>
<IdologyQuestions
questions={walletAdditionalDetails.questions}
idNumber={walletAdditionalDetails.idNumber ?? ''}
/>
</ScreenWrapper>
);
}
return (
<>
<HeaderWithBackButton title={translate('additionalDetailsStep.headerTitle')} />
<View style={[styles.flex1]}>
<View style={[styles.ph5]}>
<Text style={styles.mb3}>{translate('additionalDetailsStep.helpText')}</Text>
<TextLink
style={styles.mb3}
href={CONST.HELP_LINK_URL}
>
{translate('additionalDetailsStep.helpLink')}
</TextLink>
</View>
<FormProvider
formID={ONYXKEYS.FORMS.WALLET_ADDITIONAL_DETAILS}
validate={validate}
onSubmit={activateWallet}
scrollContextEnabled
submitButtonText={translate('common.saveAndContinue')}
style={[styles.mh5, styles.flexGrow1]}
>
<InputWrapper
InputComponent={TextInput}
inputID="legalFirstName"
containerStyles={[styles.mt4]}
label={translate(fieldNameTranslationKeys.legalFirstName)}
accessibilityLabel={translate(fieldNameTranslationKeys.legalFirstName)}
role={CONST.ROLE.PRESENTATION}
defaultValue={PersonalDetailsUtils.extractFirstAndLastNameFromAvailableDetails(currentUserPersonalDetails).firstName}
shouldSaveDraft
/>
<InputWrapper
InputComponent={TextInput}
inputID="legalLastName"
containerStyles={[styles.mt4]}
label={translate(fieldNameTranslationKeys.legalLastName)}
accessibilityLabel={translate(fieldNameTranslationKeys.legalLastName)}
role={CONST.ROLE.PRESENTATION}
defaultValue={PersonalDetailsUtils.extractFirstAndLastNameFromAvailableDetails(currentUserPersonalDetails).lastName}
shouldSaveDraft
/>
<AddressFormFields
inputKeys={{
street: 'addressStreet',
city: 'addressCity',
state: 'addressState',
zipCode: 'addressZipCode',
}}
streetTranslationKey={fieldNameTranslationKeys.addressStreet}
shouldSaveDraft
/>
<InputWrapper
InputComponent={TextInput}
inputID="phoneNumber"
containerStyles={[styles.mt4]}
inputMode={CONST.INPUT_MODE.TEL}
label={translate(fieldNameTranslationKeys.phoneNumber)}
accessibilityLabel={translate(fieldNameTranslationKeys.phoneNumber)}
role={CONST.ROLE.PRESENTATION}
defaultValue={currentUserPersonalDetails.phoneNumber}
placeholder={translate('common.phoneNumberPlaceholder')}
shouldSaveDraft
/>
<InputWrapper
InputComponent={DatePicker}
inputID="dob"
containerStyles={[styles.mt4]}
label={translate(fieldNameTranslationKeys.dob)}
placeholder={translate('common.dob')}
minDate={minDate}
maxDate={maxDate}
shouldSaveDraft
/>
<InputWrapper
InputComponent={TextInput}
inputID="ssn"
containerStyles={[styles.mt4]}
label={translate(fieldNameTranslationKeys[shouldAskForFullSSN ? 'ssnFull9' : 'ssn'])}
accessibilityLabel={translate(fieldNameTranslationKeys[shouldAskForFullSSN ? 'ssnFull9' : 'ssn'])}
role={CONST.ROLE.PRESENTATION}
maxLength={shouldAskForFullSSN ? 9 : 4}
inputMode={CONST.INPUT_MODE.NUMERIC}
/>
</FormProvider>
</View>
</>
);
}
AdditionalDetailsStep.displayName = 'AdditionalDetailsStep';
export default withCurrentUserPersonalDetails(
withOnyx<AdditionalDetailsStepProps, AdditionalDetailsStepOnyxProps>({
// @ts-expect-error: ONYXKEYS.WALLET_ADDITIONAL_DETAILS is conflicting with ONYXKEYS.FORMS.WALLET_ADDITIONAL_DETAILS
walletAdditionalDetails: {
key: ONYXKEYS.WALLET_ADDITIONAL_DETAILS,
},
})(AdditionalDetailsStep),
);