|
| 1 | +import React, {useCallback, useEffect} from 'react'; |
| 2 | +import {useOnyx} from 'react-native-onyx'; |
| 3 | +import type {ValueOf} from 'type-fest'; |
| 4 | +import BlockingView from '@components/BlockingViews/BlockingView'; |
| 5 | +import HeaderWithBackButton from '@components/HeaderWithBackButton'; |
| 6 | +import * as Illustrations from '@components/Icon/Illustrations'; |
| 7 | +import ScreenWrapper from '@components/ScreenWrapper'; |
| 8 | +import Text from '@components/Text'; |
| 9 | +import TextLink from '@components/TextLink'; |
| 10 | +import useLocalize from '@hooks/useLocalize'; |
| 11 | +import useThemeStyles from '@hooks/useThemeStyles'; |
| 12 | +import getCurrentUrl from '@navigation/currentUrl'; |
| 13 | +import * as CompanyCards from '@userActions/CompanyCards'; |
| 14 | +import getCompanyCardBankConnection from '@userActions/getCompanyCardBankConnection'; |
| 15 | +import CONST from '@src/CONST'; |
| 16 | +import ONYXKEYS from '@src/ONYXKEYS'; |
| 17 | +import ROUTES from '@src/ROUTES'; |
| 18 | +import openBankConnection from './openBankConnection'; |
| 19 | + |
| 20 | +let customWindow: Window | null = null; |
| 21 | + |
| 22 | +function BankConnection() { |
| 23 | + const styles = useThemeStyles(); |
| 24 | + const {translate} = useLocalize(); |
| 25 | + const [addNewCard] = useOnyx(ONYXKEYS.ADD_NEW_COMPANY_CARD); |
| 26 | + const bankName: ValueOf<typeof CONST.COMPANY_CARDS.BANKS> | undefined = addNewCard?.data?.selectedBank; |
| 27 | + const currentUrl = getCurrentUrl(); |
| 28 | + const isBankConnectionCompleteRoute = currentUrl.includes(ROUTES.BANK_CONNECTION_COMPLETE); |
| 29 | + const url = getCompanyCardBankConnection(bankName); |
| 30 | + |
| 31 | + const onOpenBankConnectionFlow = useCallback(() => { |
| 32 | + if (!url) { |
| 33 | + return; |
| 34 | + } |
| 35 | + customWindow = openBankConnection(url); |
| 36 | + }, [url]); |
| 37 | + |
| 38 | + const handleBackButtonPress = () => { |
| 39 | + customWindow?.close(); |
| 40 | + if (bankName === CONST.COMPANY_CARDS.BANKS.BREX) { |
| 41 | + CompanyCards.setAddNewCompanyCardStepAndData({step: CONST.COMPANY_CARDS.STEP.SELECT_BANK}); |
| 42 | + return; |
| 43 | + } |
| 44 | + if (bankName === CONST.COMPANY_CARDS.BANKS.AMEX) { |
| 45 | + CompanyCards.setAddNewCompanyCardStepAndData({step: CONST.COMPANY_CARDS.STEP.AMEX_CUSTOM_FEED}); |
| 46 | + return; |
| 47 | + } |
| 48 | + CompanyCards.setAddNewCompanyCardStepAndData({step: CONST.COMPANY_CARDS.STEP.SELECT_FEED_TYPE}); |
| 49 | + }; |
| 50 | + |
| 51 | + const CustomSubtitle = ( |
| 52 | + <Text style={[styles.textAlignCenter, styles.textSupporting]}> |
| 53 | + {bankName && translate(`workspace.moreFeatures.companyCards.pendingBankDescription`, {bankName})} |
| 54 | + <TextLink onPress={onOpenBankConnectionFlow}>{translate('workspace.moreFeatures.companyCards.pendingBankLink')}</TextLink> |
| 55 | + </Text> |
| 56 | + ); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (!url) { |
| 60 | + return; |
| 61 | + } |
| 62 | + if (isBankConnectionCompleteRoute) { |
| 63 | + customWindow?.close(); |
| 64 | + return; |
| 65 | + } |
| 66 | + customWindow = openBankConnection(url); |
| 67 | + }, [isBankConnectionCompleteRoute, url]); |
| 68 | + |
| 69 | + return ( |
| 70 | + <ScreenWrapper testID={BankConnection.displayName}> |
| 71 | + <HeaderWithBackButton |
| 72 | + title={translate('workspace.companyCards.addCardFeed')} |
| 73 | + onBackButtonPress={handleBackButtonPress} |
| 74 | + /> |
| 75 | + <BlockingView |
| 76 | + icon={Illustrations.PendingBank} |
| 77 | + iconWidth={styles.pendingBankCardIllustration.width} |
| 78 | + iconHeight={styles.pendingBankCardIllustration.height} |
| 79 | + title={translate('workspace.moreFeatures.companyCards.pendingBankTitle')} |
| 80 | + linkKey="workspace.moreFeatures.companyCards.pendingBankLink" |
| 81 | + CustomSubtitle={CustomSubtitle} |
| 82 | + shouldShowLink |
| 83 | + onLinkPress={onOpenBankConnectionFlow} |
| 84 | + /> |
| 85 | + </ScreenWrapper> |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +BankConnection.displayName = 'BankConnection'; |
| 90 | + |
| 91 | +export default BankConnection; |
0 commit comments