Skip to content

Commit

Permalink
Merge pull request #57003 from callstack-internal/fix/56339-fetch-ava…
Browse files Browse the repository at this point in the history
…ilable-cards-to-assign

Fix/56339 fetch available cards to assign
  • Loading branch information
mountiny authored Feb 24, 2025
2 parents 55801e6 + 770a778 commit 8a7ed5c
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/libs/API/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,7 @@ const READ_COMMANDS = {
OPEN_POLICY_REPORT_FIELDS_PAGE: 'OpenPolicyReportFieldsPage',
OPEN_POLICY_EXPENSIFY_CARDS_PAGE: 'OpenPolicyExpensifyCardsPage',
OPEN_POLICY_COMPANY_CARDS_FEED: 'OpenPolicyCompanyCardsFeed',
OPEN_ASSIGN_FEED_CARD_PAGE: 'OpenAssignFeedCardPage',
OPEN_POLICY_COMPANY_CARDS_PAGE: 'OpenPolicyCompanyCardsPage',
OPEN_POLICY_EDIT_CARD_LIMIT_TYPE_PAGE: 'OpenPolicyEditCardLimitTypePage',
OPEN_SEARCH_FILTERS_CARD_PAGE: 'OpenSearchFiltersCardPage',
Expand Down Expand Up @@ -1056,6 +1057,7 @@ type ReadCommandParameters = {
[READ_COMMANDS.OPEN_POLICY_EXPENSIFY_CARDS_PAGE]: Parameters.OpenPolicyExpensifyCardsPageParams;
[READ_COMMANDS.OPEN_POLICY_COMPANY_CARDS_PAGE]: Parameters.OpenPolicyExpensifyCardsPageParams;
[READ_COMMANDS.OPEN_POLICY_COMPANY_CARDS_FEED]: Parameters.OpenPolicyCompanyCardsFeedParams;
[READ_COMMANDS.OPEN_ASSIGN_FEED_CARD_PAGE]: Parameters.OpenPolicyCompanyCardsFeedParams;
[READ_COMMANDS.OPEN_POLICY_EDIT_CARD_LIMIT_TYPE_PAGE]: Parameters.OpenPolicyEditCardLimitTypePageParams;
[READ_COMMANDS.OPEN_SEARCH_FILTERS_CARD_PAGE]: null;
[READ_COMMANDS.OPEN_POLICY_PROFILE_PAGE]: Parameters.OpenPolicyProfilePageParams;
Expand Down
6 changes: 6 additions & 0 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,11 @@ function checkIfFeedConnectionIsBroken(feedCards: Record<string, Card> | undefin
return Object.values(feedCards).some((card) => card.bank !== feedToExclude && card.lastScrapeResult !== 200);
}

function hasCardListObject(workspaceAccountID: number, feedName: CompanyCardFeed): boolean {
const workspaceCards = allWorkspaceCards?.[`cards_${workspaceAccountID}_${feedName}`] ?? {};
return !!workspaceCards.cardList;
}

export {
isExpensifyCard,
isCorporateCard,
Expand Down Expand Up @@ -554,4 +559,5 @@ export {
getFeedType,
flatAllCardsList,
checkIfFeedConnectionIsBroken,
hasCardListObject,
};
40 changes: 40 additions & 0 deletions src/libs/actions/CompanyCards.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,45 @@ function openPolicyCompanyCardsFeed(policyID: string, feed: CompanyCardFeed) {
API.read(READ_COMMANDS.OPEN_POLICY_COMPANY_CARDS_FEED, parameters);
}

function openAssignFeedCardPage(policyID: string, feed: CompanyCardFeed, workspaceAccountID: number) {
const parameters: OpenPolicyCompanyCardsFeedParams = {
policyID,
feed,
};

const optimisticData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`,
value: {
isLoading: true,
},
},
];

const successData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`,
value: {
isLoading: false,
},
},
];

const failureData: OnyxUpdate[] = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`,
value: {
isLoading: false,
},
},
];

API.read(READ_COMMANDS.OPEN_ASSIGN_FEED_CARD_PAGE, parameters, {optimisticData, successData, failureData});
}

export {
setWorkspaceCompanyCardFeedName,
deleteWorkspaceCompanyCardFeed,
Expand All @@ -741,4 +780,5 @@ export {
clearAddNewCardFlow,
setAssignCardStepAndData,
clearAssignCardStepAndData,
openAssignFeedCardPage,
};
18 changes: 16 additions & 2 deletions src/pages/workspace/members/WorkspaceMemberNewCardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@ import RadioListItem from '@components/SelectionList/RadioListItem';
import type {ListItem} from '@components/SelectionList/types';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import {getCardFeedIcon, getCompanyFeeds, getCustomOrFormattedFeedName, getFilteredCardList, hasOnlyOneCardToAssign, isSelectedFeedExpired} from '@libs/CardUtils';
import {
getCardFeedIcon,
getCompanyFeeds,
getCustomOrFormattedFeedName,
getFilteredCardList,
hasCardListObject,
hasOnlyOneCardToAssign,
isCustomFeed,
isSelectedFeedExpired,
} from '@libs/CardUtils';
import type {PlatformStackScreenProps} from '@libs/Navigation/PlatformStackNavigation/types';
import type {SettingsNavigatorParamList} from '@libs/Navigation/types';
import {getPolicy, getWorkspaceAccountID} from '@libs/PolicyUtils';
Expand All @@ -20,7 +29,7 @@ import type {WithPolicyAndFullscreenLoadingProps} from '@pages/workspace/withPol
import withPolicyAndFullscreenLoading from '@pages/workspace/withPolicyAndFullscreenLoading';
import variables from '@styles/variables';
import {setIssueNewCardStepAndData} from '@userActions/Card';
import {setAssignCardStepAndData} from '@userActions/CompanyCards';
import {openAssignFeedCardPage, setAssignCardStepAndData} from '@userActions/CompanyCards';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
Expand Down Expand Up @@ -99,6 +108,10 @@ function WorkspaceMemberNewCardPage({route, personalDetails}: WorkspaceMemberNew

const handleSelectFeed = (feed: CardFeedListItem) => {
setSelectedFeed(feed.value);
const hasAllCardsData = hasCardListObject(workspaceAccountID, feed.value as CompanyCardFeed);
if (isCustomFeed(feed.value as CompanyCardFeed) && !hasAllCardsData) {
openAssignFeedCardPage(policyID, feed.value as CompanyCardFeed, workspaceAccountID);
}
setShouldShowError(false);
};

Expand Down Expand Up @@ -169,6 +182,7 @@ function WorkspaceMemberNewCardPage({route, personalDetails}: WorkspaceMemberNew
onSubmit={handleSubmit}
message={translate('common.error.pleaseSelectOne')}
buttonText={translate('common.next')}
isLoading={!!cardFeeds?.isLoading}
/>
</ScreenWrapper>
</AccessOrNotFoundWrapper>
Expand Down

0 comments on commit 8a7ed5c

Please sign in to comment.