From e15ad9600933fb82fdd210d4efaa2b346ed00372 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 13 Sep 2024 14:55:25 +0200 Subject: [PATCH 1/3] Fix crash after enabling Expensify Card --- src/libs/CardUtils.ts | 22 ++++++++++++++++++- .../workspace/WorkspaceMoreFeaturesPage.tsx | 4 ++-- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/src/libs/CardUtils.ts b/src/libs/CardUtils.ts index 2398bc1e729a..100eb6a4cd2a 100644 --- a/src/libs/CardUtils.ts +++ b/src/libs/CardUtils.ts @@ -1,6 +1,6 @@ import lodash from 'lodash'; import Onyx from 'react-native-onyx'; -import type {OnyxEntry} from 'react-native-onyx'; +import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import type {ValueOf} from 'type-fest'; import * as Illustrations from '@src/components/Icon/Illustrations'; import CONST from '@src/CONST'; @@ -26,6 +26,14 @@ Onyx.connect({ }, }); +let allCardsLists: OnyxCollection; + +Onyx.connect({ + key: ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST, + waitForCollectionCallback: true, + callback: (value) => (allCardsLists = value), +}); + /** * @returns string with a month in MM format */ @@ -194,6 +202,17 @@ function getCardFeedIcon(cardFeed: string): IconAsset { return Illustrations.AmexCompanyCards; } +/** Checks if the Expensify Card toggle should be disabled */ +function shouldExpensifyCardToggleBeDisabled(workspaceAccountID?: number, areExpensifyCardsEnabled?: boolean): boolean { + if (!areExpensifyCardsEnabled || !workspaceAccountID) { + return false; + } + + const cardsList = allCardsLists?.[`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`]; + + return !isEmptyObject(cardsList); +} + export { isExpensifyCard, isCorporateCard, @@ -210,4 +229,5 @@ export { getEligibleBankAccountsForCard, sortCardsByCardholderName, getCardFeedIcon, + shouldExpensifyCardToggleBeDisabled, }; diff --git a/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx b/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx index 0182f1ea8827..bd6f9ce158f3 100644 --- a/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx +++ b/src/pages/workspace/WorkspaceMoreFeaturesPage.tsx @@ -15,6 +15,7 @@ import useNetwork from '@hooks/useNetwork'; import usePermissions from '@hooks/usePermissions'; import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useThemeStyles from '@hooks/useThemeStyles'; +import * as CardUtils from '@libs/CardUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import Navigation from '@libs/Navigation/Navigation'; import type {FullScreenNavigatorParamList} from '@libs/Navigation/types'; @@ -71,7 +72,6 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro !!policy?.connections?.netsuite?.options?.config?.syncOptions?.syncTax; const policyID = policy?.id; const workspaceAccountID = policy?.workspaceAccountID ?? -1; - const [cardsList] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID.toString()}_${CONST.EXPENSIFY_CARD.BANK}`); const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID.toString()}`); const [isOrganizeWarningModalOpen, setIsOrganizeWarningModalOpen] = useState(false); const [isIntegrateWarningModalOpen, setIsIntegrateWarningModalOpen] = useState(false); @@ -110,7 +110,7 @@ function WorkspaceMoreFeaturesPage({policy, route}: WorkspaceMoreFeaturesPagePro subtitleTranslationKey: 'workspace.moreFeatures.expensifyCard.subtitle', isActive: policy?.areExpensifyCardsEnabled ?? false, pendingAction: policy?.pendingFields?.areExpensifyCardsEnabled, - disabled: !isEmptyObject(cardsList), + disabled: CardUtils.shouldExpensifyCardToggleBeDisabled(policy?.workspaceAccountID, policy?.areExpensifyCardsEnabled), action: (isEnabled: boolean) => { if (!policyID) { return; From 64b458add07fd86b0e85f5ee06cd43c1de36e35a Mon Sep 17 00:00:00 2001 From: Vit Horacek <36083550+mountiny@users.noreply.github.com> Date: Sat, 14 Sep 2024 00:06:50 +0200 Subject: [PATCH 2/3] Update src/libs/CardUtils.ts --- src/libs/CardUtils.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/libs/CardUtils.ts b/src/libs/CardUtils.ts index 359188cca4c6..8287064d9ff6 100644 --- a/src/libs/CardUtils.ts +++ b/src/libs/CardUtils.ts @@ -212,6 +212,7 @@ function shouldExpensifyCardToggleBeDisabled(workspaceAccountID?: number, areExp const cardsList = allCardsLists?.[`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`]; return !isEmptyObject(cardsList); +} function getCardDetailsImage(cardFeed: string): IconAsset { if (cardFeed.startsWith(CONST.COMPANY_CARD.FEED_BANK_NAME.MASTER_CARD)) { From f5b68f9fff87b094f5b2054a4726e0092052f6c4 Mon Sep 17 00:00:00 2001 From: Vit Horacek <36083550+mountiny@users.noreply.github.com> Date: Sat, 14 Sep 2024 00:06:55 +0200 Subject: [PATCH 3/3] Update src/libs/CardUtils.ts --- src/libs/CardUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/CardUtils.ts b/src/libs/CardUtils.ts index 8287064d9ff6..58dbc07aae8d 100644 --- a/src/libs/CardUtils.ts +++ b/src/libs/CardUtils.ts @@ -244,7 +244,6 @@ function getMemberCards(policy: OnyxEntry, allCardsList: OnyxCollection< } }); return cards; - } export {