Skip to content

Commit

Permalink
fix PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
SzymczakJ committed Aug 8, 2024
1 parent cf4b28e commit 3c0b0cf
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 111 deletions.
16 changes: 0 additions & 16 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5325,22 +5325,6 @@ const CONST = {
REPORT_ID: 'reportID',
KEYWORD: 'keyword',
},
SYNTAX_FILTER_VALUE_TO_KEY_MAPPING: {
date: 'DATE',
amount: 'AMOUNT',
expenseType: 'EXPENSE_TYPE',
currency: 'CURRENCY',
merchant: 'MERCHANT',
description: 'DESCRIPTION',
from: 'FROM',
to: 'TO',
category: 'CATEGORY',
tag: 'TAG',
taxRate: 'TAX_RATE',
cardID: 'CARD_ID',
reportID: 'REPORT_ID',
keyword: 'KEYWORD',
},
},

REFERRER: {
Expand Down
1 change: 1 addition & 0 deletions src/components/Search/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ function SearchPageHeader({queryJSON, hash, onSelectDeleteOption, setOfflineModa
);
const {status} = queryJSON;
const headerSubtitle = isCustomQuery ? SearchUtils.getSearchHeaderTitle(queryJSON) : translate(headerContent[status]?.titleTx);
console.log('%%%%%\n', 'headerSubtitle', headerSubtitle);

Check failure on line 136 in src/components/Search/SearchPageHeader.tsx

View workflow job for this annotation

GitHub Actions / Run ESLint

Unexpected console statement
const headerTitle = isCustomQuery ? translate('search.filtersHeader') : '';
const headerIcon = isCustomQuery ? Illustrations.Filters : headerContent[status]?.icon;

Expand Down
5 changes: 3 additions & 2 deletions src/components/SearchMultipleSelectionPicker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import useLocalize from '@hooks/useLocalize';
import localeCompare from '@libs/LocaleCompare';
import Navigation from '@libs/Navigation/Navigation';
import type {OptionData} from '@libs/ReportUtils';
import ROUTES from '@src/ROUTES';
import Button from './Button';
import SelectionList from './SelectionList';
import SelectableListItem from './SelectionList/SelectableListItem';
Expand Down Expand Up @@ -37,7 +38,7 @@ function SearchMultipleSelectionPicker({items, initiallySelectedItems, pickerTit
isSelected: true,
}));
const remainingItemsSection = items
.filter((item) => selectedItems.some((selectedItem) => selectedItem.name === item.name) === false && item?.name.toLowerCase().includes(debouncedSearchTerm?.toLowerCase()))
.filter((item) => selectedItems.some((selectedItem) => selectedItem.value === item.value) === false && item?.name.toLowerCase().includes(debouncedSearchTerm?.toLowerCase()))
.sort((a, b) => localeCompare(a.name, b.name))
.map((item) => ({
text: item.name,
Expand Down Expand Up @@ -80,7 +81,7 @@ function SearchMultipleSelectionPicker({items, initiallySelectedItems, pickerTit

const handleConfirmSelection = useCallback(() => {
onSaveSelection(selectedItems.map((item) => item.value));
Navigation.goBack();
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS);
}, [onSaveSelection, selectedItems]);

const footerContent = useMemo(
Expand Down
2 changes: 1 addition & 1 deletion src/languages/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3659,7 +3659,7 @@ export default {
status: 'Status',
currency: 'Currency',
},
expenseTypes: 'Expense types',
expenseType: 'Expense type',
},
genericErrorPage: {
title: 'Uh-oh, something went wrong!',
Expand Down
2 changes: 1 addition & 1 deletion src/languages/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3713,7 +3713,7 @@ export default {
status: 'Estado',
currency: 'Divisa',
},
expenseTypes: 'Tipos de gastos',
expenseType: 'Tipo de gasto',
},
genericErrorPage: {
title: '¡Oh-oh, algo salió mal!',
Expand Down
26 changes: 16 additions & 10 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import INPUT_IDS from '@src/types/form/NetSuiteCustomFieldForm';
import type {OnyxInputOrEntry, Policy, PolicyCategories, PolicyEmployeeList, PolicyTag, PolicyTagLists, PolicyTags, TaxRate} from '@src/types/onyx';
import type {OnyxInputOrEntry, Policy, PolicyCategories, PolicyEmployeeList, PolicyTagLists, PolicyTags, TaxRate} from '@src/types/onyx';
import type {ErrorFields, PendingAction, PendingFields} from '@src/types/onyx/OnyxCommon';
import type {
ConnectionLastSync,
Expand Down Expand Up @@ -289,11 +289,15 @@ function getTagList(policyTagList: OnyxEntry<PolicyTagLists>, tagIndex: number):
);
}

function getTagsNamesFromTagsList(policyTagLists: PolicyTagLists): string[] {
const tagsList: PolicyTag[] = Object.values(policyTagLists ?? {})
.map((policyTagList) => Object.values(policyTagList.tags))
.flat();
return tagsList.map((tag) => tag.name);
function getTagNamesFromTagsLists(policyTagLists: PolicyTagLists): string[] {
const uniqueTagNames = new Set<string>();

for (const policyTagList of Object.values(policyTagLists ?? {})) {
for (const tag of Object.values(policyTagList.tags)) {
uniqueTagNames.add(tag.name);
}
}
return Array.from(uniqueTagNames);
}

/**
Expand Down Expand Up @@ -419,13 +423,15 @@ function getTaxByID(policy: OnyxEntry<Policy>, taxID: string): TaxRate | undefin
return policy?.taxRates?.taxes?.[taxID];
}

function getAllTaxRates(): TaxRates {
let allTaxRates: TaxRates = {};
function getAllTaxRates(): Record<string, string> {
const allTaxRates: Record<string, string> = {};
Object.values(allPolicies ?? {})?.forEach((policy) => {
if (!policy?.taxRates?.taxes) {
return;
}
allTaxRates = {...allTaxRates, ...policy?.taxRates?.taxes};
Object.entries(policy?.taxRates?.taxes).forEach(([taxRateKey, taxRate]) => {
allTaxRates[taxRateKey] = taxRate.name;
});
});
return allTaxRates;
}
Expand Down Expand Up @@ -1014,7 +1020,7 @@ export {
areSettingsInErrorFields,
settingsPendingAction,
getAllTaxRates,
getTagsNamesFromTagsList,
getTagNamesFromTagsLists,
};

export type {MemberEmailsToAccountIDs};
42 changes: 23 additions & 19 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type {ListItem, ReportListItemType, TransactionListItemType} from '@compo
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {SearchAdvancedFiltersForm} from '@src/types/form';
import INPUT_IDS from '@src/types/form/SearchAdvancedFiltersForm';
import FILTER_KEYS from '@src/types/form/SearchAdvancedFiltersForm';
import type * as OnyxTypes from '@src/types/onyx';
import type SearchResults from '@src/types/onyx/SearchResults';
import type {SearchAccountDetails, SearchDataTypes, SearchPersonalDetails, SearchTransaction, SearchTypeToItemMap, SectionsType} from '@src/types/onyx/SearchResults';
Expand All @@ -18,6 +18,8 @@ import * as searchParser from './SearchParser/searchParser';
import * as TransactionUtils from './TransactionUtils';
import * as UserUtils from './UserUtils';

type KeysOfFilterKeysObject = keyof typeof CONST.SEARCH.SYNTAX_FILTER_KEYS;

const columnNamesToSortingProperty = {
[CONST.SEARCH.TABLE_COLUMNS.TO]: 'formattedTo' as const,
[CONST.SEARCH.TABLE_COLUMNS.FROM]: 'formattedFrom' as const,
Expand Down Expand Up @@ -385,8 +387,8 @@ function normalizeQuery(query: string) {
* returns Date filter query string part, which needs special logic
*/
function buildDateFilterQuery(filterValues: Partial<SearchAdvancedFiltersForm>) {
const dateBefore = filterValues[INPUT_IDS.DATE_BEFORE];
const dateAfter = filterValues[INPUT_IDS.DATE_AFTER];
const dateBefore = filterValues[FILTER_KEYS.DATE_BEFORE];
const dateAfter = filterValues[FILTER_KEYS.DATE_AFTER];

let dateFilter = '';
if (dateBefore) {
Expand Down Expand Up @@ -415,34 +417,36 @@ function sanitizeString(str: string) {
function buildQueryStringFromFilters(filterValues: Partial<SearchAdvancedFiltersForm>) {
const filtersString = Object.entries(filterValues)
.map(([filterKey, filterValue]) => {
if (filterKey === INPUT_IDS.TYPE && filterValue) {
if (filterKey === FILTER_KEYS.TYPE && filterValue) {
return `${CONST.SEARCH.SYNTAX_ROOT_KEYS.TYPE}:${filterValue as string}`;
}

if (filterKey === INPUT_IDS.STATUS && filterValue) {
if (filterKey === FILTER_KEYS.STATUS && filterValue) {
return `${CONST.SEARCH.SYNTAX_ROOT_KEYS.STATUS}:${filterValue as string}`;
}

if (filterKey === INPUT_IDS.CURRENCY && Array.isArray(filterValue) && filterValue.length > 0) {
return `${CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY}:${filterValue.join(',')}`;
}
if ((filterKey === INPUT_IDS.MERCHANT || filterKey === INPUT_IDS.DESCRIPTION || filterKey === INPUT_IDS.REPORT_ID) && filterValue) {
const keyInCorrectForm = CONST.SEARCH.SYNTAX_FILTER_VALUE_TO_KEY_MAPPING[filterKey];
return `${CONST.SEARCH.SYNTAX_FILTER_KEYS[keyInCorrectForm]}:${filterValue as string}`;
if ((filterKey === FILTER_KEYS.MERCHANT || filterKey === FILTER_KEYS.DESCRIPTION || filterKey === FILTER_KEYS.REPORT_ID) && filterValue) {
const keyInCorrectForm = (Object.keys(CONST.SEARCH.SYNTAX_FILTER_KEYS) as KeysOfFilterKeysObject[]).find((key) => CONST.SEARCH.SYNTAX_FILTER_KEYS[key] === filterKey);
if (keyInCorrectForm) {
return `${CONST.SEARCH.SYNTAX_FILTER_KEYS[keyInCorrectForm]}:${filterValue as string}`;
}
}

if (
(filterKey === INPUT_IDS.CATEGORY ||
filterKey === INPUT_IDS.CARD_ID ||
filterKey === INPUT_IDS.TAX_RATE ||
filterKey === INPUT_IDS.EXPENSE_TYPE ||
filterKey === INPUT_IDS.TAG) &&
(filterKey === FILTER_KEYS.CATEGORY ||
filterKey === FILTER_KEYS.CARD_ID ||
filterKey === FILTER_KEYS.TAX_RATE ||
filterKey === FILTER_KEYS.EXPENSE_TYPE ||
filterKey === FILTER_KEYS.TAG ||
filterKey === FILTER_KEYS.CURRENCY) &&
Array.isArray(filterValue) &&
filterValue.length > 0
) {
const arrayFilterValues = filterValues[filterKey] ?? [];
const keyInCorrectForm = CONST.SEARCH.SYNTAX_FILTER_VALUE_TO_KEY_MAPPING[filterKey];
return `${CONST.SEARCH.SYNTAX_FILTER_KEYS[keyInCorrectForm]}:${arrayFilterValues.map(sanitizeString).join(',')}`;
const filterValueArray = filterValues[filterKey] ?? [];
const keyInCorrectForm = (Object.keys(CONST.SEARCH.SYNTAX_FILTER_KEYS) as KeysOfFilterKeysObject[]).find((key) => CONST.SEARCH.SYNTAX_FILTER_KEYS[key] === filterKey);
if (keyInCorrectForm) {
return `${CONST.SEARCH.SYNTAX_FILTER_KEYS[keyInCorrectForm]}:${filterValueArray.map(sanitizeString).join(', ')}`;
}
}

return undefined;
Expand Down
8 changes: 4 additions & 4 deletions src/pages/Search/AdvancedSearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type {SearchAdvancedFiltersForm} from '@src/types/form';
import type {CardList, TaxRates} from '@src/types/onyx';
import type {CardList} from '@src/types/onyx';

function getFilterDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>, fieldName: AdvancedFiltersKeys, translate: LocaleContextProps['translate']) {
if (fieldName === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE) {
Expand Down Expand Up @@ -69,9 +69,9 @@ function getFilterCardDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>,
: undefined;
}

function getFilterTaxRateDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>, taxRates: TaxRates) {
function getFilterTaxRateDisplayTitle(filters: Partial<SearchAdvancedFiltersForm>, taxRates: Record<string, string>) {
const filterValue = filters[CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE];
return filterValue ? filterValue.map((taxRateKey) => taxRates[taxRateKey].name).join(', ') : undefined;
return filterValue ? filterValue.map((taxRateKey) => taxRates[taxRateKey]).join(', ') : undefined;
}

function AdvancedSearchFilters() {
Expand Down Expand Up @@ -139,7 +139,7 @@ function AdvancedSearchFilters() {
},
{
title: getFilterDisplayTitle(searchAdvancedFilters, CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPENSE_TYPE, translate),
description: 'search.expenseTypes' as const,
description: 'search.expenseType' as const,
route: ROUTES.SEARCH_ADVANCED_FILTERS_EXPENSE_TYPE,
},
{
Expand Down
25 changes: 7 additions & 18 deletions src/pages/Search/SearchFiltersCategoryPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {useOnyx} from 'react-native-onyx';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import SearchMultipleSelectionPicker from '@components/SearchMultipleSelectionPicker';
import type {SearchMultipleSelectionPickerItem} from '@components/SearchMultipleSelectionPicker';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Navigation from '@libs/Navigation/Navigation';
Expand All @@ -17,28 +16,18 @@ function SearchFiltersCategoryPage() {
const {translate} = useLocalize();

const [searchAdvancedFiltersForm] = useOnyx(ONYXKEYS.FORMS.SEARCH_ADVANCED_FILTERS_FORM);
const selectedCategories = searchAdvancedFiltersForm?.category?.map((category) => ({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 categories: SearchMultipleSelectionPickerItem[] = [];
if (!singlePolicyCategories) {
const categoriesList = Object.values(allPolicyIDCategories ?? {})
.map((policyCategories) => Object.values(policyCategories ?? {}))
.flat();
categoriesList.forEach((category) => {
if (categories.some((item) => item.name === category.name)) {
return;
}
categories.push({name: category.name, value: category.name});
});
} else {
Object.values(singlePolicyCategories ?? {}).forEach((category) => categories.push({name: category.name, value: category.name}));
const uniqueCategoryNames = new Set<string>();
Object.values(allPolicyIDCategories ?? {}).map((policyCategories) => Object.values(policyCategories ?? {}).forEach((category) => uniqueCategoryNames.add(category.name)));
return Array.from(uniqueCategoryNames).map((categoryName) => ({name: categoryName, value: categoryName}));
}

return categories;
return Object.values(singlePolicyCategories ?? {}).map((category) => ({name: category.name, value: category.name}));
}, [allPolicyIDCategories, singlePolicyCategories]);

const onSaveSelection = useCallback((values: string[]) => SearchActions.updateAdvancedFilters({category: values}), []);
Expand All @@ -56,11 +45,11 @@ function SearchFiltersCategoryPage() {
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS);
}}
/>
<View style={[styles.flex1, styles.pb5]}>
<View style={[styles.flex1]}>
<SearchMultipleSelectionPicker
pickerTitle={translate('common.category')}
items={categoryItems}
initiallySelectedItems={selectedCategories}
initiallySelectedItems={selectedCategoriesItems}
onSaveSelection={onSaveSelection}
/>
</View>
Expand Down
3 changes: 1 addition & 2 deletions src/pages/Search/SearchFiltersCurrencyPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ function SearchFiltersCurrencyPage() {
}, [currencyList, selectedCurrenciesCodes]);
const handleOnSubmit = (values: string[]) => {
SearchActions.updateAdvancedFilters({currency: values});
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS);
};

return (
Expand All @@ -54,7 +53,7 @@ function SearchFiltersCurrencyPage() {
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS);
}}
/>
<View style={[styles.flex1, styles.pb5]}>
<View style={[styles.flex1]}>
<SearchMultipleSelectionPicker
pickerTitle={translate('search.filters.currency')}
items={currencyItems}
Expand Down
15 changes: 8 additions & 7 deletions src/pages/Search/SearchFiltersExpenseTypePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import ROUTES from '@src/ROUTES';
function getExpenseTypeTranslationKey(expenseType: ValueOf<typeof CONST.SEARCH.TRANSACTION_TYPE>): TranslationPaths {
// eslint-disable-next-line default-case
switch (expenseType) {
case 'distance':
case CONST.SEARCH.TRANSACTION_TYPE.DISTANCE:
return 'common.distance';
case 'card':
case CONST.SEARCH.TRANSACTION_TYPE.CARD:
return 'common.card';
case 'cash':
case CONST.SEARCH.TRANSACTION_TYPE.CASH:
return 'iou.cash';
}
}
Expand All @@ -44,26 +44,27 @@ function SearchFiltersExpenseTypePage() {
});
}, [allExpenseTypes, translate]);

const onSaveSelection = useCallback((values: string[]) => SearchActions.updateAdvancedFilters({expenseType: values}), []);
const updateExpenseTypeFilter = useCallback((values: string[]) => SearchActions.updateAdvancedFilters({expenseType: values}), []);

return (
<ScreenWrapper
testID={SearchFiltersExpenseTypePage.displayName}
shouldShowOfflineIndicatorInWideScreen
offlineIndicatorStyle={styles.mtAuto}
includeSafeAreaPaddingBottom={false}
>
<HeaderWithBackButton
title={translate('search.expenseTypes')}
title={translate('search.expenseType')}
onBackButtonPress={() => {
Navigation.goBack(ROUTES.SEARCH_ADVANCED_FILTERS);
}}
/>
<View style={[styles.flex1]}>
<SearchMultipleSelectionPicker
pickerTitle={translate('search.expenseTypes')}
pickerTitle={translate('search.expenseType')}
items={expenseTypesItems}
initiallySelectedItems={selectedExpenseTypes}
onSaveSelection={onSaveSelection}
onSaveSelection={updateExpenseTypeFilter}
shouldShowTextInput={false}
/>
</View>
Expand Down
Loading

0 comments on commit 3c0b0cf

Please sign in to comment.