Skip to content

Commit

Permalink
Add getCustomOrFormattedFeedName utility function
Browse files Browse the repository at this point in the history
  • Loading branch information
VickyStash committed Nov 13, 2024
1 parent 3694181 commit b75026f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
14 changes: 13 additions & 1 deletion src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {TranslationPaths} from '@src/languages/types';
import type {OnyxValues} from '@src/ONYXKEYS';
import ONYXKEYS from '@src/ONYXKEYS';
import type {BankAccountList, Card, CardFeeds, CardList, CompanyCardFeed, PersonalDetailsList, WorkspaceCardsList} from '@src/types/onyx';
import type {CompanyFeeds} from '@src/types/onyx/CardFeeds';
import type {CompanyCardNicknames, CompanyFeeds} from '@src/types/onyx/CardFeeds';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type IconAsset from '@src/types/utils/IconAsset';
import localeCompare from './LocaleCompare';
Expand Down Expand Up @@ -293,6 +293,17 @@ const getBankCardDetailsImage = (bank: ValueOf<typeof CONST.COMPANY_CARDS.BANKS>
return iconMap[bank];
};

function getCustomOrFormattedFeedName(feed?: CompanyCardFeed, companyCardNicknames?: CompanyCardNicknames): string | undefined {
if (!feed) {
return;
}

const customFeedName = companyCardNicknames?.[feed];
const formattedFeedName = Localize.translateLocal('workspace.companyCards.feedName', {feedName: getCardFeedName(feed)});

return customFeedName ?? formattedFeedName;
}

// We will simplify the logic below once we have #50450 #50451 implemented
const getCorrectStepForSelectedBank = (selectedBank: ValueOf<typeof CONST.COMPANY_CARDS.BANKS>) => {
const banksWithFeedType = [
Expand Down Expand Up @@ -355,5 +366,6 @@ export {
getBankCardDetailsImage,
getSelectedFeed,
getCorrectStepForSelectedBank,
getCustomOrFormattedFeedName,
removeExpensifyCardFromCompanyCards,
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ function WorkspaceCompanyCardsSettingsFeedNamePage({
const [lastSelectedFeed] = useOnyx(`${ONYXKEYS.COLLECTION.LAST_SELECTED_FEED}${policyID}`);
const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`);
const selectedFeed = CardUtils.getSelectedFeed(lastSelectedFeed, cardFeeds);
const feedName =
selectedFeed && (cardFeeds?.settings?.companyCardNicknames?.[selectedFeed] ?? translate('workspace.companyCards.feedName', {feedName: CardUtils.getCardFeedName(selectedFeed)}));
const feedName = CardUtils.getCustomOrFormattedFeedName(selectedFeed, cardFeeds?.settings?.companyCardNicknames);

const validate = useCallback(
(values: FormOnyxValues<typeof ONYXKEYS.FORMS.WORKSPACE_COMPANY_CARD_FEED_NAME>) => {
const errors: FormInputErrors<typeof ONYXKEYS.FORMS.WORKSPACE_COMPANY_CARD_FEED_NAME> = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ function WorkspaceCompanyCardsSettingsPage({
const [cardFeeds] = useOnyx(`${ONYXKEYS.COLLECTION.SHARED_NVP_PRIVATE_DOMAIN_MEMBER}${workspaceAccountID}`);
const [lastSelectedFeed] = useOnyx(`${ONYXKEYS.COLLECTION.LAST_SELECTED_FEED}${policyID}`);
const selectedFeed = CardUtils.getSelectedFeed(lastSelectedFeed, cardFeeds);
const feedName =
selectedFeed && (cardFeeds?.settings?.companyCardNicknames?.[selectedFeed] ?? translate('workspace.companyCards.feedName', {feedName: CardUtils.getCardFeedName(selectedFeed)}));
const feedName = CardUtils.getCustomOrFormattedFeedName(selectedFeed, cardFeeds?.settings?.companyCardNicknames);
const companyFeeds = CardUtils.getCompanyFeeds(cardFeeds);
const liabilityType = selectedFeed && companyFeeds[selectedFeed]?.liabilityType;
const isPersonal = liabilityType === CONST.COMPANY_CARDS.DELETE_TRANSACTIONS.ALLOW;
Expand Down
18 changes: 16 additions & 2 deletions src/types/onyx/CardFeeds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,15 @@ type CardFeedData = CustomCardFeedData | DirectCardFeedData;
/** Both custom and direct company feeds */
type CompanyFeeds = Partial<Record<CompanyCardFeed, CardFeedData>>;

/** Custom feed names */
type CompanyCardNicknames = Partial<Record<CompanyCardFeed, string>>;

/** Card feeds model */
type CardFeeds = {
/** Feed settings */
settings: {
/** User-friendly feed nicknames */
companyCardNicknames?: Partial<Record<CompanyCardFeed, string>>;
companyCardNicknames?: CompanyCardNicknames;

/** Company cards feeds */
companyCards?: Partial<Record<CompanyCardFeed, CustomCardFeedData>>;
Expand Down Expand Up @@ -121,4 +124,15 @@ type AddNewCompanyCardFeed = {
};

export default CardFeeds;
export type {AddNewCardFeedStep, AddNewCompanyCardFeed, AddNewCardFeedData, CardFeedData, CustomCardFeedData, CompanyCardFeed, DirectCardFeedData, CardFeedProvider, CompanyFeeds};
export type {
AddNewCardFeedStep,
AddNewCompanyCardFeed,
AddNewCardFeedData,
CardFeedData,
CustomCardFeedData,
CompanyCardFeed,
DirectCardFeedData,
CardFeedProvider,
CompanyFeeds,
CompanyCardNicknames,
};

0 comments on commit b75026f

Please sign in to comment.