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

Add ability to bulk select cards from the same bank in the Card filter #53389

Draft
wants to merge 22 commits into
base: main
Choose a base branch
from
Draft
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
7 changes: 5 additions & 2 deletions src/components/Search/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as SearchActions from '@libs/actions/Search';
import * as CardUtils from '@libs/CardUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getAllTaxRates} from '@libs/PolicyUtils';
import * as PolicyUtils from '@libs/PolicyUtils';
Expand Down Expand Up @@ -47,7 +48,9 @@ function SearchPageHeader({queryJSON}: SearchPageHeaderProps) {
const personalDetails = usePersonalDetails();
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const taxRates = getAllTaxRates();
const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const [userCardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const [workspaceCardFeeds = {}] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}`);
const allCards = useMemo(() => CardUtils.mergeCardListWithWorkspaceFeeds(workspaceCardFeeds, userCardList), [userCardList, workspaceCardFeeds]);
const [currencyList = {}] = useOnyx(ONYXKEYS.CURRENCY_LIST);
const [policyCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES);
const [policyTagsLists] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS);
Expand Down Expand Up @@ -326,7 +329,7 @@ function SearchPageHeader({queryJSON}: SearchPageHeaderProps) {
}

const onFiltersButtonPress = () => {
const filterFormValues = SearchQueryUtils.buildFilterFormValuesFromQuery(queryJSON, policyCategories, policyTagsLists, currencyList, personalDetails, cardList, reports, taxRates);
const filterFormValues = SearchQueryUtils.buildFilterFormValuesFromQuery(queryJSON, policyCategories, policyTagsLists, currencyList, personalDetails, allCards, reports, taxRates);
SearchActions.updateAdvancedFilters(filterFormValues);

Navigation.navigate(ROUTES.SEARCH_ADVANCED_FILTERS);
Expand Down
10 changes: 7 additions & 3 deletions src/components/Search/SearchPageHeaderInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as SearchActions from '@libs/actions/Search';
import * as CardUtils from '@libs/CardUtils';
import Navigation from '@libs/Navigation/Navigation';
import {getAllTaxRates} from '@libs/PolicyUtils';
import type {OptionData} from '@libs/ReportUtils';
Expand Down Expand Up @@ -71,10 +72,13 @@ function SearchPageHeaderInput({queryJSON, children}: SearchPageHeaderInputProps
const personalDetails = usePersonalDetails();
const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT);
const taxRates = useMemo(() => getAllTaxRates(), []);
const [userCardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const [workspaceCardFeeds = {}] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}`);
const allCards = useMemo(() => CardUtils.mergeCardListWithWorkspaceFeeds(workspaceCardFeeds, userCardList), [userCardList, workspaceCardFeeds]);

const {type, inputQuery: originalInputQuery} = queryJSON;
const isCannedQuery = SearchQueryUtils.isCannedSearchQuery(queryJSON);
const queryText = SearchQueryUtils.buildUserReadableQueryString(queryJSON, personalDetails, reports, taxRates);
const queryText = SearchQueryUtils.buildUserReadableQueryString(queryJSON, personalDetails, reports, taxRates, allCards);
const headerText = isCannedQuery ? translate(getHeaderContent(type).titleText) : '';

// The actual input text that the user sees
Expand Down Expand Up @@ -107,9 +111,9 @@ function SearchPageHeaderInput({queryJSON, children}: SearchPageHeaderInputProps
}, [queryText]);

useEffect(() => {
const substitutionsMap = buildSubstitutionsMap(originalInputQuery, personalDetails, reports, taxRates);
const substitutionsMap = buildSubstitutionsMap(originalInputQuery, personalDetails, reports, taxRates, allCards);
setAutocompleteSubstitutions(substitutionsMap);
}, [originalInputQuery, personalDetails, reports, taxRates]);
}, [allCards, originalInputQuery, personalDetails, reports, taxRates]);

