-
Notifications
You must be signed in to change notification settings - Fork 3k
/
CompanyStep.js
295 lines (264 loc) · 12.5 KB
/
CompanyStep.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
import _ from 'underscore';
import lodashGet from 'lodash/get';
import React from 'react';
import {View} from 'react-native';
import Str from 'expensify-common/lib/str';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import {parsePhoneNumber} from 'awesome-phonenumber';
import HeaderWithBackButton from '../../components/HeaderWithBackButton';
import CONST from '../../CONST';
import * as BankAccounts from '../../libs/actions/BankAccounts';
import Text from '../../components/Text';
import DatePicker from '../../components/DatePicker';
import TextInput from '../../components/TextInput';
import styles from '../../styles/styles';
import CheckboxWithLabel from '../../components/CheckboxWithLabel';
import TextLink from '../../components/TextLink';
import StatePicker from '../../components/StatePicker';
import withLocalize from '../../components/withLocalize';
import * as ValidationUtils from '../../libs/ValidationUtils';
import compose from '../../libs/compose';
import ONYXKEYS from '../../ONYXKEYS';
import Picker from '../../components/Picker';
import AddressForm from './AddressForm';
import Form from '../../components/Form';
import ScreenWrapper from '../../components/ScreenWrapper';
import StepPropTypes from './StepPropTypes';
const propTypes = {
...StepPropTypes,
/** Session info for the currently logged in user. */
session: PropTypes.shape({
/** Currently logged in user email */
email: PropTypes.string,
}),
/** Object with various information about the user */
user: PropTypes.shape({
/** Whether or not the user is on a public domain email account or not */
isFromPublicDomain: PropTypes.bool,
}),
};
const defaultProps = {
session: {
email: null,
},
user: {},
};
class CompanyStep extends React.Component {
constructor(props) {
super(props);
this.submit = this.submit.bind(this);
this.validate = this.validate.bind(this);
this.defaultWebsite = lodashGet(props, 'user.isFromPublicDomain', false) ? 'https://' : `https://www.${Str.extractEmailDomain(props.session.email, '')}`;
}
componentWillUnmount() {
BankAccounts.resetReimbursementAccount();
}
/**
* @param {Array} fieldNames
*
* @returns {*}
*/
getBankAccountFields(fieldNames) {
return {
..._.pick(lodashGet(this.props.reimbursementAccount, 'achData'), ...fieldNames),
..._.pick(this.props.reimbursementAccountDraft, ...fieldNames),
};
}
/**
* @param {Object} values - form input values passed by the Form component
* @returns {Object} - Object containing the errors for each inputID, e.g. {inputID1: error1, inputID2: error2}
*/
validate(values) {
const errors = {};
if (!values.companyName) {
errors.companyName = 'bankAccount.error.companyName';
}
if (!values.addressStreet || !ValidationUtils.isValidAddress(values.addressStreet)) {
errors.addressStreet = 'bankAccount.error.addressStreet';
}
if (!values.addressZipCode || !ValidationUtils.isValidZipCode(values.addressZipCode)) {
errors.addressZipCode = 'bankAccount.error.zipCode';
}
if (!values.addressCity) {
errors.addressCity = 'bankAccount.error.addressCity';
}
if (!values.addressState) {
errors.addressState = 'bankAccount.error.addressState';
}
if (!values.companyPhone || !ValidationUtils.isValidUSPhone(values.companyPhone, true)) {
errors.companyPhone = 'bankAccount.error.phoneNumber';
}
if (!values.website || !ValidationUtils.isValidWebsite(values.website)) {
errors.website = 'bankAccount.error.website';
}
if (!values.companyTaxID || !ValidationUtils.isValidTaxID(values.companyTaxID)) {
errors.companyTaxID = 'bankAccount.error.taxID';
}
if (!values.incorporationType) {
errors.incorporationType = 'bankAccount.error.companyType';
}
if (!values.incorporationDate || !ValidationUtils.isValidDate(values.incorporationDate)) {
errors.incorporationDate = 'common.error.dateInvalid';
} else if (!values.incorporationDate || !ValidationUtils.isValidPastDate(values.incorporationDate)) {
errors.incorporationDate = 'bankAccount.error.incorporationDateFuture';
}
if (!values.incorporationState) {
errors.incorporationState = 'bankAccount.error.incorporationState';
}
if (!values.hasNoConnectionToCannabis) {
errors.hasNoConnectionToCannabis = 'bankAccount.error.restrictedBusiness';
}
return errors;
}
submit(values) {
const bankAccount = {
bankAccountID: lodashGet(this.props.reimbursementAccount, 'achData.bankAccountID') || 0,
// Fields from BankAccount step
...this.getBankAccountFields(['routingNumber', 'accountNumber', 'bankName', 'plaidAccountID', 'plaidAccessToken', 'isSavings']),
// Fields from Company step
...values,
companyTaxID: values.companyTaxID.replace(CONST.REGEX.NON_NUMERIC, ''),
companyPhone: parsePhoneNumber(values.companyPhone, {regionCode: CONST.COUNTRY.US}).number.significant,
};
BankAccounts.updateCompanyInformationForBankAccount(bankAccount);
}
render() {
const bankAccountID = lodashGet(this.props.reimbursementAccount, 'achData.bankAccountID', 0);
const shouldDisableCompanyName = Boolean(bankAccountID && this.props.getDefaultStateForField('companyName'));
const shouldDisableCompanyTaxID = Boolean(bankAccountID && this.props.getDefaultStateForField('companyTaxID'));
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithBackButton
title={this.props.translate('companyStep.headerTitle')}
stepCounter={{step: 2, total: 5}}
shouldShowGetAssistanceButton
guidesCallTaskID={CONST.GUIDES_CALL_TASK_IDS.WORKSPACE_BANK_ACCOUNT}
onBackButtonPress={this.props.onBackButtonPress}
/>
<Form
formID={ONYXKEYS.FORMS.REIMBURSEMENT_ACCOUNT_FORM}
validate={this.validate}
onSubmit={this.submit}
scrollContextEnabled
submitButtonText={this.props.translate('common.saveAndContinue')}
style={[styles.ph5, styles.flexGrow1]}
>
<Text>{this.props.translate('companyStep.subtitle')}</Text>
<TextInput
label={this.props.translate('companyStep.legalBusinessName')}
inputID="companyName"
containerStyles={[styles.mt4]}
disabled={shouldDisableCompanyName}
defaultValue={this.props.getDefaultStateForField('companyName')}
shouldSaveDraft
shouldUseDefaultValue={shouldDisableCompanyName}
/>
<AddressForm
translate={this.props.translate}
defaultValues={{
street: this.props.getDefaultStateForField('addressStreet'),
city: this.props.getDefaultStateForField('addressCity'),
state: this.props.getDefaultStateForField('addressState'),
zipCode: this.props.getDefaultStateForField('addressZipCode'),
}}
inputKeys={{
street: 'addressStreet',
city: 'addressCity',
state: 'addressState',
zipCode: 'addressZipCode',
}}
shouldSaveDraft
streetTranslationKey="common.companyAddress"
/>
<TextInput
inputID="companyPhone"
label={this.props.translate('common.phoneNumber')}
containerStyles={[styles.mt4]}
keyboardType={CONST.KEYBOARD_TYPE.PHONE_PAD}
placeholder={this.props.translate('common.phoneNumberPlaceholder')}
defaultValue={this.props.getDefaultStateForField('companyPhone')}
shouldSaveDraft
/>
<TextInput
inputID="website"
label={this.props.translate('companyStep.companyWebsite')}
containerStyles={[styles.mt4]}
defaultValue={this.props.getDefaultStateForField('website', this.defaultWebsite)}
shouldSaveDraft
hint={this.props.translate('common.websiteExample')}
keyboardType={CONST.KEYBOARD_TYPE.URL}
/>
<TextInput
inputID="companyTaxID"
label={this.props.translate('companyStep.taxIDNumber')}
containerStyles={[styles.mt4]}
keyboardType={CONST.KEYBOARD_TYPE.NUMBER_PAD}
disabled={shouldDisableCompanyTaxID}
placeholder={this.props.translate('companyStep.taxIDNumberPlaceholder')}
defaultValue={this.props.getDefaultStateForField('companyTaxID')}
shouldSaveDraft
shouldUseDefaultValue={shouldDisableCompanyTaxID}
/>
<View style={styles.mt4}>
<Picker
inputID="incorporationType"
label={this.props.translate('companyStep.companyType')}
items={_.map(this.props.translate('companyStep.incorporationTypes'), (label, value) => ({value, label}))}
placeholder={{value: '', label: '-'}}
defaultValue={this.props.getDefaultStateForField('incorporationType')}
shouldSaveDraft
/>
</View>
<View style={styles.mt4}>
<DatePicker
inputID="incorporationDate"
label={this.props.translate('companyStep.incorporationDate')}
placeholder={this.props.translate('companyStep.incorporationDatePlaceholder')}
defaultValue={this.props.getDefaultStateForField('incorporationDate')}
shouldSaveDraft
/>
</View>
<View style={styles.mt4}>
<StatePicker
inputID="incorporationState"
label={this.props.translate('companyStep.incorporationState')}
defaultValue={this.props.getDefaultStateForField('incorporationState')}
shouldSaveDraft
/>
</View>
<CheckboxWithLabel
inputID="hasNoConnectionToCannabis"
defaultValue={this.props.getDefaultStateForField('hasNoConnectionToCannabis', false)}
LabelComponent={() => (
<Text>
{`${this.props.translate('companyStep.confirmCompanyIsNot')} `}
<TextLink
// eslint-disable-next-line max-len
href="https://community.expensify.com/discussion/6191/list-of-restricted-businesses"
>
{`${this.props.translate('companyStep.listOfRestrictedBusinesses')}.`}
</TextLink>
</Text>
)}
style={[styles.mt4]}
shouldSaveDraft
/>
</Form>
</ScreenWrapper>
);
}
}
CompanyStep.propTypes = propTypes;
CompanyStep.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
session: {
key: ONYXKEYS.SESSION,
},
user: {
key: ONYXKEYS.USER,
},
}),
)(CompanyStep);