-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ACHContractStep.js
308 lines (282 loc) · 14.6 KB
/
ACHContractStep.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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import React from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import * as store from '../../libs/actions/ReimbursementAccount/store';
import Text from '../../components/Text';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import styles from '../../styles/styles';
import CheckboxWithLabel from '../../components/CheckboxWithLabel';
import TextLink from '../../components/TextLink';
import IdentityForm from './IdentityForm';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import * as BankAccounts from '../../libs/actions/BankAccounts';
import Navigation from '../../libs/Navigation/Navigation';
import CONST from '../../CONST';
import * as ValidationUtils from '../../libs/ValidationUtils';
import ONYXKEYS from '../../ONYXKEYS';
import compose from '../../libs/compose';
import * as ReimbursementAccountUtils from '../../libs/ReimbursementAccountUtils';
import reimbursementAccountPropTypes from './reimbursementAccountPropTypes';
import ReimbursementAccountForm from './ReimbursementAccountForm';
const propTypes = {
/** Name of the company */
companyName: PropTypes.string.isRequired,
...withLocalizePropTypes,
/** Bank account currently in setup */
// eslint-disable-next-line react/no-unused-prop-types
reimbursementAccount: reimbursementAccountPropTypes.isRequired,
};
class ACHContractStep extends React.Component {
constructor(props) {
super(props);
this.addBeneficialOwner = this.addBeneficialOwner.bind(this);
this.submit = this.submit.bind(this);
this.state = {
ownsMoreThan25Percent: ReimbursementAccountUtils.getDefaultStateForField(props, 'ownsMoreThan25Percent', false),
hasOtherBeneficialOwners: ReimbursementAccountUtils.getDefaultStateForField(props, 'hasOtherBeneficialOwners', false),
acceptTermsAndConditions: ReimbursementAccountUtils.getDefaultStateForField(props, 'acceptTermsAndConditions', false),
certifyTrueInformation: ReimbursementAccountUtils.getDefaultStateForField(props, 'certifyTrueInformation', false),
beneficialOwners: ReimbursementAccountUtils.getDefaultStateForField(props, 'beneficialOwners', []),
};
// These fields need to be filled out in order to submit the form (doesn't include IdentityForm fields)
this.requiredFields = [
'acceptTermsAndConditions',
'certifyTrueInformation',
];
// Map a field to the key of the error's translation
this.errorTranslationKeys = {
acceptTermsAndConditions: 'common.error.acceptedTerms',
certifyTrueInformation: 'beneficialOwnersStep.error.certify',
};
this.getErrors = () => ReimbursementAccountUtils.getErrors(this.props);
this.clearError = inputKey => ReimbursementAccountUtils.clearError(this.props, inputKey);
this.clearErrors = inputKeys => ReimbursementAccountUtils.clearErrors(this.props, inputKeys);
this.getErrorText = inputKey => ReimbursementAccountUtils.getErrorText(this.props, this.errorTranslationKeys, inputKey);
}
/**
* @returns {Boolean}
*/
validate() {
let beneficialOwnersErrors = [];
if (this.state.hasOtherBeneficialOwners) {
beneficialOwnersErrors = _.map(this.state.beneficialOwners, ValidationUtils.validateIdentity);
}
const errors = {};
_.each(this.requiredFields, (inputKey) => {
if (ValidationUtils.isRequiredFulfilled(this.state[inputKey])) {
return;
}
errors[inputKey] = true;
});
BankAccounts.setBankAccountFormValidationErrors({...errors, beneficialOwnersErrors});
return _.every(beneficialOwnersErrors, _.isEmpty) && _.isEmpty(errors);
}
removeBeneficialOwner(beneficialOwner) {
this.setState((prevState) => {
const beneficialOwners = _.without(prevState.beneficialOwners, beneficialOwner);
// We set 'beneficialOwners' to null first because we don't have a way yet to replace a specific property without merging it.
// We don't use the debounced function because we want to make both function calls.
BankAccounts.updateReimbursementAccountDraft({beneficialOwners: null});
BankAccounts.updateReimbursementAccountDraft({beneficialOwners});
// Clear errors
BankAccounts.setBankAccountFormValidationErrors({});
return {beneficialOwners};
});
}
addBeneficialOwner() {
this.setState(prevState => ({beneficialOwners: [...prevState.beneficialOwners, {}]}));
}
/**
* @returns {Boolean}
*/
canAddMoreBeneficialOwners() {
return _.size(this.state.beneficialOwners) < 3
|| (_.size(this.state.beneficialOwners) === 3 && !this.state.ownsMoreThan25Percent);
}
/**
* Clear the error associated to inputKey if found and store the inputKey new value in the state.
*
* @param {Integer} ownerIndex
* @param {Object} values
*/
clearErrorAndSetBeneficialOwnerValues(ownerIndex, values) {
this.setState((prevState) => {
const beneficialOwners = [...prevState.beneficialOwners];
beneficialOwners[ownerIndex] = {...beneficialOwners[ownerIndex], ...values};
BankAccounts.updateReimbursementAccountDraft({beneficialOwners});
return {beneficialOwners};
});
// 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(_.map(inputKeys, inputKey => `beneficialOwnersErrors.${ownerIndex}.${inputKey}`));
}
submit() {
if (!this.validate()) {
return;
}
const bankAccountID = lodashGet(store.getReimbursementAccountInSetup(), 'bankAccountID');
// If they did not select that there are other beneficial owners, then we need to clear out the array here. The
// reason we do it here is that if they filled out several beneficial owners, but then toggled the checkbox, we
// want the data to remain in the form so we don't lose the user input until they submit the form. This will
// prevent the data from being sent to the API
this.setState(prevState => ({
beneficialOwners: !prevState.hasOtherBeneficialOwners ? [] : prevState.beneficialOwners,
}),
() => BankAccounts.updateBeneficialOwnersForBankAccount({...this.state, beneficialOwners: JSON.stringify(this.state.beneficialOwners), bankAccountID}));
}
/**
* @param {Object} fieldName
*/
toggleCheckbox(fieldName) {
this.setState((prevState) => {
const newState = {[fieldName]: !prevState[fieldName]};
BankAccounts.updateReimbursementAccountDraft(newState);
return newState;
});
this.clearError(fieldName);
}
render() {
return (
<>
<HeaderWithCloseButton
title={this.props.translate('beneficialOwnersStep.additionalInformation')}
stepCounter={{step: 4, total: 5}}
onCloseButtonPress={Navigation.dismissModal}
onBackButtonPress={() => {
BankAccounts.clearOnfidoToken();
BankAccounts.goToWithdrawalAccountSetupStep(CONST.BANK_ACCOUNT.STEP.REQUESTOR);
}}
shouldShowGetAssistanceButton
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_BANK_ACCOUNT}
shouldShowBackButton
/>
<ReimbursementAccountForm
reimbursementAccount={this.props.reimbursementAccount}
onSubmit={this.submit}
>
<Text style={[styles.mb5]}>
<Text>{this.props.translate('beneficialOwnersStep.checkAllThatApply')}</Text>
</Text>
<CheckboxWithLabel
style={[styles.mb2]}
isChecked={this.state.ownsMoreThan25Percent}
onInputChange={() => this.toggleCheckbox('ownsMoreThan25Percent')}
LabelComponent={() => (
<Text>
{this.props.translate('beneficialOwnersStep.iOwnMoreThan25Percent')}
<Text style={[styles.textStrong]}>{this.props.companyName}</Text>
</Text>
)}
/>
<CheckboxWithLabel
style={[styles.mb2]}
isChecked={this.state.hasOtherBeneficialOwners}
onInputChange={() => {
this.setState((prevState) => {
const hasOtherBeneficialOwners = !prevState.hasOtherBeneficialOwners;
const newState = {
hasOtherBeneficialOwners,
beneficialOwners: hasOtherBeneficialOwners && _.isEmpty(prevState.beneficialOwners)
? [{}]
: prevState.beneficialOwners,
};
BankAccounts.updateReimbursementAccountDraft(newState);
return newState;
});
}}
LabelComponent={() => (
<Text>
{this.props.translate('beneficialOwnersStep.someoneOwnsMoreThan25Percent')}
<Text style={[styles.textStrong]}>{this.props.companyName}</Text>
</Text>
)}
/>
{this.state.hasOtherBeneficialOwners && (
<View style={[styles.mb2]}>
{_.map(this.state.beneficialOwners, (owner, index) => (
<View key={index} style={[styles.p5, styles.border, styles.mb2]}>
<Text style={[styles.textStrong, styles.mb2]}>
{this.props.translate('beneficialOwnersStep.additionalOwner')}
</Text>
<IdentityForm
translate={this.props.translate}
style={[styles.mb2]}
onFieldChange={values => this.clearErrorAndSetBeneficialOwnerValues(index, values)}
values={{
firstName: owner.firstName || '',
lastName: owner.lastName || '',
street: owner.street || '',
city: owner.city || '',
state: owner.state || '',
zipCode: owner.zipCode || '',
dob: owner.dob || '',
ssnLast4: owner.ssnLast4 || '',
}}
errors={lodashGet(this.getErrors(), `beneficialOwnersErrors[${index}]`, {})}
/>
{this.state.beneficialOwners.length > 1 && (
<TextLink onPress={() => this.removeBeneficialOwner(owner)}>
{this.props.translate('beneficialOwnersStep.removeOwner')}
</TextLink>
)}
</View>
))}
{this.canAddMoreBeneficialOwners() && (
<TextLink onPress={this.addBeneficialOwner}>
{this.props.translate('beneficialOwnersStep.addAnotherIndividual')}
<Text style={[styles.textStrong, styles.link]}>{this.props.companyName}</Text>
</TextLink>
)}
</View>
)}
<Text style={[styles.mv5]}>
{this.props.translate('beneficialOwnersStep.agreement')}
</Text>
<CheckboxWithLabel
style={[styles.mt4]}
isChecked={this.state.acceptTermsAndConditions}
onInputChange={() => this.toggleCheckbox('acceptTermsAndConditions')}
LabelComponent={() => (
<View style={[styles.flexRow]}>
<Text>{this.props.translate('common.iAcceptThe')}</Text>
<TextLink href="https://use.expensify.com/achterms">
{`${this.props.translate('beneficialOwnersStep.termsAndConditions')}`}
</TextLink>
</View>
)}
errorText={this.getErrorText('acceptTermsAndConditions')}
hasError={this.getErrors().acceptTermsAndConditions}
/>
<CheckboxWithLabel
style={[styles.mt4]}
isChecked={this.state.certifyTrueInformation}
onInputChange={() => this.toggleCheckbox('certifyTrueInformation')}
LabelComponent={() => (
<Text>{this.props.translate('beneficialOwnersStep.certifyTrueAndAccurate')}</Text>
)}
errorText={this.getErrorText('certifyTrueInformation')}
/>
</ReimbursementAccountForm>
</>
);
}
}
ACHContractStep.propTypes = propTypes;
export default compose(
withLocalize,
withOnyx({
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
reimbursementAccountDraft: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT,
},
}),
)(ACHContractStep);