Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 64 additions & 7 deletions src/pages/ReimbursementAccount/BankAccountStep.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, {useEffect, useMemo, useRef} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {useOnyx} from 'react-native-onyx';
Expand All @@ -13,20 +13,23 @@ import ScrollView from '@components/ScrollView';
import Section from '@components/Section';
import Text from '@components/Text';
import TextLink from '@components/TextLink';
import ValidateAccountMessage from '@components/ValidateAccountMessage';
import ValidateCodeActionModal from '@components/ValidateCodeActionModal';
import useLocalize from '@hooks/useLocalize';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import getPlaidDesktopMessage from '@libs/getPlaidDesktopMessage';
import * as BankAccounts from '@userActions/BankAccounts';
import * as Link from '@userActions/Link';
import * as ReimbursementAccount from '@userActions/ReimbursementAccount';
import * as User from '@userActions/User';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {ReimbursementAccountForm} from '@src/types/form/ReimbursementAccountForm';
import INPUT_IDS from '@src/types/form/ReimbursementAccountForm';
import type * as OnyxTypes from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import BankInfo from './BankInfo/BankInfo';

type BankAccountStepProps = {
Expand All @@ -47,17 +50,39 @@ type BankAccountStepProps = {

/** Goes to the previous step */
onBackButtonPress: () => void;

/** Should ValidateCodeActionModal be displayed or not */
isValidateCodeActionModalVisible?: boolean;

/** Toggle ValidateCodeActionModal */
toggleValidateCodeActionModal?: (isVisible: boolean) => void;
};

const bankInfoStepKeys = INPUT_IDS.BANK_INFO_STEP;

function BankAccountStep({plaidLinkOAuthToken = '', policyID = '', policyName = '', receivedRedirectURI, reimbursementAccount, onBackButtonPress}: BankAccountStepProps) {
function BankAccountStep({
plaidLinkOAuthToken = '',
policyID = '',
policyName = '',
receivedRedirectURI,
reimbursementAccount,
onBackButtonPress,
isValidateCodeActionModalVisible,
toggleValidateCodeActionModal,
}: BankAccountStepProps) {
const theme = useTheme();
const styles = useThemeStyles();
const {translate} = useLocalize();
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [isPlaidDisabled] = useOnyx(ONYXKEYS.IS_PLAID_DISABLED);
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST);
const [loginList] = useOnyx(ONYXKEYS.LOGIN_LIST);
const contactMethod = account?.primaryLogin ?? '';
const selectedSubStep = useRef('');

const loginData = useMemo(() => loginList?.[contactMethod], [loginList, contactMethod]);
const validateLoginError = ErrorUtils.getEarliestErrorField(loginData, 'validateLogin');
const hasMagicCodeBeenSent = !!loginData?.validateCodeSent;

let subStep = reimbursementAccount?.achData?.subStep ?? '';
const shouldReinitializePlaidLink = plaidLinkOAuthToken && receivedRedirectURI && subStep !== CONST.BANK_ACCOUNT.SUBSTEP.MANUAL;
Expand All @@ -68,6 +93,18 @@ function BankAccountStep({plaidLinkOAuthToken = '', policyID = '', policyName =
const bankAccountRoute = `${ROUTES.BANK_ACCOUNT_WITH_STEP_TO_OPEN.getRoute('new', policyID, ROUTES.WORKSPACE_INITIAL.getRoute(policyID))}`;
const personalBankAccounts = bankAccountList ? Object.keys(bankAccountList).filter((key) => bankAccountList[key].accountType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT) : [];

useEffect(() => {
if (!account?.validated) {
return;
}

if (selectedSubStep.current === CONST.BANK_ACCOUNT.SUBSTEP.MANUAL) {
BankAccounts.setBankAccountSubStep(CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL);
} else if (selectedSubStep.current === CONST.BANK_ACCOUNT.SUBSTEP.PLAID) {
BankAccounts.openPlaidView();
}
}, [account?.validated]);

const removeExistingBankAccountDetails = () => {
const bankAccountData: Partial<ReimbursementAccountForm> = {
[bankInfoStepKeys.ROUTING_NUMBER]: '',
Expand Down Expand Up @@ -138,9 +175,14 @@ function BankAccountStep({plaidLinkOAuthToken = '', policyID = '', policyName =
<MenuItem
icon={Expensicons.Bank}
title={translate('bankAccount.connectOnlineWithPlaid')}
disabled={!!isPlaidDisabled || !account?.validated}
disabled={!!isPlaidDisabled}
onPress={() => {
if (!!isPlaidDisabled || !account?.validated) {
if (isPlaidDisabled) {
return;
}
if (!account?.validated) {
selectedSubStep.current = CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL;
toggleValidateCodeActionModal?.(true);
return;
}
removeExistingBankAccountDetails();
Expand All @@ -154,8 +196,12 @@ function BankAccountStep({plaidLinkOAuthToken = '', policyID = '', policyName =
<MenuItem
icon={Expensicons.Connect}
title={translate('bankAccount.connectManually')}
disabled={!account?.validated}
onPress={() => {
if (!account?.validated) {
selectedSubStep.current = CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID;
toggleValidateCodeActionModal?.(true);
return;
}
removeExistingBankAccountDetails();
BankAccounts.setBankAccountSubStep(CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL);
}}
Expand All @@ -164,7 +210,6 @@ function BankAccountStep({plaidLinkOAuthToken = '', policyID = '', policyName =
/>
</View>
</Section>
{!account?.validated && <ValidateAccountMessage />}
<View style={[styles.mv0, styles.mh5, styles.flexRow, styles.justifyContentBetween]}>
<TextLink href={CONST.PRIVACY_URL}>{translate('common.privacy')}</TextLink>
<PressableWithoutFeedback
Expand All @@ -180,6 +225,18 @@ function BankAccountStep({plaidLinkOAuthToken = '', policyID = '', policyName =
/>
</View>
</PressableWithoutFeedback>
<ValidateCodeActionModal
title={translate('contacts.validateAccount')}
description={translate('contacts.featureRequiresValidate')}
isVisible={!!isValidateCodeActionModalVisible}
hasMagicCodeBeenSent={hasMagicCodeBeenSent}
validatePendingAction={loginData?.pendingFields?.validateCodeSent}
sendValidateCode={() => User.requestValidateCodeAction()}
handleSubmitForm={(validateCode) => User.validateSecondaryLogin(loginList, contactMethod, validateCode)}
validateError={!isEmptyObject(validateLoginError) ? validateLoginError : ErrorUtils.getLatestErrorField(loginData, 'validateCodeSent')}
clearError={() => User.clearContactMethodErrors(contactMethod, !isEmptyObject(validateLoginError) ? 'validateLogin' : 'validateCodeSent')}
onClose={() => toggleValidateCodeActionModal?.(false)}
/>
</View>
</ScrollView>
</View>
Expand Down
10 changes: 9 additions & 1 deletion src/pages/ReimbursementAccount/ReimbursementAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ function ReimbursementAccountPage({route, policy}: ReimbursementAccountPageProps
const [isLoadingApp = false] = useOnyx(ONYXKEYS.IS_LOADING_APP);
const [account] = useOnyx(ONYXKEYS.ACCOUNT);
const [isDebugModeEnabled] = useOnyx(ONYXKEYS.USER, {selector: (user) => !!user?.isDebugModeEnabled});
const [isValidateCodeActionModalVisible, setIsValidateCodeActionModalVisible] = useState(false);

const policyName = policy?.name ?? '';
const policyIDParam = route.params?.policyID ?? '-1';
Expand Down Expand Up @@ -422,7 +423,12 @@ function ReimbursementAccountPage({route, policy}: ReimbursementAccountPageProps
// or when data is being loaded. Don't show the loading indicator if we're offline and restarted the bank account setup process
// On Android, when we open the app from the background, Onfido activity gets destroyed, so we need to reopen it.
// eslint-disable-next-line react-compiler/react-compiler
if ((!hasACHDataBeenLoaded || isLoading) && shouldShowOfflineLoader && (shouldReopenOnfido || !requestorStepRef.current)) {
if (
(!hasACHDataBeenLoaded || isLoading) &&
shouldShowOfflineLoader &&
(shouldReopenOnfido || !requestorStepRef?.current) &&
!(currentStep === CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT && isValidateCodeActionModalVisible)
) {
return <ReimbursementAccountLoadingIndicator onBackButtonPress={goBack} />;
}

Expand Down Expand Up @@ -543,6 +549,8 @@ function ReimbursementAccountPage({route, policy}: ReimbursementAccountPageProps
plaidLinkOAuthToken={plaidLinkToken}
policyName={policyName}
policyID={policyIDParam}
isValidateCodeActionModalVisible={isValidateCodeActionModalVisible}
toggleValidateCodeActionModal={setIsValidateCodeActionModalVisible}
/>
);
case CONST.BANK_ACCOUNT.STEP.REQUESTOR:
Expand Down