Skip to content

Commit b23fe0f

Browse files
MelvinBotsituchan
andcommitted
Clear completed international bank drafts and track previous focus
Co-authored-by: Situ Chandra Shil <situchan@users.noreply.github.com>
1 parent 1707da3 commit b23fe0f

4 files changed

Lines changed: 74 additions & 12 deletions

File tree

‎src/libs/actions/BankAccounts.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1812,7 +1812,7 @@ function createCorpayBankAccountForWalletFlow(data: InternationalBankAccountForm
18121812
inputs: JSON.stringify(inputData),
18131813
};
18141814

1815-
const onyxData: OnyxData<typeof ONYXKEYS.REIMBURSEMENT_ACCOUNT> = {
1815+
const onyxData: OnyxData<typeof ONYXKEYS.REIMBURSEMENT_ACCOUNT | typeof ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM_DRAFT> = {
18161816
optimisticData: [
18171817
{
18181818
onyxMethod: Onyx.METHOD.MERGE,
@@ -1834,6 +1834,11 @@ function createCorpayBankAccountForWalletFlow(data: InternationalBankAccountForm
18341834
isSuccess: true,
18351835
},
18361836
},
1837+
{
1838+
onyxMethod: Onyx.METHOD.SET,
1839+
key: ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM_DRAFT,
1840+
value: null,
1841+
},
18371842
],
18381843
failureData: [
18391844
{

‎src/pages/settings/Wallet/InternationalDepositAccount/InternationalDepositAccountContent.tsx‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ScreenWrapper from '@components/ScreenWrapper';
66

77
import useAndroidBackButtonHandler from '@hooks/useAndroidBackButtonHandler';
88
import useLocalize from '@hooks/useLocalize';
9+
import usePrevious from '@hooks/usePrevious';
910
import useRootNavigationState from '@hooks/useRootNavigationState';
1011
import useSubPage from '@hooks/useSubPage';
1112

@@ -165,8 +166,8 @@ function InternationalDepositAccountContent({
165166
useAndroidBackButtonHandler(handleBackButtonPress);
166167

167168
const hasMovedToSuccess = useRef(false);
168-
const wasUnfocusedAfterSuccess = useRef(false);
169169
const isFocused = useIsFocused();
170+
const prevIsFocused = usePrevious(isFocused);
170171
const leaveFlow = useEffectEvent(() => handleFinishStep());
171172

172173
const handleNextScreen = () => {
@@ -183,20 +184,12 @@ function InternationalDepositAccountContent({
183184
// Each step stays mounted under the next one. Returning to a completed confirmation means
184185
// the user went back from success, so leave the flow instead of allowing another submission.
185186
useEffect(() => {
186-
if (!hasMovedToSuccess.current) {
187+
if (!hasMovedToSuccess.current || !isFocused || prevIsFocused) {
187188
return;
188189
}
189-
if (!isFocused) {
190-
wasUnfocusedAfterSuccess.current = true;
191-
return;
192-
}
193-
if (!wasUnfocusedAfterSuccess.current) {
194-
return;
195-
}
196-
wasUnfocusedAfterSuccess.current = false;
197190
hasMovedToSuccess.current = false;
198191
leaveFlow();
199-
}, [isFocused]);
192+
}, [isFocused, prevIsFocused]);
200193

201194
return (
202195
<ScreenWrapper

‎tests/unit/BankAccountsTest.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@ import {
88
connectBankAccountManually,
99
connectBankAccountWithPlaid,
1010
createCollectOnlyDepositAccount,
11+
createCorpayBankAccountForWalletFlow,
1112
deletePaymentBankAccount,
1213
getCorpayOnboardingFields,
1314
} from '@userActions/BankAccounts';
1415

1516
import CONST from '@src/CONST';
1617
import ONYXKEYS from '@src/ONYXKEYS';
18+
import type {InternationalBankAccountForm} from '@src/types/form';
1719
import type {LastPaymentMethod, PersonalBankAccount} from '@src/types/onyx';
1820
import type PlaidBankAccount from '@src/types/onyx/PlaidBankAccount';
1921

@@ -186,6 +188,49 @@ describe('BankAccounts', () => {
186188
});
187189
});
188190

191+
describe('createCorpayBankAccountForWalletFlow', () => {
192+
const draft: InternationalBankAccountForm = {bankCountry: CONST.COUNTRY.GB, bankCurrency: CONST.CURRENCY.GBP, acceptTerms: 'true'};
193+
194+
beforeAll(() => Onyx.init({keys: ONYXKEYS}));
195+
196+
beforeEach(async () => {
197+
await Onyx.set(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM_DRAFT, draft);
198+
});
199+
200+
afterEach(() => Onyx.clear());
201+
202+
test('clears the completed draft after successful creation without waiting for the success screen to finish', async () => {
203+
// Given a retained international draft that could otherwise reopen at confirmation
204+
createCorpayBankAccountForWalletFlow(draft, 'individual', CONST.COUNTRY.GB, 'wire');
205+
const [, , onyxData] = mockWrite.mock.calls.at(-1) ?? [];
206+
207+
// When the API applies the successful creation updates, even without a mounted confirmation
208+
await Onyx.update(onyxData?.successData ?? []);
209+
await waitForBatchedUpdates();
210+
211+
// Then the draft is removed and the normal success state is preserved
212+
expect(await getOnyxValue(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM_DRAFT)).toBeUndefined();
213+
expect(await getOnyxValue(ONYXKEYS.REIMBURSEMENT_ACCOUNT)).toEqual(expect.objectContaining({isSuccess: true, isLoading: false}));
214+
});
215+
216+
test('preserves the draft while creation is pending and after failure so it can be retried', async () => {
217+
// Given a filled draft submitted for creation
218+
createCorpayBankAccountForWalletFlow(draft, 'individual', CONST.COUNTRY.GB, 'wire');
219+
const [, , onyxData] = mockWrite.mock.calls.at(-1) ?? [];
220+
221+
// When optimistic updates are applied while the request is pending
222+
await Onyx.update(onyxData?.optimisticData ?? []);
223+
await waitForBatchedUpdates();
224+
225+
// Then the draft remains available, including after failure updates are applied
226+
expect(await getOnyxValue(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM_DRAFT)).toEqual(draft);
227+
await Onyx.update(onyxData?.failureData ?? []);
228+
await waitForBatchedUpdates();
229+
expect(await getOnyxValue(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM_DRAFT)).toEqual(draft);
230+
expect(await getOnyxValue(ONYXKEYS.REIMBURSEMENT_ACCOUNT)).toEqual(expect.objectContaining({isSuccess: false, isLoading: false}));
231+
});
232+
});
233+
189234
describe('deletePaymentBankAccount', () => {
190235
const deletedBankAccountID = 123;
191236
const replacementBankAccountID = 456;

‎tests/unit/pages/InternationalDepositAccountContentTest.tsx‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,25 @@ describe('InternationalDepositAccountContent completion navigation', () => {
111111
expect(nextPage).toHaveBeenCalledTimes(1);
112112
});
113113

114+
it('exits when confirmation loses focus before creation succeeds and then regains focus', () => {
115+
// Given confirmation was covered by an edit while its creation request was still pending
116+
const {rerender} = render(<TestFlow />);
117+
jest.mocked(useIsFocused).mockReturnValue(false);
118+
rerender(<TestFlow />);
119+
120+
// When creation succeeds while confirmation is unfocused, then confirmation regains focus
121+
act(() => mockStep.mock.lastCall?.[0].onNext());
122+
expect(goBackSpy).not.toHaveBeenCalled();
123+
jest.mocked(useIsFocused).mockReturnValue(true);
124+
rerender(<TestFlow />);
125+
126+
// Then the completed flow exits rather than allowing the same account to be submitted again
127+
expect(goBackSpy).toHaveBeenCalledTimes(1);
128+
expect(goBackSpy).toHaveBeenCalledWith(ROUTES.SETTINGS_WALLET);
129+
expect(clearDraftValues).toHaveBeenCalledWith(ONYXKEYS.FORMS.INTERNATIONAL_BANK_ACCOUNT_FORM);
130+
expect(nextPage).toHaveBeenCalledTimes(1);
131+
});
132+
114133
it('does not exit an unfinished confirmation when it regains focus', () => {
115134
// Given confirmation has not advanced, including when a creation request fails
116135
const {rerender} = render(<TestFlow />);

0 commit comments

Comments
 (0)