11import React , { useCallback , useEffect , useRef } from 'react' ;
2- import { ActivityIndicator , View } from 'react-native' ;
2+ import { ActivityIndicator , InteractionManager , View } from 'react-native' ;
33import type { LinkSuccessMetadata } from 'react-native-plaid-link-sdk' ;
44import type { PlaidLinkOnSuccessMetadata } from 'react-plaid-link/src/types' ;
55import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOfflineBlockingView' ;
@@ -15,16 +15,17 @@ import useThemeStyles from '@hooks/useThemeStyles';
1515import { setAddNewCompanyCardStepAndData , setAssignCardStepAndData } from '@libs/actions/CompanyCards' ;
1616import KeyboardShortcut from '@libs/KeyboardShortcut' ;
1717import Log from '@libs/Log' ;
18+ import { getDomainNameForPolicy } from '@libs/PolicyUtils' ;
1819import Navigation from '@navigation/Navigation' ;
1920import { handleRestrictedEvent } from '@userActions/App' ;
2021import { setPlaidEvent } from '@userActions/BankAccounts' ;
21- import { openPlaidCompanyCardLogin } from '@userActions/Plaid' ;
22+ import { importPlaidAccounts , openPlaidCompanyCardLogin } from '@userActions/Plaid' ;
2223import CONST from '@src/CONST' ;
2324import ONYXKEYS from '@src/ONYXKEYS' ;
2425import type { CompanyCardFeed } from '@src/types/onyx' ;
2526import { isEmptyObject } from '@src/types/utils/EmptyObject' ;
2627
27- function PlaidConnectionStep ( { feed} : { feed ?: CompanyCardFeed } ) {
28+ function PlaidConnectionStep ( { feed, policyID } : { feed ?: CompanyCardFeed ; policyID ?: string } ) {
2829 const { translate} = useLocalize ( ) ;
2930 const styles = useThemeStyles ( ) ;
3031 const theme = useTheme ( ) ;
@@ -39,6 +40,7 @@ function PlaidConnectionStep({feed}: {feed?: CompanyCardFeed}) {
3940 // eslint-disable-next-line @typescript-eslint/non-nullable-type-assertion-style
4041 const plaidDataErrorMessage = ! isEmptyObject ( plaidErrors ) ? ( Object . values ( plaidErrors ) . at ( 0 ) as string ) : '' ;
4142 const { isOffline} = useNetwork ( ) ;
43+ const domain = getDomainNameForPolicy ( policyID ) ;
4244
4345 const isAuthenticatedWithPlaid = useCallback ( ( ) => ! ! plaidData ?. bankAccounts ?. length || ! isEmptyObject ( plaidData ?. errors ) , [ plaidData ] ) ;
4446
@@ -76,7 +78,7 @@ function PlaidConnectionStep({feed}: {feed?: CompanyCardFeed}) {
7678 return unsubscribeToNavigationShortcuts ;
7779 }
7880 if ( addNewCard ?. data ?. selectedCountry ) {
79- openPlaidCompanyCardLogin ( addNewCard . data . selectedCountry ) ;
81+ openPlaidCompanyCardLogin ( addNewCard . data . selectedCountry , domain , feed ) ;
8082 return unsubscribeToNavigationShortcuts ;
8183 }
8284
@@ -88,10 +90,10 @@ function PlaidConnectionStep({feed}: {feed?: CompanyCardFeed}) {
8890 // If we are coming back from offline and we haven't authenticated with Plaid yet, we need to re-run our call to kick off Plaid
8991 // previousNetworkState.current also makes sure that this doesn't run on the first render.
9092 if ( previousNetworkState . current && ! isOffline && ! isAuthenticatedWithPlaid ( ) && addNewCard ?. data ?. selectedCountry ) {
91- openPlaidCompanyCardLogin ( addNewCard . data . selectedCountry ) ;
93+ openPlaidCompanyCardLogin ( addNewCard . data . selectedCountry , domain , feed ) ;
9294 }
9395 previousNetworkState . current = isOffline ;
94- } , [ addNewCard ?. data ?. selectedCountry , isAuthenticatedWithPlaid , isOffline ] ) ;
96+ } , [ addNewCard ?. data ?. selectedCountry , domain , feed , isAuthenticatedWithPlaid , isOffline ] ) ;
9597
9698 const handleBackButtonPress = ( ) => {
9799 if ( feed ) {
@@ -119,6 +121,28 @@ function PlaidConnectionStep({feed}: {feed?: CompanyCardFeed}) {
119121 ( metadata ?. institution as PlaidLinkOnSuccessMetadata [ 'institution' ] ) ?. name ?? ( metadata ?. institution as LinkSuccessMetadata [ 'institution' ] ) ?. name ;
120122
121123 if ( feed ) {
124+ if ( plaidConnectedFeed && addNewCard ?. data ?. selectedCountry && plaidConnectedFeedName ) {
125+ importPlaidAccounts (
126+ publicToken ,
127+ plaidConnectedFeed ,
128+ plaidConnectedFeedName ,
129+ addNewCard . data . selectedCountry ,
130+ getDomainNameForPolicy ( policyID ) ,
131+ JSON . stringify ( metadata ?. accounts ) ,
132+ ) ;
133+ InteractionManager . runAfterInteractions ( ( ) => {
134+ setAssignCardStepAndData ( {
135+ data : {
136+ plaidAccessToken : publicToken ,
137+ institutionId : plaidConnectedFeed ,
138+ plaidConnectedFeedName,
139+ plaidAccounts : metadata ?. accounts ,
140+ } ,
141+ currentStep : CONST . COMPANY_CARD . STEP . BANK_CONNECTION ,
142+ } ) ;
143+ } ) ;
144+ return ;
145+ }
122146 setAssignCardStepAndData ( {
123147 data : {
124148 plaidAccessToken : publicToken ,
0 commit comments