Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
VickyStash committed Nov 26, 2024
1 parent 29aeb8b commit a194899
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 24 deletions.
11 changes: 11 additions & 0 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,16 @@ function getDefaultCardName(cardholder?: string) {
return `${cardholder}'s card`;
}

function checkIfNewFeedConnected(prevFeedsData: CompanyFeeds, currentFeedsData: CompanyFeeds) {
const prevFeeds = Object.keys(prevFeedsData);
const currentFeeds = Object.keys(currentFeedsData);

return {
isNewFeedConnected: currentFeeds.length > prevFeeds.length,
newFeed: currentFeeds.find((feed) => !prevFeeds.includes(feed)) as CompanyCardFeed | undefined,
};
}

export {
isExpensifyCard,
isCorporateCard,
Expand Down Expand Up @@ -396,5 +406,6 @@ export {
removeExpensifyCardFromCompanyCards,
getFilteredCardList,
hasOnlyOneCardToAssign,
checkIfNewFeedConnected,
getDefaultCardName,
};
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useEffect, useRef, useState} from 'react';
import React, {useEffect, useMemo, useRef, useState} from 'react';
import {useOnyx} from 'react-native-onyx';
import {WebView} from 'react-native-webview';
import type {ValueOf} from 'type-fest';
Expand All @@ -7,6 +7,8 @@ import FullScreenLoadingIndicator from '@components/FullscreenLoadingIndicator';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import Modal from '@components/Modal';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import * as CardUtils from '@libs/CardUtils';
import getUAForWebView from '@libs/getUAForWebView';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
Expand All @@ -16,7 +18,6 @@ import getCompanyCardBankConnection from '@userActions/getCompanyCardBankConnect
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import {isEmptyObject} from '@src/types/utils/EmptyObject';

type BankConnectionStepProps = {
policyID?: string;
Expand All @@ -33,9 +34,8 @@ function BankConnection({policyID}: BankConnectionStepProps) {
const url = getCompanyCardBankConnection(policyID, bankName);
const workspaceAccountID = PolicyUtils.getWorkspaceAccountID(policyID ?? '-1');
const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`);
const bankKey = Object.keys(CONST.COMPANY_CARDS.BANKS).find((value) => CONST.COMPANY_CARDS.BANKS?.[value as keyof typeof CONST.COMPANY_CARDS.BANKS] === bankName);
const feedName = bankKey && bankKey !== CONST.COMPANY_CARDS.BANKS.OTHER ? CONST.COMPANY_CARD.FEED_BANK_NAME?.[bankKey as keyof typeof CONST.COMPANY_CARD.FEED_BANK_NAME] : undefined;
const connectedBank = feedName ? cardFeeds?.settings?.oAuthAccountDetails?.[feedName] : undefined;
const prevFeedsData = usePrevious(cardFeeds?.settings?.oAuthAccountDetails);
const {isNewFeedConnected, newFeed} = useMemo(() => CardUtils.checkIfNewFeedConnected(prevFeedsData ?? {}, cardFeeds?.settings?.oAuthAccountDetails ?? {}), [cardFeeds, prevFeedsData]);

const renderLoading = () => <FullScreenLoadingIndicator />;

Expand All @@ -60,11 +60,13 @@ function BankConnection({policyID}: BankConnectionStepProps) {
if (!url) {
return;
}
if (feedName && connectedBank && !isEmptyObject(connectedBank)) {
Card.updateSelectedFeed(feedName, policyID ?? '-1');
if (isNewFeedConnected) {
if (newFeed) {
Card.updateSelectedFeed(newFeed, policyID ?? '-1');
}
Navigation.navigate(ROUTES.WORKSPACE_COMPANY_CARDS.getRoute(policyID ?? '-1'));
}
}, [connectedBank, feedName, policyID, url]);
}, [isNewFeedConnected, newFeed, policyID, url]);

return (
<Modal
Expand Down
19 changes: 3 additions & 16 deletions src/pages/workspace/companyCards/addNew/BankConnection/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import TextLink from '@components/TextLink';
import useLocalize from '@hooks/useLocalize';
import usePrevious from '@hooks/usePrevious';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CardUtils from '@libs/CardUtils';
import Navigation from '@libs/Navigation/Navigation';
import * as PolicyUtils from '@libs/PolicyUtils';
import getCurrentUrl from '@navigation/currentUrl';
Expand All @@ -19,7 +20,6 @@ import getCompanyCardBankConnection from '@userActions/getCompanyCardBankConnect
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {CompanyCardFeed} from '@src/types/onyx';
import openBankConnection from './openBankConnection';

let customWindow: Window | null = null;
Expand All @@ -35,21 +35,8 @@ function BankConnection({policyID}: BankConnectionStepProps) {
const bankName: ValueOf<typeof CONST.COMPANY_CARDS.BANKS> | undefined = addNewCard?.data?.selectedBank;
const workspaceAccountID = PolicyUtils.getWorkspaceAccountID(policyID ?? '-1');
const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`);
const bankKey = Object.keys(CONST.COMPANY_CARDS.BANKS).find((value) => CONST.COMPANY_CARDS.BANKS?.[value as keyof typeof CONST.COMPANY_CARDS.BANKS] === bankName);
const feedName = bankKey && bankKey !== CONST.COMPANY_CARDS.BANKS.OTHER ? CONST.COMPANY_CARD.FEED_BANK_NAME?.[bankKey as keyof typeof CONST.COMPANY_CARD.FEED_BANK_NAME] : undefined;
const connectedBank = feedName ? cardFeeds?.settings?.oAuthAccountDetails?.[feedName] : undefined;
const prevFeedsData = usePrevious(cardFeeds?.settings?.oAuthAccountDetails);
const {isNewFeedConnected, newFeed} = useMemo(() => {
const prevFeeds = Object.keys(prevFeedsData ?? {});
const currentFeeds = Object.keys(cardFeeds?.settings?.oAuthAccountDetails ?? {});
const isNewFeed = currentFeeds.length > prevFeeds.length;

if (!isNewFeed) {
return {isNewFeedConnected: false};
}

return {isNewFeedConnected: true, newFeed: currentFeeds.find((feed) => !prevFeeds.includes(feed)) as CompanyCardFeed};
}, [cardFeeds, prevFeedsData]);
const {isNewFeedConnected, newFeed} = useMemo(() => CardUtils.checkIfNewFeedConnected(prevFeedsData ?? {}, cardFeeds?.settings?.oAuthAccountDetails ?? {}), [cardFeeds, prevFeedsData]);

const currentUrl = getCurrentUrl();
const isBankConnectionCompleteRoute = currentUrl.includes(ROUTES.BANK_CONNECTION_COMPLETE);
Expand Down Expand Up @@ -99,7 +86,7 @@ function BankConnection({policyID}: BankConnectionStepProps) {
return;
}
customWindow = openBankConnection(url);
}, [connectedBank, feedName, isBankConnectionCompleteRoute, policyID, url]);
}, [isNewFeedConnected, newFeed, isBankConnectionCompleteRoute, policyID, url]);

return (
<ScreenWrapper testID={BankConnection.displayName}>
Expand Down

0 comments on commit a194899

Please sign in to comment.