1- import React , { useCallback , useState } from 'react' ;
1+ import React , { useCallback , useEffect } from 'react' ;
22import CheckboxWithLabel from '@components/CheckboxWithLabel' ;
33import FormProvider from '@components/Form/FormProvider' ;
44import InputWrapper from '@components/Form/InputWrapper' ;
@@ -13,8 +13,9 @@ import useNetwork from '@hooks/useNetwork';
1313import useOnyx from '@hooks/useOnyx' ;
1414import useThemeStyles from '@hooks/useThemeStyles' ;
1515import { getCurrencySymbol } from '@libs/CurrencyUtils' ;
16+ import { getLatestErrorMessage } from '@libs/ErrorUtils' ;
1617import type CustomSubStepProps from '@pages/settings/Wallet/InternationalDepositAccount/types' ;
17- import { createCorpayBankAccountForWalletFlow } from '@userActions/BankAccounts' ;
18+ import { clearReimbursementAccountBankCreation , createCorpayBankAccountForWalletFlow } from '@userActions/BankAccounts' ;
1819import CONST from '@src/CONST' ;
1920import type { TranslationPaths } from '@src/languages/types' ;
2021import ONYXKEYS from '@src/ONYXKEYS' ;
@@ -44,9 +45,8 @@ function TermsAndConditionsLabel() {
4445function Confirmation ( { onNext, onMove, formValues, fieldsMap} : CustomSubStepProps ) {
4546 const { translate} = useLocalize ( ) ;
4647 const styles = useThemeStyles ( ) ;
47- const [ isSubmitting , setIsSubmitting ] = useState ( false ) ;
48- const [ error , setError ] = useState ( '' ) ;
4948 const [ corpayFields ] = useOnyx ( ONYXKEYS . CORPAY_FIELDS , { canBeMissing : false } ) ;
49+ const [ reimbursementAccount ] = useOnyx ( ONYXKEYS . REIMBURSEMENT_ACCOUNT , { canBeMissing : false } ) ;
5050 const { isOffline} = useNetwork ( ) ;
5151
5252 const getTitle = ( field : CorpayFormField , fieldName : string ) => {
@@ -62,25 +62,20 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
6262 } ;
6363
6464 const getDataAndGoToNextStep = ( values : FormOnyxValues < typeof ONYXKEYS . FORMS . INTERNATIONAL_BANK_ACCOUNT_FORM > ) => {
65- setError ( '' ) ;
66- setIsSubmitting ( true ) ;
67- createCorpayBankAccountForWalletFlow (
68- { ...formValues , ...values } ,
69- corpayFields ?. classification ?? '' ,
70- corpayFields ?. destinationCountry ?? '' ,
71- corpayFields ?. preferredMethod ?? '' ,
72- ) . then ( ( response ) => {
73- setIsSubmitting ( false ) ;
74- if ( response ?. jsonCode ) {
75- if ( response . jsonCode === CONST . JSON_CODE . SUCCESS ) {
76- onNext ( ) ;
77- } else {
78- setError ( response . message ?? '' ) ;
79- }
80- }
81- } ) ;
65+ createCorpayBankAccountForWalletFlow ( { ...formValues , ...values } , corpayFields ?. classification ?? '' , corpayFields ?. destinationCountry ?? '' , corpayFields ?. preferredMethod ?? '' ) ;
8266 } ;
8367
68+ useEffect ( ( ) => {
69+ if ( reimbursementAccount ?. isLoading === true || ! ! reimbursementAccount ?. errors ) {
70+ return ;
71+ }
72+
73+ if ( reimbursementAccount ?. isSuccess === true ) {
74+ onNext ( ) ;
75+ clearReimbursementAccountBankCreation ( ) ;
76+ }
77+ } , [ reimbursementAccount ?. isLoading , reimbursementAccount ?. isSuccess , reimbursementAccount ?. errors , onNext ] ) ;
78+
8479 const summaryItems : MenuItemProps [ ] = [
8580 {
8681 description : translate ( 'common.country' ) ,
@@ -162,6 +157,8 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
162157 [ translate ] ,
163158 ) ;
164159
160+ const errorMessage = getLatestErrorMessage ( reimbursementAccount ) ;
161+
165162 return (
166163 < ScrollView contentContainerStyle = { styles . flexGrow1 } >
167164 < Text style = { [ styles . textHeadlineLineHeightXXL , styles . ph5 , styles . mb3 ] } > { translate ( 'addPersonalBankAccount.confirmationStepHeader' ) } </ Text >
@@ -185,7 +182,7 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
185182 submitButtonText = { translate ( 'common.confirm' ) }
186183 style = { [ styles . mh5 , styles . flexGrow1 ] }
187184 enabledWhenOffline = { false }
188- isLoading = { isSubmitting }
185+ isLoading = { reimbursementAccount ?. isLoading }
189186 shouldHideFixErrorsAlert
190187 >
191188 < InputWrapper
@@ -196,11 +193,13 @@ function Confirmation({onNext, onMove, formValues, fieldsMap}: CustomSubStepProp
196193 style = { [ styles . mt3 ] }
197194 shouldSaveDraft
198195 />
199- < FormHelpMessage
200- style = { [ styles . mt3 , styles . mbn1 ] }
201- isError
202- message = { error }
203- />
196+ { ! ! errorMessage && (
197+ < FormHelpMessage
198+ style = { [ styles . mt3 , styles . mbn1 ] }
199+ isError
200+ message = { errorMessage }
201+ />
202+ ) }
204203 </ FormProvider >
205204 </ ScrollView >
206205 ) ;
0 commit comments