Skip to content
Open
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
18 changes: 16 additions & 2 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,23 @@ function setPersonalBankAccountContinueKYCOnSuccess(onSuccessFallbackRoute: Rout
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {onSuccessFallbackRoute});
}

function clearPersonalBankAccount() {
/**
* Clears personal bank account state. Pass `shouldPreserveAccountData=true` to only clear UI/error
* fields while keeping routing and context fields intact.
*/
function clearPersonalBankAccount(shouldPreserveAccountData = false) {
clearPlaid();
Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, null);
if (shouldPreserveAccountData) {
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {
shouldShowSuccess: null,
isLoading: null,
errors: null,
errorFields: null,
updateError: null,
});
} else {
Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, null);
}
Onyx.set(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, null);
clearPersonalBankAccountSetupType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function AccountFlowEntryPoint({policyName = '', onBackButtonPress}: AccountFlow
const [isPlaidDisabled] = useOnyx(ONYXKEYS.IS_PLAID_DISABLED);

useEffect(() => {
clearPersonalBankAccount();
clearPersonalBankAccount(true);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Clear stale routing when entering the generic add-account flow

When this entry point is reached from a normal wallet add-bank-account action after the user previously abandoned a payment/KYC-driven add-bank flow, openPersonalBankAccountSetupView({}) does not clear PERSONAL_BANK_ACCOUNT, and this call now preserves the old onSuccessFallbackRoute/exitReportID. After the later wallet-initiated bank account succeeds, AddPersonalBankAccountPage.exitFlow(true) can continue the stale KYC/payment flow or dismiss to an old report instead of returning to the wallet. Preserve only routing data that was intentionally seeded for the current flow, or clear stale routing before this generic entry point is reused.

Useful? React with 👍 / 👎.

}, []);

const handleConnectManually = () => {
Expand Down
48 changes: 47 additions & 1 deletion tests/actions/BankAccountsTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import Onyx from 'react-native-onyx';
import {connectBankAccountWithPlaid} from '@libs/actions/BankAccounts';
import {clearPersonalBankAccount, connectBankAccountWithPlaid} from '@libs/actions/BankAccounts';
import {WRITE_COMMANDS} from '@libs/API/types';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {ReimbursementAccountForm} from '@src/types/form/ReimbursementAccountForm';
import type PlaidBankAccount from '@src/types/onyx/PlaidBankAccount';
import getOnyxValue from '../utils/getOnyxValue';
Expand Down Expand Up @@ -109,4 +110,49 @@ describe('actions/BankAccounts', () => {
});
});
});

describe('clearPersonalBankAccount', () => {
test('wipes the entire PERSONAL_BANK_ACCOUNT key by default', async () => {
await Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {onSuccessFallbackRoute: ROUTES.ENABLE_PAYMENTS, shouldShowSuccess: true});
await waitForBatchedUpdates();

clearPersonalBankAccount();
await waitForBatchedUpdates();

expect(await getOnyxValue(ONYXKEYS.PERSONAL_BANK_ACCOUNT)).toBeFalsy();
});

test('preserves navigation fields and clears form/status fields when shouldPreserveAccountData=true', async () => {
await Onyx.set(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {
onSuccessFallbackRoute: ROUTES.ENABLE_PAYMENTS,
exitReportID: 'report123',
shouldShowSuccess: true,
isLoading: true,
errors: {field: 'error'},
updateError: 'addPersonalBankAccount.updatePersonalInfoFailure',
});
await waitForBatchedUpdates();

clearPersonalBankAccount(true);
await waitForBatchedUpdates();

const result = await getOnyxValue(ONYXKEYS.PERSONAL_BANK_ACCOUNT);
expect(result?.onSuccessFallbackRoute).toBe(ROUTES.ENABLE_PAYMENTS);
expect(result?.exitReportID).toBe('report123');
expect(result?.shouldShowSuccess).toBeFalsy();
expect(result?.isLoading).toBeFalsy();
expect(result?.errors).toBeFalsy();
expect(result?.updateError).toBeFalsy();
});

test('always clears the form draft regardless of the flag', async () => {
await Onyx.set(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, {setupType: CONST.BANK_ACCOUNT.SETUP_TYPE.MANUAL});
await waitForBatchedUpdates();

clearPersonalBankAccount(true);
await waitForBatchedUpdates();

expect((await getOnyxValue(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT))?.setupType).toBeFalsy();
});
});
});
Loading