-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReimbursementAccountPage.js
415 lines (378 loc) · 16.8 KB
/
ReimbursementAccountPage.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
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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import React from 'react';
import {withOnyx} from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import {View} from 'react-native';
import PropTypes from 'prop-types';
import ScreenWrapper from '../../components/ScreenWrapper';
import * as BankAccounts from '../../libs/actions/BankAccounts';
import ONYXKEYS from '../../ONYXKEYS';
import ReimbursementAccountLoadingIndicator from '../../components/ReimbursementAccountLoadingIndicator';
import Navigation from '../../libs/Navigation/Navigation';
import CONST from '../../CONST';
import BankAccount from '../../libs/models/BankAccount';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import styles from '../../styles/styles';
import getPlaidOAuthReceivedRedirectURI from '../../libs/getPlaidOAuthReceivedRedirectURI';
import Text from '../../components/Text';
import {withNetwork} from '../../components/OnyxProvider';
import networkPropTypes from '../../components/networkPropTypes';
import BankAccountStep from './BankAccountStep';
import CompanyStep from './CompanyStep';
import ContinueBankAccountSetup from './ContinueBankAccountSetup';
import RequestorStep from './RequestorStep';
import ValidationStep from './ValidationStep';
import ACHContractStep from './ACHContractStep';
import EnableStep from './EnableStep';
import ROUTES from '../../ROUTES';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import * as ReimbursementAccountProps from './reimbursementAccountPropTypes';
import WorkspaceResetBankAccountModal from '../workspace/WorkspaceResetBankAccountModal';
import reimbursementAccountDraftPropTypes from './ReimbursementAccountDraftPropTypes';
import * as ReimbursementAccountUtils from '../../libs/ReimbursementAccountUtils';
const propTypes = {
/** Plaid SDK token to use to initialize the widget */
plaidLinkToken: PropTypes.string,
/** ACH data for the withdrawal account actively being set up */
reimbursementAccount: ReimbursementAccountProps.reimbursementAccountPropTypes,
/** The draft values of the bank account being setup */
reimbursementAccountDraft: reimbursementAccountDraftPropTypes,
/** The token required to initialize the Onfido SDK */
onfidoToken: PropTypes.string,
/** Information about the network */
network: networkPropTypes.isRequired,
/** Current session for the user */
session: PropTypes.shape({
/** User login */
email: PropTypes.string,
}).isRequired,
/** Route object from navigation */
route: PropTypes.shape({
/** Params that are passed into the route */
params: PropTypes.shape({
/** A step to navigate to if we need to drop the user into a specific point in the flow */
stepToOpen: PropTypes.string,
}),
}),
...withLocalizePropTypes,
};
const defaultProps = {
reimbursementAccount: ReimbursementAccountProps.reimbursementAccountDefaultProps,
reimbursementAccountDraft: {},
onfidoToken: '',
plaidLinkToken: '',
route: {
params: {
stepToOpen: '',
},
},
};
class ReimbursementAccountPage extends React.Component {
constructor(props) {
super(props);
this.continue = this.continue.bind(this);
this.getDefaultStateForField = this.getDefaultStateForField.bind(this);
this.goBack = this.goBack.bind(this);
const achData = lodashGet(this.props.reimbursementAccount, 'achData', {});
const hasInProgressVBBA = achData.bankAccountID && achData.state !== BankAccount.STATE.OPEN && achData.state !== BankAccount.STATE.LOCKED;
this.state = {
shouldHideContinueSetupButton: !hasInProgressVBBA,
};
}
componentDidMount() {
this.fetchData();
}
componentDidUpdate(prevProps) {
if (prevProps.network.isOffline && !this.props.network.isOffline) {
this.fetchData();
}
const currentStep = lodashGet(this.props.reimbursementAccount, 'achData.currentStep') || CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
const previousStep = lodashGet(prevProps.reimbursementAccount, 'achData.currentStep') || CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
if (currentStep === previousStep) {
return;
}
// When the step changes we will navigate to update the route params. This is mostly cosmetic as we only use
// the route params when the component first mounts to jump to a specific route instead of picking up where the
// user left off in the flow.
BankAccounts.hideBankAccountErrors();
Navigation.navigate(ROUTES.getBankAccountRoute(this.getRouteForCurrentStep(currentStep)));
}
/**
* @param {String} fieldName
* @param {*} defaultValue
*
* @returns {*}
*/
getDefaultStateForField(fieldName, defaultValue = '') {
return ReimbursementAccountUtils.getDefaultStateForField(this.props.reimbursementAccountDraft, this.props.reimbursementAccount, fieldName, defaultValue);
}
/**
* We can pass stepToOpen in the URL to force which step to show.
* Mainly needed when user finished the flow in verifying state, and Ops ask them to modify some fields from a specific step.
* @returns {String}
*/
getStepToOpenFromRouteParams() {
switch (lodashGet(this.props.route, ['params', 'stepToOpen'])) {
case 'new':
return CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
case 'company':
return CONST.BANK_ACCOUNT.STEP.COMPANY;
case 'personal-information':
return CONST.BANK_ACCOUNT.STEP.REQUESTOR;
case 'contract':
return CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT;
case 'validate':
return CONST.BANK_ACCOUNT.STEP.VALIDATION;
case 'enable':
return CONST.BANK_ACCOUNT.STEP.ENABLE;
default:
return '';
}
}
/**
* @param {String} currentStep
* @returns {String}
*/
getRouteForCurrentStep(currentStep) {
switch (currentStep) {
case CONST.BANK_ACCOUNT.STEP.COMPANY:
return 'company';
case CONST.BANK_ACCOUNT.STEP.REQUESTOR:
return 'personal-information';
case CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT:
return 'contract';
case CONST.BANK_ACCOUNT.STEP.VALIDATION:
return 'validate';
case CONST.BANK_ACCOUNT.STEP.ENABLE:
return 'enable';
case CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT:
default:
return 'new';
}
}
/**
* Retrieve verified business bank account currently being set up.
* @param {boolean} ignoreLocalCurrentStep Pass true if you want the last "updated" view (from db), not the last "viewed" view (from onyx).
*/
fetchData(ignoreLocalCurrentStep) {
// Show loader right away, as optimisticData might be set only later in case multiple calls are in the queue
BankAccounts.setReimbursementAccountLoading(true);
// We can specify a step to navigate to by using route params when the component mounts.
// We want to use the same stepToOpen variable when the network state changes because we can be redirected to a different step when the account refreshes.
const stepToOpen = this.getStepToOpenFromRouteParams();
const achData = lodashGet(this.props.reimbursementAccount, 'achData', {});
const subStep = achData.subStep || '';
const localCurrentStep = achData.currentStep || '';
BankAccounts.openReimbursementAccountPage(stepToOpen, subStep, ignoreLocalCurrentStep ? '' : localCurrentStep);
}
continue() {
this.setState({
shouldHideContinueSetupButton: true,
});
this.fetchData(true);
}
goBack() {
const achData = lodashGet(this.props.reimbursementAccount, 'achData', {});
const currentStep = achData.currentStep || CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
const subStep = achData.subStep;
const shouldShowOnfido = this.props.onfidoToken && !achData.isOnfidoSetupComplete;
const hasInProgressVBBA = achData.bankAccountID && achData.state !== BankAccount.STATE.OPEN && achData.state !== BankAccount.STATE.LOCKED;
switch (currentStep) {
case CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT:
if (hasInProgressVBBA) {
this.setState({shouldHideContinueSetupButton: false});
}
if (subStep) {
BankAccounts.setBankAccountSubStep(null);
} else {
Navigation.goBack();
}
break;
case CONST.BANK_ACCOUNT.STEP.COMPANY:
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT, {subStep: CONST.BANK_ACCOUNT.SUBSTEP.MANUAL});
break;
case CONST.BANK_ACCOUNT.STEP.REQUESTOR:
if (shouldShowOnfido) {
BankAccounts.clearOnfidoToken();
} else {
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.COMPANY);
}
break;
case CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT:
BankAccounts.clearOnfidoToken();
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.REQUESTOR);
break;
case CONST.BANK_ACCOUNT.STEP.VALIDATION:
if (_.contains([BankAccount.STATE.VERIFYING, BankAccount.STATE.SETUP], achData.state)) {
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT);
} else if (achData.state === BankAccount.STATE.PENDING) {
this.setState({
shouldHideContinueSetupButton: false,
});
} else {
Navigation.goBack();
}
break;
default: Navigation.goBack();
}
}
render() {
// The SetupWithdrawalAccount flow allows us to continue the flow from various points depending on where the
// user left off. This view will refer to the achData as the single source of truth to determine which route to
// display. We can also specify a specific route to navigate to via route params when the component first
// mounts which will set the achData.currentStep after the account data is fetched and overwrite the logical
// next step.
const achData = lodashGet(this.props.reimbursementAccount, 'achData', {});
const currentStep = achData.currentStep || CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
if (this.props.reimbursementAccount.isLoading) {
const isSubmittingVerificationsData = _.contains([
CONST.BANK_ACCOUNT.STEP.COMPANY,
CONST.BANK_ACCOUNT.STEP.REQUESTOR,
CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT,
], currentStep);
return (
<ReimbursementAccountLoadingIndicator
isSubmittingVerificationsData={isSubmittingVerificationsData}
onBackButtonPress={this.goBack}
/>
);
}
if (this.props.reimbursementAccount.shouldShowResetModal && Boolean(achData.bankAccountID)) {
return (
<WorkspaceResetBankAccountModal reimbursementAccount={this.props.reimbursementAccount} />
);
}
// Show the "Continue with setup" button if a bank account setup is already in progress and no specific further step was passed in the url
if (!this.state.shouldHideContinueSetupButton
&& Boolean(achData.bankAccountID)
&& achData.state !== BankAccount.STATE.OPEN
&& achData.state !== BankAccount.STATE.LOCKED
&& (
_.contains([CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT, ''], this.getStepToOpenFromRouteParams())
|| achData.state === BankAccount.STATE.PENDING
)) {
return (
<ContinueBankAccountSetup
continue={this.continue}
startOver={() => {
this.setState({shouldHideContinueSetupButton: true});
BankAccounts.requestResetFreePlanBankAccount();
}}
/>
);
}
let errorComponent;
const userHasPhonePrimaryEmail = Str.endsWith(this.props.session.email, CONST.SMS.DOMAIN);
if (userHasPhonePrimaryEmail) {
errorComponent = (
<View style={[styles.m5]}>
<Text>{this.props.translate('bankAccount.hasPhoneLoginError')}</Text>
</View>
);
}
const throttledDate = lodashGet(this.props.reimbursementAccount, 'throttledDate');
if (throttledDate) {
errorComponent = (
<View style={[styles.m5]}>
<Text>
{this.props.translate('bankAccount.hasBeenThrottledError')}
</Text>
</View>
);
}
if (errorComponent) {
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={this.props.translate('workspace.common.bankAccount')}
onCloseButtonPress={Navigation.dismissModal}
/>
{errorComponent}
</ScreenWrapper>
);
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT) {
return (
<BankAccountStep
reimbursementAccount={this.props.reimbursementAccount}
reimbursementAccountDraft={this.props.reimbursementAccountDraft}
onBackButtonPress={this.goBack}
receivedRedirectURI={getPlaidOAuthReceivedRedirectURI()}
plaidLinkOAuthToken={this.props.plaidLinkToken}
getDefaultStateForField={this.getDefaultStateForField}
/>
);
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.COMPANY) {
return (
<CompanyStep
reimbursementAccount={this.props.reimbursementAccount}
reimbursementAccountDraft={this.props.reimbursementAccountDraft}
onBackButtonPress={this.goBack}
getDefaultStateForField={this.getDefaultStateForField}
/>
);
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.REQUESTOR) {
const shouldShowOnfido = this.props.onfidoToken && !achData.isOnfidoSetupComplete;
return (
<RequestorStep
reimbursementAccount={this.props.reimbursementAccount}
reimbursementAccountDraft={this.props.reimbursementAccountDraft}
onBackButtonPress={this.goBack}
shouldShowOnfido={Boolean(shouldShowOnfido)}
getDefaultStateForField={this.getDefaultStateForField}
/>
);
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT) {
return (
<ACHContractStep
reimbursementAccount={this.props.reimbursementAccount}
reimbursementAccountDraft={this.props.reimbursementAccountDraft}
onBackButtonPress={this.goBack}
companyName={achData.companyName}
getDefaultStateForField={this.getDefaultStateForField}
/>
);
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.VALIDATION) {
return (
<ValidationStep
reimbursementAccount={this.props.reimbursementAccount}
onBackButtonPress={this.goBack}
/>
);
}
if (currentStep === CONST.BANK_ACCOUNT.STEP.ENABLE) {
return (
<EnableStep reimbursementAccount={this.props.reimbursementAccount} />
);
}
}
}
ReimbursementAccountPage.propTypes = propTypes;
ReimbursementAccountPage.defaultProps = defaultProps;
export default compose(
withNetwork(),
withOnyx({
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
reimbursementAccountDraft: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT,
},
session: {
key: ONYXKEYS.SESSION,
},
plaidLinkToken: {
key: ONYXKEYS.PLAID_LINK_TOKEN,
},
onfidoToken: {
key: ONYXKEYS.ONFIDO_TOKEN,
},
}),
withLocalize,
)(ReimbursementAccountPage);