-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathReimbursementAccountPage.js
261 lines (239 loc) · 9.81 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
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 Log from '../../libs/Log';
import ScreenWrapper from '../../components/ScreenWrapper';
import * as BankAccounts from '../../libs/actions/BankAccounts';
import ONYXKEYS from '../../ONYXKEYS';
import ReimbursementAccountLoadingIndicator from '../../components/ReimbursementAccountLoadingIndicator';
import Permissions from '../../libs/Permissions';
import Navigation from '../../libs/Navigation/Navigation';
import CONST from '../../CONST';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import styles from '../../styles/styles';
import KeyboardAvoidingView from '../../components/KeyboardAvoidingView';
import getPlaidOAuthReceivedRedirectURI from '../../libs/getPlaidOAuthReceivedRedirectURI';
import Text from '../../components/Text';
// Steps
import BankAccountStep from './BankAccountStep';
import CompanyStep from './CompanyStep';
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 reimbursementAccountPropTypes from './reimbursementAccountPropTypes';
import WorkspaceResetBankAccountModal from '../workspace/WorkspaceResetBankAccountModal';
const propTypes = {
/** List of betas */
betas: PropTypes.arrayOf(PropTypes.string).isRequired,
/** ACH data for the withdrawal account actively being set up */
reimbursementAccount: reimbursementAccountPropTypes,
/** 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: {
loading: true,
},
route: {
params: {
stepToOpen: '',
},
},
};
class ReimbursementAccountPage extends React.Component {
componentDidMount() {
// We can specify a step to navigate to by using route params when the component mounts.
const stepToOpen = this.getStepToOpenFromRouteParams();
// If we are trying to navigate to `/bank-account/new` and we already have a bank account then don't allow returning to `/new`
BankAccounts.fetchFreePlanVerifiedBankAccount(stepToOpen !== CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT ? stepToOpen : '');
}
componentDidUpdate(prevProps) {
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)));
}
/**
* @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';
}
}
render() {
if (!Permissions.canUseFreePlan(this.props.betas)) {
Log.info('Not showing new bank account page because user is not on free plan beta');
Navigation.dismissModal();
return null;
}
// 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.loading) {
const isSubmittingVerificationsData = _.contains([
CONST.BANK_ACCOUNT.STEP.COMPANY,
CONST.BANK_ACCOUNT.STEP.REQUESTOR,
CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT,
], currentStep);
return (
<ReimbursementAccountLoadingIndicator
isSubmittingVerificationsData={isSubmittingVerificationsData}
/>
);
}
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>
);
}
return (
<ScreenWrapper>
<KeyboardAvoidingView>
{currentStep === CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT && (
<BankAccountStep
achData={achData}
isPlaidDisabled={this.props.reimbursementAccount.isPlaidDisabled}
receivedRedirectURI={getPlaidOAuthReceivedRedirectURI()}
plaidLinkOAuthToken={this.props.plaidLinkToken}
/>
)}
{currentStep === CONST.BANK_ACCOUNT.STEP.COMPANY && (
<CompanyStep achData={achData} />
)}
{currentStep === CONST.BANK_ACCOUNT.STEP.REQUESTOR && (
<RequestorStep achData={achData} />
)}
{currentStep === CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT && (
<ACHContractStep companyName={achData.companyName} />
)}
{currentStep === CONST.BANK_ACCOUNT.STEP.VALIDATION && (
<ValidationStep />
)}
{currentStep === CONST.BANK_ACCOUNT.STEP.ENABLE && (
<EnableStep
achData={this.props.reimbursementAccount.achData}
/>
)}
<WorkspaceResetBankAccountModal />
</KeyboardAvoidingView>
</ScreenWrapper>
);
}
}
ReimbursementAccountPage.propTypes = propTypes;
ReimbursementAccountPage.defaultProps = defaultProps;
export default compose(
withOnyx({
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
session: {
key: ONYXKEYS.SESSION,
},
betas: {
key: ONYXKEYS.BETAS,
},
plaidLinkToken: {
key: ONYXKEYS.PLAID_LINK_TOKEN,
},
}),
withLocalize,
)(ReimbursementAccountPage);