forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ValidationStep.js
271 lines (244 loc) · 10.7 KB
/
ValidationStep.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
import lodashGet from 'lodash/get';
import React from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import _ from 'underscore';
import styles from '../../styles/styles';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import * as BankAccounts from '../../libs/actions/BankAccounts';
import * as Report from '../../libs/actions/Report';
import HeaderWithCloseButton from '../../components/HeaderWithCloseButton';
import Navigation from '../../libs/Navigation/Navigation';
import ExpensiTextInput from '../../components/ExpensiTextInput';
import Text from '../../components/Text';
import BankAccount from '../../libs/models/BankAccount';
import TextLink from '../../components/TextLink';
import ONYXKEYS from '../../ONYXKEYS';
import compose from '../../libs/compose';
import * as ReimbursementAccountUtils from '../../libs/ReimbursementAccountUtils';
import * as ValidationUtils from '../../libs/ValidationUtils';
import EnableStep from './EnableStep';
import reimbursementAccountPropTypes from './reimbursementAccountPropTypes';
import ReimbursementAccountForm from './ReimbursementAccountForm';
import * as Expensicons from '../../components/Icon/Expensicons';
import * as Illustrations from '../../components/Icon/Illustrations';
import WorkspaceSection from '../workspace/WorkspaceSection';
const propTypes = {
...withLocalizePropTypes,
/** Bank account currently in setup */
reimbursementAccount: reimbursementAccountPropTypes,
};
const defaultProps = {
reimbursementAccount: {
errors: {},
maxAttemptsReached: false,
},
};
class ValidationStep extends React.Component {
constructor(props) {
super(props);
this.submit = this.submit.bind(this);
this.state = {
amount1: ReimbursementAccountUtils.getDefaultStateForField(props, 'amount1', ''),
amount2: ReimbursementAccountUtils.getDefaultStateForField(props, 'amount2', ''),
amount3: ReimbursementAccountUtils.getDefaultStateForField(props, 'amount3', ''),
};
this.requiredFields = [
'amount1',
'amount2',
'amount3',
];
this.errorTranslationKeys = {
amount1: 'common.error.invalidAmount',
amount2: 'common.error.invalidAmount',
amount3: 'common.error.invalidAmount',
};
this.getErrors = () => ReimbursementAccountUtils.getErrors(this.props);
this.getErrorText = inputKey => ReimbursementAccountUtils.getErrorText(this.props, this.errorTranslationKeys, inputKey);
this.clearError = inputKey => ReimbursementAccountUtils.clearError(this.props, inputKey);
}
/**
* @param {Object} value
*/
setValue(value) {
BankAccounts.updateReimbursementAccountDraft(value);
this.setState(value);
}
/**
* Clear the error associated to inputKey if found and store the inputKey new value in the state.
*
* @param {String} inputKey
* @param {String} value
*/
clearErrorAndSetValue(inputKey, value) {
this.setValue({[inputKey]: value});
this.clearError(inputKey);
}
/**
* @returns {Boolean}
*/
validate() {
const errors = {};
const values = {
amount1: this.filterInput(this.state.amount1),
amount2: this.filterInput(this.state.amount2),
amount3: this.filterInput(this.state.amount3),
};
_.each(this.requiredFields, (inputKey) => {
if (ValidationUtils.isRequiredFulfilled(values[inputKey])) {
return;
}
errors[inputKey] = true;
});
BankAccounts.setBankAccountFormValidationErrors(errors);
return _.size(errors) === 0;
}
submit() {
if (!this.validate()) {
BankAccounts.showBankAccountErrorModal();
return;
}
const amount1 = this.filterInput(this.state.amount1);
const amount2 = this.filterInput(this.state.amount2);
const amount3 = this.filterInput(this.state.amount3);
const validateCode = [amount1, amount2, amount3].join(',');
// Send valid amounts to BankAccountAPI::validateBankAccount in Web-Expensify
const bankaccountID = lodashGet(this.props.reimbursementAccount, 'achData.bankAccountID');
BankAccounts.validateBankAccount(bankaccountID, validateCode);
}
/**
* Filter input for validation amount
* Anything that isn't a number is returned as an empty string
* Any dollar amount (e.g. 1.12) will be returned as 112
*
* @param {String} amount field input
*
* @returns {String}
*/
filterInput(amount) {
let value = amount.trim();
if (value === '' || !Math.abs(Str.fromUSDToNumber(value)) || _.isNaN(Number(value))) {
return '';
}
// If the user enters the values in dollars, convert it to the respective cents amount
if (_.contains(value, '.')) {
value = Str.fromUSDToNumber(value);
}
return value;
}
render() {
const state = lodashGet(this.props, 'reimbursementAccount.achData.state');
// If a user tries to navigate directly to the validate page we'll show them the EnableStep
if (state === BankAccount.STATE.OPEN) {
return <EnableStep achData={lodashGet(this.props, 'reimbursementAccount.achData')} />;
}
const maxAttemptsReached = lodashGet(this.props, 'reimbursementAccount.maxAttemptsReached');
const isVerifying = !maxAttemptsReached && state === BankAccount.STATE.VERIFYING;
return (
<View style={[styles.flex1, styles.justifyContentBetween]}>
<HeaderWithCloseButton
title={this.props.translate('workspace.common.testTransactions')}
stepCounter={{step: 5, total: 5}}
onCloseButtonPress={Navigation.dismissModal}
onBackButtonPress={() => Navigation.goBack()}
shouldShowBackButton
shouldShowStepCounter={!isVerifying}
/>
{maxAttemptsReached && (
<View style={[styles.m5, styles.flex1]}>
<Text>
{this.props.translate('validationStep.maxAttemptsReached')}
{' '}
{this.props.translate('common.please')}
{' '}
<TextLink onPress={Report.navigateToConciergeChat}>
{this.props.translate('common.contactUs')}
</TextLink>
.
</Text>
</View>
)}
{!maxAttemptsReached && state === BankAccount.STATE.PENDING && (
<ReimbursementAccountForm
onSubmit={this.submit}
>
<View style={[styles.mb2]}>
<Text style={[styles.mb5]}>
{this.props.translate('validationStep.description')}
</Text>
<Text style={[styles.mb2]}>
{this.props.translate('validationStep.descriptionCTA')}
</Text>
</View>
<View style={[styles.mv5, styles.flex1]}>
<ExpensiTextInput
containerStyles={[styles.mb1]}
placeholder="1.52"
keyboardType="decimal-pad"
value={this.state.amount1}
onChangeText={amount1 => this.clearErrorAndSetValue('amount1', amount1)}
errorText={this.getErrorText('amount1')}
/>
<ExpensiTextInput
containerStyles={[styles.mb1]}
placeholder="1.53"
keyboardType="decimal-pad"
value={this.state.amount2}
onChangeText={amount2 => this.clearErrorAndSetValue('amount2', amount2)}
errorText={this.getErrorText('amount2')}
/>
<ExpensiTextInput
containerStyles={[styles.mb1]}
placeholder="1.54"
keyboardType="decimal-pad"
value={this.state.amount3}
onChangeText={amount3 => this.clearErrorAndSetValue('amount3', amount3)}
errorText={this.getErrorText('amount3')}
/>
</View>
</ReimbursementAccountForm>
)}
{isVerifying && (
<View style={[styles.flex1]}>
<WorkspaceSection
title={this.props.translate('workspace.bankAccount.letsFinishInChat')}
icon={Illustrations.ConciergeBlue}
menuItems={[
{
title: this.props.translate('validationStep.letsChatCTA'),
icon: Expensicons.ChatBubble,
onPress: Report.navigateToConciergeChat,
shouldShowRightIcon: true,
},
{
title: this.props.translate('workspace.bankAccount.noLetsStartOver'),
icon: Expensicons.RotateLeft,
shouldShowRightIcon: true,
onPress: BankAccounts.requestResetFreePlanBankAccount,
},
]}
>
<Text>
{this.props.translate('validationStep.letsChatText')}
</Text>
</WorkspaceSection>
</View>
)}
</View>
);
}
}
ValidationStep.propTypes = propTypes;
ValidationStep.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
reimbursementAccountDraft: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT_DRAFT,
},
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
}),
)(ValidationStep);