From 21dd01de8cc21e41257bdbfa4a994f0deb6459c2 Mon Sep 17 00:00:00 2001 From: Kamil Owczarz Date: Fri, 11 Aug 2023 15:21:52 +0200 Subject: [PATCH] Rewrite CompanyStep to functional component --- src/pages/ReimbursementAccount/CompanyStep.js | 283 +++++++++--------- 1 file changed, 137 insertions(+), 146 deletions(-) diff --git a/src/pages/ReimbursementAccount/CompanyStep.js b/src/pages/ReimbursementAccount/CompanyStep.js index a57c80b0a7e2..23ec83f9c976 100644 --- a/src/pages/ReimbursementAccount/CompanyStep.js +++ b/src/pages/ReimbursementAccount/CompanyStep.js @@ -1,6 +1,6 @@ import _ from 'underscore'; import lodashGet from 'lodash/get'; -import React from 'react'; +import React, {useMemo} from 'react'; import {View} from 'react-native'; import Str from 'expensify-common/lib/str'; import {withOnyx} from 'react-native-onyx'; @@ -53,33 +53,26 @@ const defaultProps = { policyID: '', }; -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, '')}`; - } - +function CompanyStep ({reimbursementAccount, reimbursementAccountDraft, getDefaultStateForField, onBackButtonPress, translate, session, user, policyID}) { /** * @param {Array} fieldNames * * @returns {*} */ - getBankAccountFields(fieldNames) { + function getBankAccountFields(fieldNames) { return { - ..._.pick(lodashGet(this.props.reimbursementAccount, 'achData'), ...fieldNames), - ..._.pick(this.props.reimbursementAccountDraft, ...fieldNames), + ..._.pick(lodashGet(reimbursementAccount, 'achData'), ...fieldNames), + ..._.pick(reimbursementAccountDraft, ...fieldNames), }; } + const defaultWebsite = useMemo(() => lodashGet(user, 'isFromPublicDomain', false) ? 'https://' : `https://www.${Str.extractEmailDomain(session.email, '')}`, [user, session]); + /** * @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 validate = (values) => { const errors = {}; if (!values.companyName) { @@ -135,12 +128,12 @@ class CompanyStep extends React.Component { return errors; } - submit(values) { + const submit = (values) => { const bankAccount = { - bankAccountID: lodashGet(this.props.reimbursementAccount, 'achData.bankAccountID') || 0, + bankAccountID: lodashGet(reimbursementAccount, 'achData.bankAccountID') || 0, // Fields from BankAccount step - ...this.getBankAccountFields(['routingNumber', 'accountNumber', 'bankName', 'plaidAccountID', 'plaidAccessToken', 'isSavings']), + ...getBankAccountFields(['routingNumber', 'accountNumber', 'bankName', 'plaidAccountID', 'plaidAccessToken', 'isSavings']), // Fields from Company step ...values, @@ -148,144 +141,142 @@ class CompanyStep extends React.Component { companyPhone: parsePhoneNumber(values.companyPhone, {regionCode: CONST.COUNTRY.US}).number.significant, }; - BankAccounts.updateCompanyInformationForBankAccount(bankAccount, this.props.policyID); + BankAccounts.updateCompanyInformationForBankAccount(bankAccount, policyID); } - 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')); + const bankAccountID = lodashGet(reimbursementAccount, 'achData.bankAccountID', 0); + const shouldDisableCompanyName = Boolean(bankAccountID && getDefaultStateForField('companyName')); + const shouldDisableCompanyTaxID = Boolean(bankAccountID && getDefaultStateForField('companyTaxID')); - return ( - - + +
+ {translate('companyStep.subtitle')} + - - {this.props.translate('companyStep.subtitle')} - - - - + + + + + ({value, label}))} + placeholder={{value: '', label: '-'}} + defaultValue={getDefaultStateForField('incorporationType')} shouldSaveDraft - hint={this.props.translate('common.websiteExample')} - keyboardType={CONST.KEYBOARD_TYPE.URL} /> - + + - - ({value, label}))} - placeholder={{value: '', label: '-'}} - defaultValue={this.props.getDefaultStateForField('incorporationType')} - shouldSaveDraft - /> - - - - - - - - ( - - {`${this.props.translate('companyStep.confirmCompanyIsNot')} `} - - {`${this.props.translate('companyStep.listOfRestrictedBusinesses')}.`} - - - )} - style={[styles.mt4]} + + + - -
- ); - } + + ( + + {`${translate('companyStep.confirmCompanyIsNot')} `} + + {`${translate('companyStep.listOfRestrictedBusinesses')}.`} + + + )} + style={[styles.mt4]} + shouldSaveDraft + /> + + + ); } CompanyStep.propTypes = propTypes;