Skip to content

Commit

Permalink
fix: adjust to change from ValidationStep
Browse files Browse the repository at this point in the history
  • Loading branch information
barttom committed Feb 7, 2024
1 parent 12b559d commit 9f060e6
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,19 @@ import BankAccount from '@libs/models/BankAccount';
import EnableBankAccount from '@pages/ReimbursementAccount/EnableBankAccount/EnableBankAccount';
import * as Report from '@userActions/Report';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Account, ReimbursementAccount} from '@src/types/onyx';
import type {Account, Policy, ReimbursementAccount} from '@src/types/onyx';
import BankAccountValidationForm from './components/BankAccountValidationForm';
import FinishChatCard from './components/FinishChatCard';

type ConnectBankAccountOnyxProps = {
/** User's account who is setting up bank account */
account: OnyxEntry<Account>;

/** The policy which the user has access to and which the report is tied to */
policy: OnyxEntry<Policy>;

/** Reimbursement account from ONYX */
reimbursementAccount: OnyxEntry<ReimbursementAccount>;
};

type ConnectBankAccountProps = ConnectBankAccountOnyxProps & {
Expand All @@ -29,11 +35,11 @@ type ConnectBankAccountProps = ConnectBankAccountOnyxProps & {
onBackButtonPress: () => void;
};

function ConnectBankAccount({reimbursementAccount, onBackButtonPress, account}: ConnectBankAccountProps) {
function ConnectBankAccount({reimbursementAccount, onBackButtonPress, account, policy}: ConnectBankAccountProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();

const handleNavigateToConciergeChat = () => Report.navigateToConciergeChat();
const handleNavigateToConciergeChat = () => Report.navigateToConciergeChat(false, true);
const bankAccountState = reimbursementAccount.achData?.state ?? '';

// If a user tries to navigate directly to the validate page we'll show them the EnableStep
Expand Down Expand Up @@ -74,6 +80,7 @@ function ConnectBankAccount({reimbursementAccount, onBackButtonPress, account}:
<BankAccountValidationForm
requiresTwoFactorAuth={requiresTwoFactorAuth}
reimbursementAccount={reimbursementAccount}
policy={policy}
/>
)}
{isBankAccountVerifying && (
Expand All @@ -92,4 +99,10 @@ export default withOnyx<ConnectBankAccountProps, ConnectBankAccountOnyxProps>({
account: {
key: ONYXKEYS.ACCOUNT,
},
policy: {
key: ({reimbursementAccount}) => `${ONYXKEYS.COLLECTION.POLICY}${reimbursementAccount?.achData?.policyID}`,
},
reimbursementAccount: {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
},
})(ConnectBankAccount);
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import Text from '@components/Text';
import TextInput from '@components/TextInput';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import getPermittedDecimalSeparator from '@libs/getPermittedDecimalSeparator';
import * as ValidationUtils from '@libs/ValidationUtils';
import * as BankAccounts from '@userActions/BankAccounts';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReimbursementAccount} from '@src/types/onyx';
import type {Policy, ReimbursementAccount} from '@src/types/onyx';
import type {ReimbursementAccountDraftValues} from '@src/types/onyx/ReimbursementAccountDraft';
import Enable2FACard from './Enable2FACard';

Expand All @@ -21,6 +23,9 @@ type BankAccountValidationFormProps = {

/** Boolean required to display Enable2FACard component */
requiresTwoFactorAuth: boolean;

/** The policy which the user has access to and which the report is tied to */
policy: Policy | null;
};

const getAmountValues = (values: ReimbursementAccountDraftValues): Record<string, string> => ({
Expand All @@ -29,41 +34,41 @@ const getAmountValues = (values: ReimbursementAccountDraftValues): Record<string
amount3: values?.amount3 ?? '',
});

const filterInput = (amount: string) => {
const value = amount ? amount.trim() : '';
if (value === '' || Number.isNaN(Number(value)) || !Math.abs(Str.fromUSDToNumber(value, true))) {
const filterInput = (amount: string, amountRegex?: RegExp) => {
let value = amount ? amount.toString().trim() : '';
value = value.replace(/^0+|0+$/g, '');
if (value === '' || Number.isNaN(Number(value)) || !Math.abs(Str.fromUSDToNumber(value, false)) || (amountRegex && !amountRegex.test(value))) {
return '';
}

// If the user enters the values in dollars, convert it to the respective cents amount
if (value.includes('.')) {
return Str.fromUSDToNumber(value, true);
}

return value;
};

const validate = (values: ReimbursementAccountDraftValues) => {
const errors: Record<string, string> = {};
const amountValues = getAmountValues(values);
function BankAccountValidationForm({requiresTwoFactorAuth, reimbursementAccount, policy}: BankAccountValidationFormProps) {
const {translate, toLocaleDigit} = useLocalize();
const styles = useThemeStyles();

Object.keys(amountValues).forEach((key) => {
const value = amountValues[key];
const filteredValue = filterInput(value);
if (ValidationUtils.isRequiredFulfilled(filteredValue.toString())) {
return;
}
errors[key] = 'common.error.invalidAmount';
});
const policyID = reimbursementAccount?.achData?.policyID ?? '';

return errors;
};
const validate = (values: ReimbursementAccountDraftValues) => {
const errors: Record<string, string> = {};
const amountValues = getAmountValues(values);
const decimalSeparator = toLocaleDigit('.');
const outputCurrency = policy?.outputCurrency ?? CONST.CURRENCY.USD;
const amountRegex = RegExp(String.raw`^-?\d{0,8}([${getPermittedDecimalSeparator(decimalSeparator)}]\d{0,${CurrencyUtils.getCurrencyDecimals(outputCurrency)}})?$`, 'i');

Object.keys(amountValues).forEach((key) => {
const value = amountValues[key];
const filteredValue = filterInput(value, amountRegex);
if (ValidationUtils.isRequiredFulfilled(filteredValue.toString())) {
return;
}
errors[key] = 'common.error.invalidAmount';
});

function BankAccountValidationForm({requiresTwoFactorAuth, reimbursementAccount}: BankAccountValidationFormProps) {
const {translate} = useLocalize();
const styles = useThemeStyles();
return errors;
};

const policyID = reimbursementAccount?.achData?.policyID ?? '';
const submit = useCallback(
(values: ReimbursementAccountDraftValues) => {
const amount1 = filterInput(values.amount1 ?? '');
Expand Down
8 changes: 1 addition & 7 deletions src/pages/ReimbursementAccount/ReimbursementAccountPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,7 @@ function ReimbursementAccountPage({reimbursementAccount, route, onfidoToken, pol
}

if (currentStep === CONST.BANK_ACCOUNT.STEP.VALIDATION) {
return (
<ConnectBankAccount
reimbursementAccount={reimbursementAccount}
onBackButtonPress={goBack}
account={account}
/>
);
return <ConnectBankAccount onBackButtonPress={goBack} />;
}

if (currentStep === CONST.BANK_ACCOUNT.STEP.ENABLE) {
Expand Down

0 comments on commit 9f060e6

Please sign in to comment.