@@ -3,12 +3,14 @@ import type {ComponentType} from 'react';
3
3
import { useOnyx } from 'react-native-onyx' ;
4
4
import InteractiveStepWrapper from '@components/InteractiveStepWrapper' ;
5
5
import useLocalize from '@hooks/useLocalize' ;
6
+ import useNetwork from '@hooks/useNetwork' ;
6
7
import useSubStep from '@hooks/useSubStep' ;
7
8
import NotFoundPage from '@pages/ErrorPage/NotFoundPage' ;
8
9
import { getBankInfoStepValues } from '@pages/ReimbursementAccount/NonUSD/utils/getBankInfoStepValues' ;
9
10
import getInitialSubStepForBankInfoStep from '@pages/ReimbursementAccount/NonUSD/utils/getInitialSubStepForBankInfoStep' ;
10
11
import getInputKeysForBankInfoStep from '@pages/ReimbursementAccount/NonUSD/utils/getInputKeysForBankInfoStep' ;
11
12
import { clearReimbursementAccountBankCreation , createCorpayBankAccount , getCorpayBankAccountFields } from '@userActions/BankAccounts' ;
13
+ import { clearErrors } from '@userActions/FormActions' ;
12
14
import CONST from '@src/CONST' ;
13
15
import ONYXKEYS from '@src/ONYXKEYS' ;
14
16
import type { ReimbursementAccountForm } from '@src/types/form/ReimbursementAccountForm' ;
@@ -35,42 +37,57 @@ function BankInfo({onBackButtonPress, onSubmit, policyID}: BankInfoProps) {
35
37
const { translate} = useLocalize ( ) ;
36
38
37
39
const [ reimbursementAccount ] = useOnyx ( ONYXKEYS . REIMBURSEMENT_ACCOUNT , { canBeMissing : false } ) ;
38
- const [ reimbursementAccountDraft ] = useOnyx ( ONYXKEYS . FORMS . REIMBURSEMENT_ACCOUNT_FORM_DRAFT , { canBeMissing : false } ) ;
39
- const [ corpayFields ] = useOnyx ( ONYXKEYS . CORPAY_FIELDS , { initWithStoredValues : false , canBeMissing : true } ) ;
40
+ const [ reimbursementAccountDraft ] = useOnyx ( ONYXKEYS . FORMS . REIMBURSEMENT_ACCOUNT_FORM_DRAFT , { canBeMissing : true } ) ;
41
+ const [ corpayFields ] = useOnyx ( ONYXKEYS . CORPAY_FIELDS , { canBeMissing : true } ) ;
40
42
const [ policy ] = useOnyx ( `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` , { canBeMissing : false } ) ;
41
43
const currency = policy ?. outputCurrency ?? '' ;
42
- const country = reimbursementAccount ?. achData ?. [ COUNTRY ] ?? reimbursementAccountDraft ?. [ COUNTRY ] ?? '' ;
44
+ const country = reimbursementAccountDraft ?. [ COUNTRY ] ?? reimbursementAccount ?. achData ?. [ COUNTRY ] ?? '' ;
43
45
const inputKeys = getInputKeysForBankInfoStep ( corpayFields ) ;
44
46
const values = useMemo ( ( ) => getBankInfoStepValues ( inputKeys , reimbursementAccountDraft , reimbursementAccount ) , [ inputKeys , reimbursementAccount , reimbursementAccountDraft ] ) ;
45
- const startFrom = getInitialSubStepForBankInfoStep ( values , corpayFields ) ;
47
+ const startFrom = useMemo ( ( ) => getInitialSubStepForBankInfoStep ( values , corpayFields ) , [ corpayFields , values ] ) ;
46
48
47
49
const submit = ( ) => {
48
50
const { formFields, isLoading, isSuccess, ...corpayData } = corpayFields ?? { } ;
49
51
50
52
createCorpayBankAccount ( { ...values , ...corpayData } as ReimbursementAccountForm , policyID ) ;
51
53
} ;
52
54
55
+ useNetwork ( {
56
+ onReconnect : ( ) => {
57
+ getCorpayBankAccountFields ( country , currency ) ;
58
+ } ,
59
+ } ) ;
60
+
53
61
useEffect ( ( ) => {
54
62
if ( reimbursementAccount ?. isLoading === true || ! ! reimbursementAccount ?. errors ) {
55
63
return ;
56
64
}
57
65
58
66
if ( reimbursementAccount ?. isSuccess === true ) {
59
67
onSubmit ( ) ;
68
+ clearReimbursementAccountBankCreation ( ) ;
60
69
}
61
- } , [ onSubmit , reimbursementAccount ?. errors , reimbursementAccount ?. isLoading , reimbursementAccount ?. isSuccess ] ) ;
62
-
63
- useEffect ( ( ) => {
64
- return ( ) => clearReimbursementAccountBankCreation ( ) ;
65
- } , [ ] ) ;
70
+ } , [ corpayFields ?. bankCurrency , country , currency , onSubmit , reimbursementAccount ?. errors , reimbursementAccount ?. isLoading , reimbursementAccount ?. isSuccess ] ) ;
66
71
67
72
useEffect ( ( ) => {
73
+ // No fetching when there is no country
68
74
if ( country === '' ) {
69
75
return ;
70
76
}
71
77
78
+ // When workspace currency is set to EUR we need to refetch if country from Step 1 doesn't match country inside fetched Corpay data
79
+ if ( currency === CONST . CURRENCY . EUR && country !== corpayFields ?. bankCountry ) {
80
+ getCorpayBankAccountFields ( country , currency ) ;
81
+ return ;
82
+ }
83
+
84
+ // No fetching when workspace currency matches the currency inside fetched Corpay
85
+ if ( currency === corpayFields ?. bankCurrency ) {
86
+ return ;
87
+ }
88
+
72
89
getCorpayBankAccountFields ( country , currency ) ;
73
- } , [ country , currency ] ) ;
90
+ } , [ corpayFields ?. bankCurrency , corpayFields ?. bankCountry , country , currency ] ) ;
74
91
75
92
const bodyContent : Array < ComponentType < BankInfoSubStepProps > > = [ BankAccountDetails , AccountHolderDetails , Confirmation ] ;
76
93
@@ -85,6 +102,7 @@ function BankInfo({onBackButtonPress, onSubmit, policyID}: BankInfoProps) {
85
102
} = useSubStep < BankInfoSubStepProps > ( { bodyContent, startFrom, onFinished : submit } ) ;
86
103
87
104
const handleBackButtonPress = ( ) => {
105
+ clearErrors ( ONYXKEYS . FORMS . REIMBURSEMENT_ACCOUNT_FORM ) ;
88
106
if ( isEditing ) {
89
107
goToTheLastStep ( ) ;
90
108
return ;
0 commit comments