Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: get all the cards from different feeds #52612

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -28,6 +28,15 @@ Onyx.connect({
},
});

let allWorkspaceCards: OnyxCollection<WorkspaceCardsList> = {};
Onyx.connect({
key: ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST,
waitForCollectionCallback: true,
callback: (value) => {
allWorkspaceCards = value;
},
});

/**
* @returns string with a month in MM format
*/
Expand Down Expand Up @@ -341,6 +350,17 @@ function getSelectedFeed(lastSelectedFeed: OnyxEntry<CompanyCardFeed>, 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,
Expand All @@ -367,4 +387,5 @@ export {
getCorrectStepForSelectedBank,
getCustomOrFormattedFeedName,
removeExpensifyCardFromCompanyCards,
getAllCardsForWorkspace,
};
21 changes: 5 additions & 16 deletions src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@
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}`);

Expand All @@ -85,27 +83,18 @@
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, workspaceCards]);

const confirmModalPrompt = useMemo(() => {
const isApprover = Member.isApprover(policy, accountID);
Expand Down Expand Up @@ -315,7 +304,7 @@
{translate('walletPage.assignedCards')}
</Text>
</View>
{(memberCards as MemberCard[]).map((memberCard) => {

Check failure on line 307 in src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

This assertion is unnecessary since it does not change the type of the expression

Check failure on line 307 in src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx

View workflow job for this annotation

GitHub Actions / ESLint check

This assertion is unnecessary since it does not change the type of the expression
const isCardDeleted = memberCard.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
return (
<OfflineWithFeedback
Expand Down
Loading