1- import React , { useEffect , useMemo , useRef } from 'react' ;
1+ import React , { useCallback , useEffect , useMemo } from 'react' ;
22import { View } from 'react-native' ;
33import type { OnyxEntry } from 'react-native-onyx' ;
4+ import type { ValueOf } from 'type-fest' ;
45import HeaderWithBackButton from '@components/HeaderWithBackButton' ;
56import Icon from '@components/Icon' ;
67import { Bank , Connect , Lightbulb , Lock , RotateLeft } from '@components/Icon/Expensicons' ;
@@ -25,7 +26,7 @@ import {REIMBURSEMENT_ACCOUNT_ROUTE_NAMES} from '@libs/ReimbursementAccountUtils
2526import WorkspaceResetBankAccountModal from '@pages/workspace/WorkspaceResetBankAccountModal' ;
2627import { goToWithdrawalAccountSetupStep , openPlaidView , updateReimbursementAccountDraft } from '@userActions/BankAccounts' ;
2728import { openExternalLink , openExternalLinkWithToken } from '@userActions/Link' ;
28- import { requestResetBankAccount , resetReimbursementAccount , setBankAccountSubStep } from '@userActions/ReimbursementAccount' ;
29+ import { requestResetBankAccount , resetReimbursementAccount , setBankAccountSubStep , setReimbursementAccountOptionPressed } from '@userActions/ReimbursementAccount' ;
2930import { clearContactMethodErrors , requestValidateCodeAction , validateSecondaryLogin } from '@userActions/User' ;
3031import CONST from '@src/CONST' ;
3132import ONYXKEYS from '@src/ONYXKEYS' ;
@@ -96,7 +97,7 @@ function VerifiedBankAccountFlowEntryPoint({
9697 const errors = reimbursementAccount ?. errors ?? { } ;
9798 const pendingAction = reimbursementAccount ?. pendingAction ?? null ;
9899 const [ loginList ] = useOnyx ( ONYXKEYS . LOGIN_LIST , { canBeMissing : true } ) ;
99- const optionPressed = useRef ( '' ) ;
100+ const [ reimbursementAccountOptionPressed ] = useOnyx ( ONYXKEYS . REIMBURSEMENT_ACCOUNT_OPTION_PRESSED , { canBeMissing : true } ) ;
100101 const isAccountValidated = account ?. validated ?? false ;
101102
102103 const contactMethod = account ?. primaryLogin ?? '' ;
@@ -119,6 +120,18 @@ function VerifiedBankAccountFlowEntryPoint({
119120 updateReimbursementAccountDraft ( bankAccountData ) ;
120121 } ;
121122
123+ /**
124+ * Prepares and redirects user to next step in the USD flow
125+ */
126+ const prepareNextStep = useCallback (
127+ ( setupType : ValueOf < typeof CONST . BANK_ACCOUNT . SETUP_TYPE > ) => {
128+ setBankAccountSubStep ( setupType ) ;
129+ setUSDBankAccountStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
130+ goToWithdrawalAccountSetupStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
131+ } ,
132+ [ setUSDBankAccountStep ] ,
133+ ) ;
134+
122135 /**
123136 * optionPressed ref indicates what user selected before modal to validate account was displayed
124137 * In this hook we check if account was validated and then proceed with the option user selected
@@ -129,23 +142,25 @@ function VerifiedBankAccountFlowEntryPoint({
129142 return ;
130143 }
131144
132- if ( optionPressed . current === CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ) {
145+ if ( reimbursementAccountOptionPressed === CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ) {
133146 if ( isNonUSDWorkspace ) {
134147 setNonUSDBankAccountStep ( CONST . NON_USD_BANK_ACCOUNT . STEP . COUNTRY ) ;
148+ setReimbursementAccountOptionPressed ( CONST . BANK_ACCOUNT . SETUP_TYPE . EMPTY ) ;
135149 return ;
136150 }
137151
138- setBankAccountSubStep ( CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ) ;
139- goToWithdrawalAccountSetupStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
140- } else if ( optionPressed . current === CONST . BANK_ACCOUNT . SETUP_TYPE . PLAID ) {
152+ prepareNextStep ( CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ) ;
153+ setReimbursementAccountOptionPressed ( CONST . BANK_ACCOUNT . SETUP_TYPE . EMPTY ) ;
154+ } else if ( reimbursementAccountOptionPressed === CONST . BANK_ACCOUNT . SETUP_TYPE . PLAID ) {
141155 openPlaidView ( ) ;
142- goToWithdrawalAccountSetupStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
156+ prepareNextStep ( CONST . BANK_ACCOUNT . SETUP_TYPE . PLAID ) ;
157+ setReimbursementAccountOptionPressed ( CONST . BANK_ACCOUNT . SETUP_TYPE . EMPTY ) ;
143158 }
144- } , [ isAccountValidated , isNonUSDWorkspace , setNonUSDBankAccountStep ] ) ;
159+ } , [ isAccountValidated , isNonUSDWorkspace , prepareNextStep , reimbursementAccountOptionPressed , setNonUSDBankAccountStep ] ) ;
145160
146161 const handleConnectManually = ( ) => {
147162 if ( ! isAccountValidated ) {
148- optionPressed . current = CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ;
163+ setReimbursementAccountOptionPressed ( CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ) ;
149164 toggleValidateCodeActionModal ?.( true ) ;
150165 return ;
151166 }
@@ -156,9 +171,7 @@ function VerifiedBankAccountFlowEntryPoint({
156171 }
157172
158173 removeExistingBankAccountDetails ( ) ;
159- setBankAccountSubStep ( CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ) ;
160- setUSDBankAccountStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
161- goToWithdrawalAccountSetupStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
174+ prepareNextStep ( CONST . BANK_ACCOUNT . SETUP_TYPE . MANUAL ) ;
162175 } ;
163176
164177 const handleConnectPlaid = ( ) => {
@@ -167,15 +180,13 @@ function VerifiedBankAccountFlowEntryPoint({
167180 }
168181
169182 if ( ! isAccountValidated ) {
170- optionPressed . current = CONST . BANK_ACCOUNT . SETUP_TYPE . PLAID ;
183+ setReimbursementAccountOptionPressed ( CONST . BANK_ACCOUNT . SETUP_TYPE . PLAID ) ;
171184 toggleValidateCodeActionModal ?.( true ) ;
172185 return ;
173186 }
174187
175188 removeExistingBankAccountDetails ( ) ;
176- setBankAccountSubStep ( CONST . BANK_ACCOUNT . SETUP_TYPE . PLAID ) ;
177- setUSDBankAccountStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
178- goToWithdrawalAccountSetupStep ( CONST . BANK_ACCOUNT . STEP . COUNTRY ) ;
189+ prepareNextStep ( CONST . BANK_ACCOUNT . SETUP_TYPE . PLAID ) ;
179190 } ;
180191
181192 return (
0 commit comments