Skip to content

Commit

Permalink
Merge pull request #51015 from Expensify/revert-50122-cmartins-addNoO…
Browse files Browse the repository at this point in the history
…perator

[CP Staging] Revert "Support empty value for categories and tags"
  • Loading branch information
mjasikowski authored Oct 17, 2024
2 parents ee384b0 + 5abb86d commit ce400f7
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 73 deletions.
1 change: 0 additions & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5683,7 +5683,6 @@ const CONST = {
KEYWORD: 'keyword',
IN: 'in',
},
EMPTY_VALUE: 'none',
},

REFERRER: {
Expand Down
16 changes: 2 additions & 14 deletions src/components/Search/SearchMultipleSelectionPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import useLocalize from '@hooks/useLocalize';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import type {OptionData} from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';

type SearchMultipleSelectionPickerItem = {
Expand All @@ -29,25 +28,14 @@ function SearchMultipleSelectionPicker({items, initiallySelectedItems, pickerTit
const [searchTerm, debouncedSearchTerm, setSearchTerm] = useDebouncedState('');
const [selectedItems, setSelectedItems] = useState<SearchMultipleSelectionPickerItem[]>(initiallySelectedItems ?? []);

const sortOptionsWithEmptyValue = (a: SearchMultipleSelectionPickerItem, b: SearchMultipleSelectionPickerItem) => {
// Always show `No category` and `No tag` as the first option
if (a.value === CONST.SEARCH.EMPTY_VALUE) {
return -1;
}
if (b.value === CONST.SEARCH.EMPTY_VALUE) {
return 1;
}
return localeCompare(a.name, b.name);
};

useEffect(() => {
setSelectedItems(initiallySelectedItems ?? []);
}, [initiallySelectedItems]);

const {sections, noResultsFound} = useMemo(() => {
const selectedItemsSection = selectedItems
.filter((item) => item?.name.toLowerCase().includes(debouncedSearchTerm?.toLowerCase()))
.sort((a, b) => sortOptionsWithEmptyValue(a, b))
.sort((a, b) => localeCompare(a.name, b.name))
.map((item) => ({
text: item.name,
keyForList: item.name,
Expand All @@ -56,7 +44,7 @@ function SearchMultipleSelectionPicker({items, initiallySelectedItems, pickerTit
}));
const remainingItemsSection = items
.filter((item) => selectedItems.some((selectedItem) => selectedItem.value === item.value) === false && item?.name.toLowerCase().includes(debouncedSearchTerm?.toLowerCase()))
.sort((a, b) => sortOptionsWithEmptyValue(a, b))
.sort((a, b) => localeCompare(a.name, b.name))
.map((item) => ({
text: item.name,
keyForList: item.name,
Expand Down
2 changes: 0 additions & 2 deletions src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4292,8 +4292,6 @@ const translations = {
current: 'Current',
past: 'Past',
},
noCategory: 'No category',
noTag: 'No tag',
expenseType: 'Expense type',
recentSearches: 'Recent searches',
recentChats: 'Recent chats',
Expand Down
2 changes: 0 additions & 2 deletions src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4338,8 +4338,6 @@ const translations = {
current: 'Actual',
past: 'Anterior',
},
noCategory: 'Sin categoría',
noTag: 'Sin etiqueta',
expenseType: 'Tipo de gasto',
recentSearches: 'Búsquedas recientes',
recentChats: 'Chats recientes',
Expand Down
32 changes: 4 additions & 28 deletions src/pages/Search/AdvancedSearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,6 @@ function getFilterParticipantDisplayTitle(accountIDs: string[], personalDetails:
.join(', ');
}

const sortOptionsWithEmptyValue = (a: string, b: string) => {
// Always show `No category` and `No tag` as the first option
if (a === CONST.SEARCH.EMPTY_VALUE) {
return -1;
}
if (b === CONST.SEARCH.EMPTY_VALUE) {
return 1;
}
return localeCompare(a, b);
};

function getFilterDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>, fieldName: AdvancedFiltersKeys, translate: LocaleContextProps['translate']) {
if (fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE) {
// the value of date filter is a combination of dateBefore + dateAfter values
Expand Down Expand Up @@ -186,27 +175,14 @@ function getFilterDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>, fiel
return;
}

if (fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY && filters[fieldName]) {
if (
(fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY || fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY || fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG) &&
filters[fieldName]
) {
const filterArray = filters[fieldName] ?? [];
return filterArray.sort(localeCompare).join(', ');
}

if (fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY && filters[fieldName]) {
const filterArray = filters[fieldName] ?? [];
return filterArray
.sort(sortOptionsWithEmptyValue)
.map((value) => (value === CONST.SEARCH.EMPTY_VALUE ? translate('search.noCategory') : value))
.join(', ');
}

if (fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG && filters[fieldName]) {
const filterArray = filters[fieldName] ?? [];
return filterArray
.sort(sortOptionsWithEmptyValue)
.map((value) => (value === CONST.SEARCH.EMPTY_VALUE ? translate('search.noTag') : value))
.join(', ');
}

if (fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.DESCRIPTION) {
return filters[fieldName];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useSafePaddingBottomStyle from '@hooks/useSafePaddingBottomStyle';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import * as SearchActions from '@userActions/Search';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';

Expand All @@ -18,27 +17,19 @@ function SearchFiltersCategoryPage() {
const {translate} = useLocalize();

const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM);
const selectedCategoriesItems = searchAdvancedFiltersForm?.category?.map((category) => {
if (category === CONST.SEARCH.EMPTY_VALUE) {
return {name: translate('search.noCategory'), value: category};
}
return {name: category, value: category};
});
const selectedCategoriesItems = searchAdvancedFiltersForm?.category?.map((category) => ({name: category, value: category}));
const policyID = searchAdvancedFiltersForm?.policyID ?? '-1';
const [allPolicyIDCategories] = useOnyx(ONYXKEYS.COLLECTION.POLICY_CATEGORIES);
const singlePolicyCategories = allPolicyIDCategories?.[`${ONYXKEYS.COLLECTION.POLICY_CATEGORIES}${policyID}`];

const categoryItems = useMemo(() => {
const items = [{name: translate('search.noCategory'), value: CONST.SEARCH.EMPTY_VALUE as string}];
if (!singlePolicyCategories) {
const uniqueCategoryNames = new Set<string>();
Object.values(allPolicyIDCategories ?? {}).map((policyCategories) => Object.values(policyCategories ?? {}).forEach((category) => uniqueCategoryNames.add(category.name)));
items.push(...Array.from(uniqueCategoryNames).map((categoryName) => ({name: categoryName, value: categoryName})));
} else {
items.push(...Object.values(singlePolicyCategories ?? {}).map((category) => ({name: category.name, value: category.name})));
return Array.from(uniqueCategoryNames).map((categoryName) => ({name: categoryName, value: categoryName}));
}
return items;
}, [allPolicyIDCategories, singlePolicyCategories, translate]);
return Object.values(singlePolicyCategories ?? {}).map((category) => ({name: category.name, value: category.name}));
}, [allPolicyIDCategories, singlePolicyCategories]);

const onSaveSelection = useCallback((values: string[]) => SearchActions.updateAdvancedFilters({category: values}), []);
const safePaddingBottomStyle = useSafePaddingBottomStyle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
import {getTagNamesFromTagsLists} from '@libs/PolicyUtils';
import * as SearchActions from '@userActions/Search';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {PolicyTagLists} from '@src/types/onyx';
Expand All @@ -19,18 +18,12 @@ function SearchFiltersTagPage() {
const {translate} = useLocalize();

const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM);
const selectedTagsItems = searchAdvancedFiltersForm?.tag?.map((tag) => {
if (tag === CONST.SEARCH.EMPTY_VALUE) {
return {name: translate('search.noTag'), value: tag};
}
return {name: tag, value: tag};
});
const selectedTagsItems = searchAdvancedFiltersForm?.tag?.map((tag) => ({name: tag, value: tag}));
const policyID = searchAdvancedFiltersForm?.policyID ?? '-1';
const [allPoliciesTagsLists] = useOnyx(ONYXKEYS.COLLECTION.POLICY_TAGS);
const singlePolicyTagsList: PolicyTagLists | undefined = allPoliciesTagsLists?.[`${ONYXKEYS.COLLECTION.POLICY_TAGS}${policyID}`];

const tagItems = useMemo(() => {
const items = [{name: translate('search.noTag'), value: CONST.SEARCH.EMPTY_VALUE as string}];
if (!singlePolicyTagsList) {
const uniqueTagNames = new Set<string>();
const tagListsUnpacked = Object.values(allPoliciesTagsLists ?? {}).filter((item) => !!item) as PolicyTagLists[];
Expand All @@ -40,12 +33,10 @@ function SearchFiltersTagPage() {
})
.flat()
.forEach((tag) => uniqueTagNames.add(tag));
items.push(...Array.from(uniqueTagNames).map((tagName) => ({name: tagName, value: tagName})));
} else {
items.push(...getTagNamesFromTagsLists(singlePolicyTagsList).map((name) => ({name, value: name})));
return Array.from(uniqueTagNames).map((tagName) => ({name: tagName, value: tagName}));
}
return items;
}, [allPoliciesTagsLists, singlePolicyTagsList, translate]);
return getTagNamesFromTagsLists(singlePolicyTagsList).map((name) => ({name, value: name}));
}, [allPoliciesTagsLists, singlePolicyTagsList]);

const updateTagFilter = useCallback((values: string[]) => SearchActions.updateAdvancedFilters({tag: values}), []);

Expand Down

0 comments on commit ce400f7

Please sign in to comment.