From 012840de587803dbfbf4dc6d0776e01bf6c5c6f0 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 15 Nov 2024 17:04:44 +0700 Subject: [PATCH 1/2] fix: get all the cards from different feeds --- src/libs/CardUtils.ts | 23 ++++++++++++++++++- .../members/WorkspaceMemberDetailsPage.tsx | 21 ++++------------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/libs/CardUtils.ts b/src/libs/CardUtils.ts index d56477c3f148..9b49194e3c84 100644 --- a/src/libs/CardUtils.ts +++ b/src/libs/CardUtils.ts @@ -1,6 +1,6 @@ import groupBy from 'lodash/groupBy'; 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 ExpensifyCardImage from '@assets/images/expensify-card.svg'; import * as Illustrations from '@src/components/Icon/Illustrations'; @@ -28,6 +28,15 @@ Onyx.connect({ }, }); +let allWorkspaceCards: OnyxCollection = {}; +Onyx.connect({ + key: ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST, + waitForCollectionCallback: true, + callback: (value) => { + allWorkspaceCards = value; + }, +}); + /** * @returns string with a month in MM format */ @@ -341,6 +350,17 @@ function getSelectedFeed(lastSelectedFeed: OnyxEntry, cardFeeds return lastSelectedFeed ?? defaultFeed; } +function getAllCardsForWorkspace(workspaceAccountID: number): CardList { + const cards = {}; + for (const [key, values] of Object.entries(allWorkspaceCards ?? {})) { + if (key.includes(workspaceAccountID.toString()) && values) { + const {cardList, ...rest} = values; + Object.assign(cards, rest); + } + } + return cards; +} + export { isExpensifyCard, isCorporateCard, @@ -367,4 +387,5 @@ export { getCorrectStepForSelectedBank, getCustomOrFormattedFeedName, removeExpensifyCardFromCompanyCards, + getAllCardsForWorkspace, }; diff --git a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx index b0066187e6f1..bd3408988be0 100644 --- a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx +++ b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx @@ -61,8 +61,6 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM const {translate} = useLocalize(); const StyleUtils = useStyleUtils(); const currentUserPersonalDetails = useCurrentUserPersonalDetails(); - const [cards] = useOnyx(`${ONYXKEYS.CARD_LIST}`); - const [expensifyCards] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}${workspaceAccountID}_${CONST.EXPENSIFY_CARD.BANK}`); const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`); const [cardSettings] = useOnyx(`${ONYXKEYS.COLLECTION.PRIVATE_EXPENSIFY_CARD_SETTINGS}${workspaceAccountID}`); @@ -85,27 +83,18 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM const hasMultipleFeeds = Object.values(CardUtils.getCompanyFeeds(cardFeeds)).filter((feed) => !feed.pending).length > 0; const paymentAccountID = cardSettings?.paymentBankAccountID ?? 0; + const workspaceCards = CardUtils.getAllCardsForWorkspace(workspaceAccountID); + useEffect(() => { CompanyCards.openPolicyCompanyCardsPage(policyID, workspaceAccountID); }, [policyID, workspaceAccountID]); const memberCards = useMemo(() => { - if (!cards && !expensifyCards) { + if (!workspaceCards) { return []; } - // For admin Expensify Cards can also appear in the cards list, so we need to remove duplicates - const allCards = [...Object.values(cards ?? {}), ...Object.values(expensifyCards ?? {})]; - const cardIDs = new Set(); - const uniqueObjects = allCards.filter((obj) => { - if (cardIDs.has(obj.cardID)) { - return false; - } - cardIDs.add(obj.cardID); - return true; - }); - - return Object.values(uniqueObjects ?? {}).filter((card) => card.accountID === accountID && workspaceAccountID.toString() === card.fundID); - }, [accountID, workspaceAccountID, cards, expensifyCards]); + return Object.values(workspaceCards ?? {}).filter((card) => card.accountID === accountID); + }, [accountID, workspaceAccountID, workspaceCards]); const confirmModalPrompt = useMemo(() => { const isApprover = Member.isApprover(policy, accountID); From eb859fc1fb1c8fefd233ef80ae0676bc04b956a8 Mon Sep 17 00:00:00 2001 From: Agata Kosior Date: Fri, 15 Nov 2024 17:08:54 +0700 Subject: [PATCH 2/2] fix: remove unnecessary dep --- src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx index bd3408988be0..38f876089c42 100644 --- a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx +++ b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx @@ -94,7 +94,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM return []; } return Object.values(workspaceCards ?? {}).filter((card) => card.accountID === accountID); - }, [accountID, workspaceAccountID, workspaceCards]); + }, [accountID, workspaceCards]); const confirmModalPrompt = useMemo(() => { const isApprover = Member.isApprover(policy, accountID);