Skip to content

Commit

Permalink
Merge pull request #49173 from VickyStash/bugfix/49147-expensify-card…
Browse files Browse the repository at this point in the history
…s-crash

[Workspace Feeds] Fix app crash after enabling Expensify Card
  • Loading branch information
mountiny authored Sep 13, 2024
2 parents b2e7287 + f5b68f9 commit e86bd66
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ Onyx.connect({
},
});

let allCardsLists: OnyxCollection<WorkspaceCardsList>;

Onyx.connect({
key: ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST,
waitForCollectionCallback: true,
callback: (value) => (allCardsLists = value),
});

/**
* @returns string with a month in MM format
*/
Expand Down Expand Up @@ -195,6 +203,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);
}

function getCardDetailsImage(cardFeed: string): IconAsset {
if (cardFeed.startsWith(CONST.COMPANY_CARD.FEED_BANK_NAME.MASTER_CARD)) {
return Illustrations.MasterCardCompanyCardDetail;
Expand Down Expand Up @@ -243,6 +262,7 @@ export {
getEligibleBankAccountsForCard,
sortCardsByCardholderName,
getCardFeedIcon,
shouldExpensifyCardToggleBeDisabled,
getCardDetailsImage,
getMemberCards,
};
4 changes: 2 additions & 2 deletions src/pages/workspace/WorkspaceMoreFeaturesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e86bd66

Please sign in to comment.