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

store applied filters in onyx #48312

Merged
Show file tree
Hide file tree
Changes from 2 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
9 changes: 8 additions & 1 deletion src/components/Search/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,14 @@ function SearchPageHeader({queryJSON, hash, onSelectDeleteOption, setOfflineModa
<Button
text={translate('search.filtersHeader')}
icon={Expensicons.Filters}
onPress={() => Navigation.navigate(ROUTES.SEARCH_ADVANCED_FILTERS)}
onPress={() => {
const filters = SearchUtils.getFilters(queryJSON);
const form = SearchUtils.getFilterFormObject(filters);
form.type = queryJSON.type;
form.status = queryJSON.status;
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
SearchActions.updateAdvancedFilters(form);
Navigation.navigate(ROUTES.SEARCH_ADVANCED_FILTERS);
}}
medium
/>
</HeaderWrapper>
Expand Down
48 changes: 46 additions & 2 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import * as TransactionUtils from './TransactionUtils';
import * as UserUtils from './UserUtils';

type KeysOfFilterKeysObject = keyof typeof CONST.SEARCH.SYNTAX_FILTER_KEYS;
type KeysOfRootKeysObject = keyof typeof CONST.SEARCH.SYNTAX_ROOT_KEYS;
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved

const columnNamesToSortingProperty = {
[CONST.SEARCH.TABLE_COLUMNS.TO]: 'formattedTo' as const,
Expand Down Expand Up @@ -441,7 +442,14 @@ function buildQueryStringFromFilters(filterValues: Partial<SearchAdvancedFilters
if (filterKey === FILTER_KEYS.KEYWORD && 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}`;
return `${filterValue as string}`;
}
}

if ((filterKey === FILTER_KEYS.TYPE || filterKey === FILTER_KEYS.STATUS) && filterValue) {
const keyInCorrectForm = (Object.keys(CONST.SEARCH.SYNTAX_ROOT_KEYS) as KeysOfRootKeysObject[]).find((key) => CONST.SEARCH.SYNTAX_ROOT_KEYS[key] === filterKey);
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
if (keyInCorrectForm) {
return `${CONST.SEARCH.SYNTAX_ROOT_KEYS[keyInCorrectForm]}:${filterValue as string}`;
}
}

Expand All @@ -457,7 +465,7 @@ function buildQueryStringFromFilters(filterValues: Partial<SearchAdvancedFilters
Array.isArray(filterValue) &&
filterValue.length > 0
) {
const filterValueArray = filterValues[filterKey] ?? [];
const filterValueArray = Array.from(new Set<string>(filterValues[filterKey] ?? []));
289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
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(',')}`;
Expand Down Expand Up @@ -517,6 +525,41 @@ function getFilters(queryJSON: SearchQueryJSON) {
return filters;
}

289Adam289 marked this conversation as resolved.
Show resolved Hide resolved
function getFilterFormObject(filters: QueryFilters) {
const filterKeys = Object.keys(filters);
const filterFormObject = {} as Partial<SearchAdvancedFiltersForm>;
for (const filterKey of filterKeys) {
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.REPORT_ID || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.MERCHANT || filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.DESCRIPTION) {
filterFormObject[filterKey] = filters[filterKey]?.[0]?.value.toLocaleString();
}
if (
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CATEGORY ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CARD_ID ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAX_RATE ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.EXPENSE_TYPE ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TAG ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.CURRENCY ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.FROM ||
filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.TO
) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add In and Has filters keys

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also consider defining a MULTIPLE_VALUES_FILTER_KEYS const and then check for Object.values(MULTIPLE_VALUES_FILTER_KEYS).includes(filterKey) instead. This will simplify the condition if we need it in multiple places

filterFormObject[filterKey] = filters[filterKey]?.map((filter) => filter.value.toLocaleString());
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.KEYWORD) {
filterFormObject[filterKey] = filters[filterKey]?.map((filter) => filter.value.toLocaleString()).join(' ');
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.DATE) {
filterFormObject[FILTER_KEYS.DATE_BEFORE] = filters[filterKey]?.find((filter) => filter.operator === 'lt')?.value.toLocaleString();
filterFormObject[FILTER_KEYS.DATE_AFTER] = filters[filterKey]?.find((filter) => filter.operator === 'gt')?.value.toLocaleString();
}
if (filterKey === CONST.SEARCH.SYNTAX_FILTER_KEYS.AMOUNT) {
filterFormObject[FILTER_KEYS.LESS_THAN] = filters[filterKey]?.find((filter) => filter.operator === 'lt')?.value.toLocaleString();
filterFormObject[FILTER_KEYS.GREATER_THAN] = filters[filterKey]?.find((filter) => filter.operator === 'gt')?.value.toLocaleString();
}
}

return filterFormObject;
}

/**
* Given a SearchQueryJSON this function will try to find the value of policyID filter saved in query
* and return just the first policyID value from the filter.
Expand Down Expand Up @@ -575,6 +618,7 @@ export {
buildSearchQueryString,
getCurrentSearchParams,
getFilters,
getFilterFormObject,
getPolicyIDFromSearchQuery,
getListItem,
getSearchHeaderTitle,
Expand Down
4 changes: 4 additions & 0 deletions src/types/form/SearchAdvancedFiltersForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type {ValueOf} from 'type-fest';
import type Form from './Form';

const FILTER_KEYS = {
TYPE: 'type',
STATUS: 'status',
DATE_AFTER: 'dateAfter',
DATE_BEFORE: 'dateBefore',
CURRENCY: 'currency',
Expand All @@ -26,6 +28,8 @@ type InputID = ValueOf<typeof FILTER_KEYS>;
type SearchAdvancedFiltersForm = Form<
InputID,
{
[FILTER_KEYS.TYPE]: string;
[FILTER_KEYS.STATUS]: string;
[FILTER_KEYS.DATE_AFTER]: string;
[FILTER_KEYS.DATE_BEFORE]: string;
[FILTER_KEYS.CURRENCY]: string[];
Expand Down
Loading