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

[Workspace Feeds] Fix app crash after enabling Expensify Card #49173

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 21 additions & 1 deletion src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import lodash from 'lodash';
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 * as Illustrations from '@src/components/Icon/Illustrations';
import CONST from '@src/CONST';
Expand All @@ -26,6 +26,14 @@ Onyx.connect({
},
});

let allCardsLists: OnyxCollection<WorkspaceCardsList>;

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change

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 @@ -194,6 +202,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);
mountiny marked this conversation as resolved.
Show resolved Hide resolved
}

export {
isExpensifyCard,
isCorporateCard,
Expand All @@ -210,4 +229,5 @@ export {
getEligibleBankAccountsForCard,
sortCardsByCardholderName,
getCardFeedIcon,
shouldExpensifyCardToggleBeDisabled,
};
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),
mountiny marked this conversation as resolved.
Show resolved Hide resolved
action: (isEnabled: boolean) => {
if (!policyID) {
return;
Expand Down
Loading