11import React , { useCallback , useEffect , useMemo , useState } from 'react' ;
2+ import { ActivityIndicator } from 'react-native' ;
23import { useOnyx } from 'react-native-onyx' ;
34import BlockingView from '@components/BlockingViews/BlockingView' ;
45import FullPageOfflineBlockingView from '@components/BlockingViews/FullPageOfflineBlockingView' ;
@@ -8,10 +9,12 @@ import ScreenWrapper from '@components/ScreenWrapper';
89import Text from '@components/Text' ;
910import TextLink from '@components/TextLink' ;
1011import useCardFeeds from '@hooks/useCardFeeds' ;
12+ import useImportPlaidAccounts from '@hooks/useImportPlaidAccounts' ;
1113import useLocalize from '@hooks/useLocalize' ;
1214import useNetwork from '@hooks/useNetwork' ;
1315import usePermissions from '@hooks/usePermissions' ;
1416import usePrevious from '@hooks/usePrevious' ;
17+ import useTheme from '@hooks/useTheme' ;
1518import useThemeStyles from '@hooks/useThemeStyles' ;
1619import { setAssignCardStepAndData } from '@libs/actions/CompanyCards' ;
1720import { checkIfNewFeedConnected , getBankName , isSelectedFeedExpired } from '@libs/CardUtils' ;
@@ -20,7 +23,7 @@ import type {PlatformStackRouteProp} from '@navigation/PlatformStackNavigation/t
2023import type { SettingsNavigatorParamList } from '@navigation/types' ;
2124import { updateSelectedFeed } from '@userActions/Card' ;
2225import { setAddNewCompanyCardStepAndData } from '@userActions/CompanyCards' ;
23- import { getCompanyCardBankConnection , getCompanyCardPlaidConnection } from '@userActions/getCompanyCardBankConnection' ;
26+ import { getCompanyCardBankConnection } from '@userActions/getCompanyCardBankConnection' ;
2427import CONST from '@src/CONST' ;
2528import ONYXKEYS from '@src/ONYXKEYS' ;
2629import ROUTES from '@src/ROUTES' ;
@@ -46,6 +49,7 @@ function BankConnection({policyID: policyIDFromProps, feed, route}: BankConnecti
4649 const { translate} = useLocalize ( ) ;
4750 const [ addNewCard ] = useOnyx ( ONYXKEYS . ADD_NEW_COMPANY_CARD , { canBeMissing : true } ) ;
4851 const [ assignCard ] = useOnyx ( ONYXKEYS . ASSIGN_CARD , { canBeMissing : true } ) ;
52+ const theme = useTheme ( ) ;
4953 const { bankName : bankNameFromRoute , backTo, policyID : policyIDFromRoute } = route ?. params ?? { } ;
5054 const policyID = policyIDFromProps ?? policyIDFromRoute ;
5155 const [ cardFeeds ] = useCardFeeds ( policyID ) ;
@@ -59,17 +63,14 @@ function BankConnection({policyID: policyIDFromProps, feed, route}: BankConnecti
5963 ) ;
6064 const { isOffline} = useNetwork ( ) ;
6165 const plaidToken = addNewCard ?. data ?. publicToken ?? assignCard ?. data ?. plaidAccessToken ;
62- const plaidFeed = addNewCard ?. data ?. plaidConnectedFeed ?? assignCard ?. data ?. institutionId ;
63- const plaidAccounts = addNewCard ?. data ?. plaidAccounts ?? assignCard ?. data ?. plaidAccounts ;
64- const plaidFeedName = addNewCard ?. data ?. plaidConnectedFeedName ?? assignCard ?. data ?. plaidConnectedFeedName ;
65- const country = addNewCard ?. data ?. selectedCountry ;
6666 const { isBetaEnabled} = usePermissions ( ) ;
6767 const isPlaid = isBetaEnabled ( CONST . BETAS . PLAID_COMPANY_CARDS ) && ! ! plaidToken ;
6868
69- const url = isPlaid ? getCompanyCardPlaidConnection ( policyID , plaidToken , plaidFeed , plaidFeedName , country , plaidAccounts ) : getCompanyCardBankConnection ( policyID , bankName ) ;
69+ const url = getCompanyCardBankConnection ( policyID , bankName ) ;
7070 const isFeedExpired = feed ? isSelectedFeedExpired ( cardFeeds ?. settings ?. oAuthAccountDetails ?. [ feed ] ) : false ;
7171 const headerTitleAddCards = ! backTo ? translate ( 'workspace.companyCards.addCards' ) : undefined ;
7272 const headerTitle = feed ? translate ( 'workspace.companyCards.assignCard' ) : headerTitleAddCards ;
73+ const onImportPlaidAccounts = useImportPlaidAccounts ( policyID ) ;
7374
7475 const onOpenBankConnectionFlow = useCallback ( ( ) => {
7576 if ( ! url ) {
@@ -114,7 +115,7 @@ function BankConnection({policyID: policyIDFromProps, feed, route}: BankConnecti
114115 ) ;
115116
116117 useEffect ( ( ) => {
117- if ( ! url || isOffline ) {
118+ if ( ( ! url && ! isPlaid ) || isOffline ) {
118119 return ;
119120 }
120121
@@ -128,8 +129,14 @@ function BankConnection({policyID: policyIDFromProps, feed, route}: BankConnecti
128129 } ) ;
129130 return ;
130131 }
131- customWindow = openBankConnection ( url ) ;
132- return ;
132+ if ( isPlaid ) {
133+ onImportPlaidAccounts ( ) ;
134+ return ;
135+ }
136+ if ( url ) {
137+ customWindow = openBankConnection ( url ) ;
138+ return ;
139+ }
133140 }
134141
135142 // Handle add new card flow
@@ -144,9 +151,15 @@ function BankConnection({policyID: policyIDFromProps, feed, route}: BankConnecti
144151 return ;
145152 }
146153 if ( ! shouldBlockWindowOpen ) {
147- customWindow = openBankConnection ( url ) ;
154+ if ( isPlaid ) {
155+ onImportPlaidAccounts ( ) ;
156+ return ;
157+ }
158+ if ( url ) {
159+ customWindow = openBankConnection ( url ) ;
160+ }
148161 }
149- } , [ isNewFeedConnected , shouldBlockWindowOpen , newFeed , policyID , url , feed , isFeedExpired , isOffline , assignCard ?. data ?. dateOption ] ) ;
162+ } , [ isNewFeedConnected , shouldBlockWindowOpen , newFeed , policyID , url , feed , isFeedExpired , isOffline , assignCard ?. data ?. dateOption , isPlaid , onImportPlaidAccounts ] ) ;
150163
151164 return (
152165 < ScreenWrapper
@@ -158,15 +171,23 @@ function BankConnection({policyID: policyIDFromProps, feed, route}: BankConnecti
158171 onBackButtonPress = { handleBackButtonPress }
159172 />
160173 < FullPageOfflineBlockingView addBottomSafeAreaPadding >
161- < BlockingView
162- icon = { PendingBank }
163- iconWidth = { styles . pendingBankCardIllustration . width }
164- iconHeight = { styles . pendingBankCardIllustration . height }
165- title = { translate ( 'workspace.moreFeatures.companyCards.pendingBankTitle' ) }
166- CustomSubtitle = { CustomSubtitle }
167- onLinkPress = { onOpenBankConnectionFlow }
168- addBottomSafeAreaPadding
169- />
174+ { isPlaid ? (
175+ < ActivityIndicator
176+ size = { CONST . ACTIVITY_INDICATOR_SIZE . LARGE }
177+ style = { styles . flex1 }
178+ color = { theme . spinner }
179+ />
180+ ) : (
181+ < BlockingView
182+ icon = { PendingBank }
183+ iconWidth = { styles . pendingBankCardIllustration . width }
184+ iconHeight = { styles . pendingBankCardIllustration . height }
185+ title = { translate ( 'workspace.moreFeatures.companyCards.pendingBankTitle' ) }
186+ CustomSubtitle = { CustomSubtitle }
187+ onLinkPress = { onOpenBankConnectionFlow }
188+ addBottomSafeAreaPadding
189+ />
190+ ) }
170191 </ FullPageOfflineBlockingView >
171192 </ ScreenWrapper >
172193 ) ;
0 commit comments