-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathRequestorStep.js
311 lines (287 loc) · 14.1 KB
/
RequestorStep.js
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
import React from 'react';
import lodashGet from 'lodash/get';
import {ScrollView, View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import moment from 'moment';
import PropTypes from 'prop-types';
import styles from '../../styles/styles';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import CONST from '../../CONST';
import TextLink from '../../components/TextLink';
import Navigation from '../../libs/Navigation/Navigation';
import CheckboxWithLabel from '../../components/CheckboxWithLabel';
import Text from '../../components/Text';
import * as BankAccounts from '../../libs/actions/BankAccounts';
import IdentityForm from './IdentityForm';
import * as ValidationUtils from '../../libs/ValidationUtils';
import Onfido from '../../components/Onfido';
import compose from '../../libs/compose';
import ONYXKEYS from '../../ONYXKEYS';
import * as ReimbursementAccountUtils from '../../libs/ReimbursementAccountUtils';
import Growl from '../../libs/Growl';
import reimbursementAccountPropTypes from './reimbursementAccountPropTypes';
import ReimbursementAccountForm from './ReimbursementAccountForm';
import * as Link from '../../libs/actions/Link';
const propTypes = {
/** Bank account currently in setup */
reimbursementAccount: reimbursementAccountPropTypes.isRequired,
onfidoToken: PropTypes.string,
...withLocalizePropTypes,
};
const defaultProps = {
onfidoToken: '',
};
class RequestorStep extends React.Component {
constructor(props) {
super(props);
this.submit = this.submit.bind(this);
this.clearErrorsAndSetValues = this.clearErrorsAndSetValues.bind(this);
this.state = {
firstName: ReimbursementAccountUtils.getDefaultStateForField(props, 'firstName'),
lastName: ReimbursementAccountUtils.getDefaultStateForField(props, 'lastName'),
requestorAddressStreet: ReimbursementAccountUtils.getDefaultStateForField(props, 'requestorAddressStreet'),
requestorAddressCity: ReimbursementAccountUtils.getDefaultStateForField(props, 'requestorAddressCity'),
requestorAddressState: ReimbursementAccountUtils.getDefaultStateForField(props, 'requestorAddressState'),
requestorAddressZipCode: ReimbursementAccountUtils.getDefaultStateForField(props, 'requestorAddressZipCode'),
dob: ReimbursementAccountUtils.getDefaultStateForField(props, 'dob'),
ssnLast4: ReimbursementAccountUtils.getDefaultStateForField(props, 'ssnLast4'),
isControllingOfficer: ReimbursementAccountUtils.getDefaultStateForField(props, 'isControllingOfficer', false),
onfidoData: lodashGet(props, ['reimbursementAccount', 'achData', 'onfidoData'], ''),
isOnfidoSetupComplete: lodashGet(props, ['achData', 'isOnfidoSetupComplete'], false),
};
// Required fields not validated by `validateIdentity`
this.requiredFields = [
'isControllingOfficer',
];
// Map a field to the key of the error's translation
this.errorTranslationKeys = {
firstName: 'bankAccount.error.firstName',
lastName: 'bankAccount.error.lastName',
isControllingOfficer: 'requestorStep.isControllingOfficerError',
};
this.clearError = inputKey => ReimbursementAccountUtils.clearError(this.props, inputKey);
this.clearErrors = inputKeys => ReimbursementAccountUtils.clearErrors(this.props, inputKeys);
this.getErrors = () => ReimbursementAccountUtils.getErrors(this.props);
}
/**
* Clear the errors associated to keys in values if found and store the new values in the state.
*
* @param {Object} values
*/
clearErrorsAndSetValues(values) {
const renamedFields = {
street: 'requestorAddressStreet',
city: 'requestorAddressCity',
state: 'requestorAddressState',
zipCode: 'requestorAddressZipCode',
};
const newState = {};
_.each(values, (value, inputKey) => {
const renamedInputKey = lodashGet(renamedFields, inputKey, inputKey);
newState[renamedInputKey] = value;
});
this.setState(newState);
BankAccounts.updateReimbursementAccountDraft(newState);
// Prepare inputKeys for clearing errors
const inputKeys = _.keys(values);
// dob field has multiple validations/errors, we are handling it temporarily like this.
if (_.contains(inputKeys, 'dob')) {
inputKeys.push('dobAge');
}
this.clearErrors(inputKeys);
}
/**
* @returns {Boolean}
*/
validate() {
const errors = ValidationUtils.validateIdentity({
firstName: this.state.firstName,
lastName: this.state.lastName,
street: this.state.requestorAddressStreet,
state: this.state.requestorAddressState,
city: this.state.requestorAddressCity,
zipCode: this.state.requestorAddressZipCode,
dob: this.state.dob,
ssnLast4: this.state.ssnLast4,
});
_.each(this.requiredFields, (inputKey) => {
if (ValidationUtils.isRequiredFulfilled(this.state[inputKey])) {
return;
}
errors[inputKey] = true;
});
if (_.size(errors)) {
BankAccounts.setBankAccountFormValidationErrors(errors);
return false;
}
return true;
}
submit() {
if (!this.validate()) {
return;
}
const payload = {
bankAccountID: ReimbursementAccountUtils.getDefaultStateForField(this.props, 'bankAccountID', 0),
...this.state,
dob: moment(this.state.dob).format(CONST.DATE.MOMENT_FORMAT_STRING),
};
BankAccounts.updatePersonalInformationForBankAccount(payload);
}
submitOnfidoVerification() {
if (!this.validate()) {
return;
}
const payload = {
bankAccountID: ReimbursementAccountUtils.getDefaultStateForField(this.props, 'bankAccountID', 0),
...this.state,
dob: moment(this.state.dob).format(CONST.DATE.MOMENT_FORMAT_STRING),
};
BankAccounts.setupWithdrawalAccount(payload);
}
render() {
const achData = this.props.reimbursementAccount.achData;
const shouldShowOnfido = achData.useOnfido && this.props.onfidoToken && !this.state.isOnfidoSetupComplete;
return (
<>
<HeaderWithCloseButton
title={this.props.translate('requestorStep.headerTitle')}
stepCounter={{step: 3, total: 5}}
shouldShowGetAssistanceButton
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_BANK_ACCOUNT}
shouldShowBackButton
onBackButtonPress={() => {
if (shouldShowOnfido) {
BankAccounts.clearOnfidoToken();
} else {
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.COMPANY);
}
}}
onCloseButtonPress={Navigation.dismissModal}
/>
{shouldShowOnfido ? (
<ScrollView contentContainerStyle={styles.flex1}>
<Onfido
sdkToken={this.props.onfidoToken}
onUserExit={() => {
// We're taking the user back to the company step. They will need to come back to the requestor step to make the Onfido flow appear again.
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.COMPANY);
}}
onError={() => {
// In case of any unexpected error we log it to the server, show a growl, and return the user back to the company step so they can try again.
Growl.error(this.props.translate('onfidoStep.genericError'), 10000);
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.COMPANY);
}}
onSuccess={(onfidoData) => {
this.setState({
onfidoData,
isOnfidoSetupComplete: true,
}, this.submitOnfidoVerification);
}}
/>
</ScrollView>
) : (
<ReimbursementAccountForm
onSubmit={this.submit}
>
<Text>{this.props.translate('requestorStep.subtitle')}</Text>
<View style={[styles.mb5, styles.mt1, styles.dFlex, styles.flexRow]}>
<TextLink
style={[styles.textMicro]}
// eslint-disable-next-line max-len
href="https://community.expensify.com/discussion/6983/faq-why-do-i-need-to-provide-personal-documentation-when-setting-up-updating-my-bank-account"
>
{`${this.props.translate('requestorStep.learnMore')}`}
</TextLink>
<Text style={[styles.textMicroSupporting]}>{' | '}</Text>
<TextLink
style={[styles.textMicro, styles.textLink]}
// eslint-disable-next-line max-len
href="https://community.expensify.com/discussion/5677/deep-dive-security-how-expensify-protects-your-information"
>
{`${this.props.translate('requestorStep.isMyDataSafe')}`}
</TextLink>
</View>
<IdentityForm
onFieldChange={this.clearErrorsAndSetValues}
values={{
firstName: this.state.firstName,
lastName: this.state.lastName,
street: this.state.requestorAddressStreet,
state: this.state.requestorAddressState,
city: this.state.requestorAddressCity,
zipCode: this.state.requestorAddressZipCode,
dob: this.state.dob,
ssnLast4: this.state.ssnLast4,
}}
errors={this.props.reimbursementAccount.errorFields}
/>
<CheckboxWithLabel
isChecked={this.state.isControllingOfficer}
onInputChange={() => {
this.setState((prevState) => {
const newState = {isControllingOfficer: !prevState.isControllingOfficer};
BankAccounts.updateReimbursementAccountDraft(newState);
return newState;
});
this.clearError('isControllingOfficer');
}}
LabelComponent={() => (
<View style={[styles.flex1, styles.pr1]}>
<Text>
{this.props.translate('requestorStep.isControllingOfficer')}
</Text>
</View>
)}
style={[styles.mt4]}
errorText={this.getErrors().isControllingOfficer ? this.props.translate('requestorStep.isControllingOfficerError') : ''}
/>
<Text style={[styles.mt3, styles.textMicroSupporting]}>
{this.props.translate('requestorStep.onFidoConditions')}
<Text
onPress={() => Link.openExternalLink('https://onfido.com/facial-scan-policy-and-release/')}
style={[styles.textMicro, styles.link]}
accessibilityRole="link"
>
{`${this.props.translate('onfidoStep.facialScan')}`}
</Text>
{', '}
<Text
onPress={() => Link.openExternalLink('https://onfido.com/privacy/')}
style={[styles.textMicro, styles.link]}
accessibilityRole="link"
>
{`${this.props.translate('common.privacyPolicy')}`}
</Text>
{` ${this.props.translate('common.and')} `}
<Text
onPress={() => Link.openExternalLink('https://onfido.com/terms-of-service/')}
style={[styles.textMicro, styles.link]}
accessibilityRole="link"
>
{`${this.props.translate('common.termsOfService')}`}
</Text>
</Text>
</ReimbursementAccountForm>
)}
</>
);
}
}
RequestorStep.propTypes = propTypes;
RequestorStep.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
onfidoToken: {
key: ONYXKEYS.ONFIDO_TOKEN,
},
reimbursementAccountDraft: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT,
},
}),
)(RequestorStep);