const onSearchQueryChange = useCallback(
(userQuery: string) => {
Expand Down
12 changes: 8 additions & 4 deletions src/components/Search/SearchRouter/SearchRouterList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ function SearchRouterList(
const statusAutocompleteList = Object.values({...CONST.SEARCH.STATUS.TRIP, ...CONST.SEARCH.STATUS.INVOICE, ...CONST.SEARCH.STATUS.CHAT, ...CONST.SEARCH.STATUS.TRIP});
const expenseTypes = Object.values(CONST.SEARCH.TRANSACTION_TYPE);

const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const cardAutocompleteList = Object.values(cardList);
const [userCardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const [workspaceCardFeeds = {}] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}`);
const allCards = useMemo(() => CardUtils.mergeCardListWithWorkspaceFeeds(workspaceCardFeeds, userCardList), [userCardList, workspaceCardFeeds]);
const cardAutocompleteList = Object.values(allCards);

const participantsAutocompleteList = useMemo(() => {
if (!areOptionsInitialized) {
return [];
Expand Down Expand Up @@ -332,7 +335,7 @@ function SearchRouterList(

return filteredCards.map((card) => ({
filterKey: CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID,
text: CardUtils.getCardDescription(card.cardID),
text: CardUtils.getCardDescription(card.cardID, allCards),
autocompleteID: card.cardID.toString(),
}));
}
Expand All @@ -355,6 +358,7 @@ function SearchRouterList(
statusAutocompleteList,
expenseTypes,
cardAutocompleteList,
allCards,
]);

const sortedRecentSearches = useMemo(() => {
Expand All @@ -364,7 +368,7 @@ function SearchRouterList(
const recentSearchesData = sortedRecentSearches?.slice(0, 5).map(({query, timestamp}) => {
const searchQueryJSON = SearchQueryUtils.buildSearchQueryJSON(query);
return {
text: searchQueryJSON ? SearchQueryUtils.buildUserReadableQueryString(searchQueryJSON, personalDetails, reports, taxRates) : query,
text: searchQueryJSON ? SearchQueryUtils.buildUserReadableQueryString(searchQueryJSON, personalDetails, reports, taxRates, allCards) : query,
singleIcon: Expensicons.History,
searchQuery: query,
keyForList: timestamp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ function buildSubstitutionsMap(
personalDetails: OnyxTypes.PersonalDetailsList | undefined,
reports: OnyxCollection<OnyxTypes.Report>,
allTaxRates: Record<string, string[]>,
cardList: OnyxTypes.CardList,
): SubstitutionMap {
const parsedQuery = parser.parse(query) as {ranges: SearchAutocompleteQueryRange[]};

Expand Down Expand Up @@ -61,7 +62,7 @@ function buildSubstitutionsMap(
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID
) {
const displayValue = SearchQueryUtils.getFilterDisplayValue(filterKey, filterValue, personalDetails, reports);
const displayValue = SearchQueryUtils.getFilterDisplayValue(filterKey, filterValue, personalDetails, reports, cardList);

// If displayValue === filterValue, then it means there is nothing to substitute, so we don't add any key to map
if (displayValue !== filterValue) {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/usePaymentMethodState/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import type {ViewStyle} from 'react-native';
import type {StyleProp, ViewStyle} from 'react-native';
import type {AccountData} from '@src/types/onyx';
import type IconAsset from '@src/types/utils/IconAsset';

type FormattedSelectedPaymentMethodIcon = {
icon: IconAsset;
iconHeight?: number;
iconWidth?: number;
iconStyles?: ViewStyle[];
iconStyles?: StyleProp<ViewStyle>;
iconSize?: number;
};

Expand Down
7 changes: 7 additions & 0 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4585,6 +4585,13 @@ const translations = {
greaterThan: ({amount}: OptionalParam<RequestAmountParams> = {}) => `Greater than ${amount ?? ''}`,
between: ({greaterThan, lessThan}: FiltersAmountBetweenParams) => `Between ${greaterThan} and ${lessThan}`,
},
card: {
expensify: 'Expensify',
individualCards: 'Individual cards',
cardFeeds: 'Card feeds',
cardFeedName: ({cardFeedBankName, cardFeedLabel}: {cardFeedBankName: string; cardFeedLabel?: string}) =>
`All ${cardFeedBankName}${cardFeedLabel ? ` - ${cardFeedLabel}` : ''}`,
},
current: 'Current',
past: 'Past',
submitted: 'Submitted',
Expand Down
7 changes: 7 additions & 0 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4629,6 +4629,13 @@ const translations = {
link: 'Enlace',
pinned: 'Fijado',
unread: 'No leído',
card: {
expensify: 'Expensify',
individualCards: 'Tarjetas individuales',
cardFeeds: 'Flujos de tarjetas',
cardFeedName: ({cardFeedBankName, cardFeedLabel}: {cardFeedBankName: string; cardFeedLabel?: string}) =>
`Todo ${cardFeedBankName}${cardFeedLabel ? ` - ${cardFeedLabel}` : ''}`,
},
amount: {
lessThan: ({amount}: OptionalParam<RequestAmountParams> = {}) => `Menos de ${amount ?? ''}`,
greaterThan: ({amount}: OptionalParam<RequestAmountParams> = {}) => `Más que ${amount ?? ''}`,
Expand Down
26 changes: 23 additions & 3 deletions src/libs/CardUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,34 @@ function isCorporateCard(cardID: number) {
* @param cardID
* @returns string in format %<bank> - <lastFourPAN || Not Activated>%.
*/
function getCardDescription(cardID?: number) {
function getCardDescription(cardID?: number, cards: CardList = allCards) {
if (!cardID) {
return '';
}
const card = allCards[cardID];
const card = cards[cardID];
if (!card) {
return '';
}
const cardDescriptor = card.state === CONST.EXPENSIFY_CARD.STATE.NOT_ACTIVATED ? Localize.translateLocal('cardTransactions.notActivated') : card.lastFourPAN;
return cardDescriptor ? `${card.bank} - ${cardDescriptor}` : `${card.bank}`;
const humanReadableBankName = card.bank === CONST.EXPENSIFY_CARD.BANK ? CONST.EXPENSIFY_CARD.BANK : getCardFeedName(card.bank as CompanyCardFeed);
return cardDescriptor ? `${humanReadableBankName} - ${cardDescriptor}` : `${humanReadableBankName}`;
}

function isCard(item: Card | Record<string, string>): item is Card {
return 'cardID' in item && !!item.cardID && 'bank' in item && !!item.bank;
}

function mergeCardListWithWorkspaceFeeds(workspaceFeeds: Record<string, WorkspaceCardsList | undefined>, cardList = allCards) {
const feedCards: CardList = {...cardList};
Object.values(workspaceFeeds ?? {}).forEach((currentCardFeed) => {
Object.values(currentCardFeed ?? {}).forEach((card) => {
if (!isCard(card)) {
return;
}
feedCards[card.cardID] = card;
});
});
return feedCards;
}

/**
Expand Down Expand Up @@ -415,4 +433,6 @@ export {
hasOnlyOneCardToAssign,
checkIfNewFeedConnected,
getDefaultCardName,
mergeCardListWithWorkspaceFeeds,
isCard,
};
13 changes: 10 additions & 3 deletions src/libs/SearchQueryUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,13 @@ function getPolicyIDFromSearchQuery(queryJSON: SearchQueryJSON) {
/**
* Returns the human-readable "pretty" string for a specified filter value.
*/
function getFilterDisplayValue(filterName: string, filterValue: string, personalDetails: OnyxTypes.PersonalDetailsList | undefined, reports: OnyxCollection<OnyxTypes.Report>) {
function getFilterDisplayValue(
filterName: string,
filterValue: string,
personalDetails: OnyxTypes.PersonalDetailsList | undefined,
reports: OnyxCollection<OnyxTypes.Report>,
cardList: OnyxTypes.CardList,
) {
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM || filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.TO) {
// login can be an empty string
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
Expand All @@ -515,7 +521,7 @@ function getFilterDisplayValue(filterName: string, filterValue: string, personal
if (Number.isNaN(cardID)) {
return filterValue;
}
return CardUtils.getCardDescription(cardID) || filterValue;
return CardUtils.getCardDescription(cardID, cardList) || filterValue;
}
if (filterName === CONST.SEARCH.SYNTAX_FILTER_KEYS.IN) {
return ReportUtils.getReportName(reports?.[`${ONYXKEYS.COLLECTION.REPORT}${filterValue}`]) || filterValue;
Expand All @@ -538,6 +544,7 @@ function buildUserReadableQueryString(
PersonalDetails: OnyxTypes.PersonalDetailsList | undefined,
reports: OnyxCollection<OnyxTypes.Report>,
taxRates: Record<string, string[]>,
cardList: OnyxTypes.CardList,
) {
const {type, status} = queryJSON;
const filters = queryJSON.flatFilters;
Expand Down Expand Up @@ -569,7 +576,7 @@ function buildUserReadableQueryString(
} else {
displayQueryFilters = queryFilter.map((filter) => ({
operator: filter.operator,
value: getFilterDisplayValue(key, filter.value.toString(), PersonalDetails, reports),
value: getFilterDisplayValue(key, filter.value.toString(), PersonalDetails, reports, cardList),
}));
}
title += buildFilterValuesString(key, displayQueryFilters);
Expand Down
11 changes: 7 additions & 4 deletions src/pages/Search/AdvancedSearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import useLocalize from '@hooks/useLocalize';
import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
import useWaitForNavigation from '@hooks/useWaitForNavigation';
import * as CardUtils from '@libs/CardUtils';
import {convertToDisplayStringWithoutCurrency} from '@libs/CurrencyUtils';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -216,7 +217,7 @@ function getFilterCardDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>,
return filterValue
? Object.values(cards)
.filter((card) => filterValue.includes(card.cardID.toString()))
.map((card) => card.bank)
.map((card) => CardUtils.getCardDescription(card.cardID, cards))
.join(', ')
: undefined;
}
Expand Down Expand Up @@ -373,7 +374,9 @@ function AdvancedSearchFilters() {
const [savedSearches] = useOnyx(ONYXKEYS.SAVED_SEARCHES);
const [searchAdvancedFilters = {} as SearchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM);
const policyID = searchAdvancedFilters.policyID ?? '-1';
const [cardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const [userCardList = {}] = useOnyx(ONYXKEYS.CARD_LIST);
const [workspaceCardFeeds = {}] = useOnyx(`${ONYXKEYS.COLLECTION.WORKSPACE_CARDS_LIST}`);
const allCards = useMemo(() => CardUtils.mergeCardListWithWorkspaceFeeds(workspaceCardFeeds, userCardList), [userCardList, workspaceCardFeeds]);
const taxRates = getAllTaxRates();
const personalDetails = usePersonalDetails();

Expand Down Expand Up @@ -410,7 +413,7 @@ function AdvancedSearchFilters() {

const shouldDisplayCategoryFilter = shouldDisplayFilter(nonPersonalPolicyCategoryCount, areCategoriesEnabled, !!singlePolicyCategories);
const shouldDisplayTagFilter = shouldDisplayFilter(tagListsUnpacked.length, areTagsEnabled, !!singlePolicyTagLists);
const shouldDisplayCardFilter = shouldDisplayFilter(Object.keys(cardList).length, areCardsEnabled);
const shouldDisplayCardFilter = shouldDisplayFilter(Object.keys(allCards).length, areCardsEnabled);
const shouldDisplayTaxFilter = shouldDisplayFilter(Object.keys(taxRates).length, areTaxEnabled);

let currentType = searchAdvancedFilters?.type ?? CONST.SEARCH.DATA_TYPES.EXPENSE;
Expand Down Expand Up @@ -485,7 +488,7 @@ function AdvancedSearchFilters() {
if (!shouldDisplayCardFilter) {
return;
}
filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, cardList);
filterTitle = baseFilterConfig[key].getTitle(searchAdvancedFilters, allCards);
} else if (key === CONST.SEARCH.SYNTAX_FILTER_KEYS.POSTED) {
if (!shouldDisplayCardFilter) {
return;
Expand Down
Loading
Loading