From ed7a9e396eb9e242549fca4c0b349ca76766ad6e Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 21 Nov 2024 13:03:15 +0100 Subject: [PATCH 1/4] Navigate to some other existing feed after removing one --- src/libs/actions/CompanyCards.ts | 36 +++++++++++-------- .../WorkspaceCompanyCardsSettingsPage.tsx | 4 ++- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/libs/actions/CompanyCards.ts b/src/libs/actions/CompanyCards.ts index 4a102ab9bb72..8e83b9192a71 100644 --- a/src/libs/actions/CompanyCards.ts +++ b/src/libs/actions/CompanyCards.ts @@ -151,27 +151,33 @@ function setWorkspaceCompanyCardTransactionLiability(workspaceAccountID: number, API.write(WRITE_COMMANDS.SET_COMPANY_CARD_TRANSACTION_LIABILITY, parameters, onyxData); } -function deleteWorkspaceCompanyCardFeed(policyID: string, workspaceAccountID: number, bankName: CompanyCardFeed) { +function deleteWorkspaceCompanyCardFeed(policyID: string, workspaceAccountID: number, bankName: CompanyCardFeed, feedToOpen?: CompanyCardFeed) { const authToken = NetworkStore.getAuthToken(); const isCustomFeed = CardUtils.isCustomFeed(bankName); const feedUpdates = {[bankName]: null}; - const onyxData: OnyxData = { - optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`, - value: { - settings: { - ...(isCustomFeed ? {companyCards: feedUpdates} : {oAuthAccountDetails: feedUpdates}), - companyCardNicknames: { - [bankName]: null, - }, + const optimisticData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`, + value: { + settings: { + ...(isCustomFeed ? {companyCards: feedUpdates} : {oAuthAccountDetails: feedUpdates}), + companyCardNicknames: { + [bankName]: null, }, }, }, - ], - }; + }, + ]; + + if (feedToOpen) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.LAST_SELECTED_FEED}${policyID}`, + value: feedToOpen, + }); + } const parameters = { authToken, @@ -179,7 +185,7 @@ function deleteWorkspaceCompanyCardFeed(policyID: string, workspaceAccountID: nu bankName, }; - API.write(WRITE_COMMANDS.DELETE_COMPANY_CARD_FEED, parameters, onyxData); + API.write(WRITE_COMMANDS.DELETE_COMPANY_CARD_FEED, parameters, {optimisticData}); } function assignWorkspaceCompanyCard(policyID: string, data?: Partial) { diff --git a/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx b/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx index 41f698f61dab..2fb232360594 100644 --- a/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx +++ b/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx @@ -23,6 +23,7 @@ import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; +import type {CompanyCardFeed} from '@src/types/onyx'; type WorkspaceCompanyCardsSettingsPageProps = StackScreenProps; @@ -51,7 +52,8 @@ function WorkspaceCompanyCardsSettingsPage({ const deleteCompanyCardFeed = () => { if (selectedFeed) { - CompanyCards.deleteWorkspaceCompanyCardFeed(policyID, workspaceAccountID, selectedFeed); + const feedToOpen = (Object.keys(companyFeeds) as CompanyCardFeed[]).filter((feed) => feed !== selectedFeed).at(0); + CompanyCards.deleteWorkspaceCompanyCardFeed(policyID, workspaceAccountID, selectedFeed, feedToOpen); } setDeleteCompanyCardConfirmModalVisible(false); Navigation.setNavigationActionToMicrotaskQueue(Navigation.goBack); From 68a64fa52f5c727148df1ecb99c451bce5c74359 Mon Sep 17 00:00:00 2001 From: VickyStash Date: Thu, 21 Nov 2024 14:29:55 +0100 Subject: [PATCH 2/4] Remove getCompanyCardNumber function --- src/libs/CardUtils.ts | 9 --------- .../workspace/companyCards/WorkspaceCompanyCardsList.tsx | 3 +-- 2 files changed, 1 insertion(+), 11 deletions(-) diff --git a/src/libs/CardUtils.ts b/src/libs/CardUtils.ts index 77aeb8e0ecc3..b52ea975994b 100644 --- a/src/libs/CardUtils.ts +++ b/src/libs/CardUtils.ts @@ -212,14 +212,6 @@ function sortCardsByCardholderName(cardsList: OnyxEntry, per }); } -function getCompanyCardNumber(cardList: Record, lastFourPAN?: string, cardName = ''): string { - if (!lastFourPAN) { - return ''; - } - - return Object.keys(cardList).find((card) => card.endsWith(lastFourPAN)) ?? cardName; -} - function getCardFeedIcon(cardFeed: CompanyCardFeed | typeof CONST.EXPENSIFY_CARD.BANK): IconAsset { const feedIcons = { [CONST.COMPANY_CARD.FEED_BANK_NAME.VISA]: Illustrations.VisaCompanyCardDetailLarge, @@ -368,7 +360,6 @@ export { getTranslationKeyForLimitType, getEligibleBankAccountsForCard, sortCardsByCardholderName, - getCompanyCardNumber, getCardFeedIcon, getCardFeedName, getCompanyFeeds, diff --git a/src/pages/workspace/companyCards/WorkspaceCompanyCardsList.tsx b/src/pages/workspace/companyCards/WorkspaceCompanyCardsList.tsx index 75b4f44fc843..7959879609e7 100644 --- a/src/pages/workspace/companyCards/WorkspaceCompanyCardsList.tsx +++ b/src/pages/workspace/companyCards/WorkspaceCompanyCardsList.tsx @@ -36,7 +36,6 @@ function WorkspaceCompanyCardsList({cardsList, policyID}: WorkspaceCompanyCardsL const renderItem = useCallback( ({item, index}: ListRenderItemInfo) => { const cardID = Object.keys(cardsList ?? {}).find((id) => cardsList?.[id].cardID === item.cardID); - const cardName = CardUtils.getCompanyCardNumber(cardsList?.cardList ?? {}, item.lastFourPAN); const isCardDeleted = item.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE; return ( From fb7a68b236ddb24cb5a6701c85760a65457d3fac Mon Sep 17 00:00:00 2001 From: VickyStash Date: Fri, 22 Nov 2024 09:30:09 +0100 Subject: [PATCH 3/4] Update card display on the members page --- 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 b0066187e6f1..3ffe224dd50a 100644 --- a/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx +++ b/src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx @@ -326,7 +326,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM > Date: Fri, 22 Nov 2024 11:46:41 +0100 Subject: [PATCH 4/4] Fix minor bug --- .../companyCards/WorkspaceCompanyCardsSettingsPage.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx b/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx index 2fb232360594..8215a3d0bf40 100644 --- a/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx +++ b/src/pages/workspace/companyCards/WorkspaceCompanyCardsSettingsPage.tsx @@ -1,5 +1,5 @@ import type {StackScreenProps} from '@react-navigation/stack'; -import React, {useState} from 'react'; +import React, {useMemo, useState} from 'react'; import {View} from 'react-native'; import {useOnyx} from 'react-native-onyx'; import ConfirmModal from '@components/ConfirmModal'; @@ -40,7 +40,8 @@ function WorkspaceCompanyCardsSettingsPage({ const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`); const [lastSelectedFeed] = useOnyx(`${ONYXKEYS.COLLECTION.LAST_SELECTED_FEED}${policyID}`); - const selectedFeed = CardUtils.getSelectedFeed(lastSelectedFeed, cardFeeds); + // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps -- we want to run the hook only once to escape unexpected feed change + const selectedFeed = useMemo(() => CardUtils.getSelectedFeed(lastSelectedFeed, cardFeeds), []); const feedName = CardUtils.getCustomOrFormattedFeedName(selectedFeed, cardFeeds?.settings?.companyCardNicknames); const companyFeeds = CardUtils.getCompanyFeeds(cardFeeds); const liabilityType = selectedFeed && companyFeeds[selectedFeed]?.liabilityType;