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

Merged
Merged
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,7 +1,7 @@
import {fromUnixTime, isBefore} from 'date-fns';
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 @@ -30,6 +30,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 @@ -398,6 +407,17 @@ function checkIfNewFeedConnected(prevFeedsData: CompanyFeeds, currentFeedsData:
};
}

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;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please add a comment that in case of direct feeds, the list of unassigned cards yet is returned in the cardList property in the collection?

Object.assign(cards, rest);
}
}
return cards;
}

export {
isExpensifyCard,
isCorporateCard,
Expand Down Expand Up @@ -427,4 +447,5 @@ export {
hasOnlyOneCardToAssign,
checkIfNewFeedConnected,
getDefaultCardName,
getAllCardsForWorkspace,
};
28 changes: 8 additions & 20 deletions src/pages/workspace/members/WorkspaceMemberDetailsPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
const {formatPhoneNumber, 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}`);

const [isRemoveMemberConfirmModalVisible, setIsRemoveMemberConfirmModalVisible] = useState(false);
const [isRoleSelectionModalVisible, setIsRoleSelectionModalVisible] = useState(false);
Expand All @@ -84,29 +81,20 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
const ownerDetails = personalDetails?.[policy?.ownerAccountID ?? CONST.DEFAULT_NUMBER_ID] ?? ({} as PersonalDetails);
const policyOwnerDisplayName = formatPhoneNumber(PersonalDetailsUtils.getDisplayNameOrDefault(ownerDetails)) ?? policy?.owner ?? '';
const hasMultipleFeeds = Object.values(CardUtils.getCompanyFeeds(cardFeeds)).filter((feed) => !feed.pending).length > 0;
const paymentAccountID = cardSettings?.paymentBankAccountID ?? CONST.DEFAULT_NUMBER_ID;

const workspaceCards = CardUtils.getAllCardsForWorkspace(workspaceAccountID);
const hasWorkspaceCardsAssigned = !!workspaceCards && !!Object.values(workspaceCards).length;

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 @@ -220,7 +208,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
return <NotFoundPage />;
}

const shouldShowCardsSection = (!!policy?.areExpensifyCardsEnabled && !!paymentAccountID) || (!!policy?.areCompanyCardsEnabled && hasMultipleFeeds);
const shouldShowCardsSection = hasWorkspaceCardsAssigned && (!!policy?.areExpensifyCardsEnabled || !!policy?.areCompanyCardsEnabled);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use the old condition here. this new change caused #56372


return (
<AccessOrNotFoundWrapper
Expand Down Expand Up @@ -316,7 +304,7 @@ function WorkspaceMemberDetailsPage({personalDetails, policy, route}: WorkspaceM
{translate('walletPage.assignedCards')}
</Text>
</View>
{(memberCards as MemberCard[]).map((memberCard) => {
{memberCards.map((memberCard) => {
const isCardDeleted = memberCard.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE;
return (
<OfflineWithFeedback
Expand Down
Loading