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

Preserve policyID when navigating to canned search queries #49694

Merged
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
5 changes: 3 additions & 2 deletions src/components/Search/SearchPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,9 @@ function SearchPageHeader({queryJSON, hash, onSelectDeleteOption, setOfflineModa
}

const onPress = () => {
const values = SearchUtils.buildFilterFormValuesFromQuery(queryJSON);
SearchActions.updateAdvancedFilters(values);
const filterFormValues = SearchUtils.buildFilterFormValuesFromQuery(queryJSON);
SearchActions.updateAdvancedFilters(filterFormValues);

Navigation.navigate(ROUTES.SEARCH_ADVANCED_FILTERS);
};

Expand Down
88 changes: 45 additions & 43 deletions src/components/Search/SearchStatusBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,125 +15,126 @@ import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import type {SearchDataTypes} from '@src/types/onyx/SearchResults';
import type IconAsset from '@src/types/utils/IconAsset';
import type {ChatSearchStatus, ExpenseSearchStatus, InvoiceSearchStatus, SearchQueryString, SearchStatus, TripSearchStatus} from './types';
import type {ChatSearchStatus, ExpenseSearchStatus, InvoiceSearchStatus, SearchStatus, TripSearchStatus} from './types';

type SearchStatusBarProps = {
type: SearchDataTypes;
status: SearchStatus;
policyID: string | undefined;
resetOffset: () => void;
};

const expenseOptions: Array<{key: ExpenseSearchStatus; icon: IconAsset; text: TranslationPaths; query: SearchQueryString}> = [
const expenseOptions: Array<{status: ExpenseSearchStatus; type: SearchDataTypes; icon: IconAsset; text: TranslationPaths}> = [
{
key: CONST.SEARCH.STATUS.EXPENSE.ALL,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
status: CONST.SEARCH.STATUS.EXPENSE.ALL,
icon: Expensicons.All,
text: 'common.all',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.EXPENSE, CONST.SEARCH.STATUS.EXPENSE.ALL),
},
{
key: CONST.SEARCH.STATUS.EXPENSE.DRAFTS,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
status: CONST.SEARCH.STATUS.EXPENSE.DRAFTS,
icon: Expensicons.Pencil,
text: 'common.drafts',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.EXPENSE, CONST.SEARCH.STATUS.EXPENSE.DRAFTS),
},
{
key: CONST.SEARCH.STATUS.EXPENSE.OUTSTANDING,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
status: CONST.SEARCH.STATUS.EXPENSE.OUTSTANDING,
icon: Expensicons.Hourglass,
text: 'common.outstanding',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.EXPENSE, CONST.SEARCH.STATUS.EXPENSE.OUTSTANDING),
},
{
key: CONST.SEARCH.STATUS.EXPENSE.APPROVED,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
status: CONST.SEARCH.STATUS.EXPENSE.APPROVED,
icon: Expensicons.ThumbsUp,
text: 'iou.approved',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.EXPENSE, CONST.SEARCH.STATUS.EXPENSE.APPROVED),
},
{
key: CONST.SEARCH.STATUS.EXPENSE.PAID,
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
status: CONST.SEARCH.STATUS.EXPENSE.PAID,
icon: Expensicons.MoneyBag,
text: 'iou.settledExpensify',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.EXPENSE, CONST.SEARCH.STATUS.EXPENSE.PAID),
},
];

const invoiceOptions: Array<{key: InvoiceSearchStatus; icon: IconAsset; text: TranslationPaths; query: SearchQueryString}> = [
const invoiceOptions: Array<{type: SearchDataTypes; status: InvoiceSearchStatus; icon: IconAsset; text: TranslationPaths}> = [
{
key: CONST.SEARCH.STATUS.INVOICE.ALL,
type: CONST.SEARCH.DATA_TYPES.INVOICE,
status: CONST.SEARCH.STATUS.INVOICE.ALL,
icon: Expensicons.All,
text: 'common.all',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.INVOICE, CONST.SEARCH.STATUS.INVOICE.ALL),
},
{
key: CONST.SEARCH.STATUS.INVOICE.OUTSTANDING,
type: CONST.SEARCH.DATA_TYPES.INVOICE,
status: CONST.SEARCH.STATUS.INVOICE.OUTSTANDING,
icon: Expensicons.Hourglass,
text: 'common.outstanding',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.INVOICE, CONST.SEARCH.STATUS.INVOICE.OUTSTANDING),
},
{
key: CONST.SEARCH.STATUS.INVOICE.PAID,
type: CONST.SEARCH.DATA_TYPES.INVOICE,
status: CONST.SEARCH.STATUS.INVOICE.PAID,
icon: Expensicons.MoneyBag,
text: 'iou.settledExpensify',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.INVOICE, CONST.SEARCH.STATUS.INVOICE.PAID),
},
];

const tripOptions: Array<{key: TripSearchStatus; icon: IconAsset; text: TranslationPaths; query: SearchQueryString}> = [
const tripOptions: Array<{type: SearchDataTypes; status: TripSearchStatus; icon: IconAsset; text: TranslationPaths}> = [
{
key: CONST.SEARCH.STATUS.TRIP.ALL,
type: CONST.SEARCH.DATA_TYPES.TRIP,
status: CONST.SEARCH.STATUS.TRIP.ALL,
icon: Expensicons.All,
text: 'common.all',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.TRIP, CONST.SEARCH.STATUS.TRIP.ALL),
},
{
key: CONST.SEARCH.STATUS.TRIP.CURRENT,
type: CONST.SEARCH.DATA_TYPES.TRIP,
status: CONST.SEARCH.STATUS.TRIP.CURRENT,
icon: Expensicons.Calendar,
text: 'search.filters.current',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.TRIP, CONST.SEARCH.STATUS.TRIP.CURRENT),
},
{
key: CONST.SEARCH.STATUS.TRIP.PAST,
type: CONST.SEARCH.DATA_TYPES.TRIP,
status: CONST.SEARCH.STATUS.TRIP.PAST,
icon: Expensicons.History,
text: 'search.filters.past',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.TRIP, CONST.SEARCH.STATUS.TRIP.PAST),
},
];

const chatOptions: Array<{key: ChatSearchStatus; icon: IconAsset; text: TranslationPaths; query: SearchQueryString}> = [
const chatOptions: Array<{type: SearchDataTypes; status: ChatSearchStatus; icon: IconAsset; text: TranslationPaths}> = [
{
key: CONST.SEARCH.STATUS.CHAT.ALL,
type: CONST.SEARCH.DATA_TYPES.CHAT,
status: CONST.SEARCH.STATUS.CHAT.ALL,
icon: Expensicons.All,
text: 'common.all',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.CHAT.ALL),
},
{
key: CONST.SEARCH.STATUS.CHAT.UNREAD,
type: CONST.SEARCH.DATA_TYPES.CHAT,
status: CONST.SEARCH.STATUS.CHAT.UNREAD,
icon: Expensicons.ChatBubbleUnread,
text: 'common.unread',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.CHAT.UNREAD),
},
{
key: CONST.SEARCH.STATUS.CHAT.SENT,
type: CONST.SEARCH.DATA_TYPES.CHAT,
status: CONST.SEARCH.STATUS.CHAT.SENT,
icon: Expensicons.Send,
text: 'common.sent',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.CHAT.SENT),
},
{
key: CONST.SEARCH.STATUS.CHAT.ATTACHMENTS,
type: CONST.SEARCH.DATA_TYPES.CHAT,
status: CONST.SEARCH.STATUS.CHAT.ATTACHMENTS,
icon: Expensicons.Document,
text: 'common.attachments',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.CHAT.ATTACHMENTS),
},
{
key: CONST.SEARCH.STATUS.CHAT.LINKS,
type: CONST.SEARCH.DATA_TYPES.CHAT,
status: CONST.SEARCH.STATUS.CHAT.LINKS,
icon: Expensicons.Paperclip,
text: 'common.links',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.CHAT.LINKS),
},
{
key: CONST.SEARCH.STATUS.CHAT.PINNED,
type: CONST.SEARCH.DATA_TYPES.CHAT,
status: CONST.SEARCH.STATUS.CHAT.PINNED,
icon: Expensicons.Pin,
text: 'search.filters.pinned',
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.CHAT.PINNED),
},
];

Expand All @@ -151,7 +152,7 @@ function getOptions(type: SearchDataTypes) {
}
}

function SearchStatusBar({type, status, resetOffset}: SearchStatusBarProps) {
function SearchStatusBar({type, status, policyID, resetOffset}: SearchStatusBarProps) {
const {singleExecution} = useSingleExecution();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
Expand All @@ -171,15 +172,16 @@ function SearchStatusBar({type, status, resetOffset}: SearchStatusBarProps) {
{options.map((item, index) => {
const onPress = singleExecution(() => {
resetOffset();
Navigation.setParams({q: item.query});
const query = SearchUtils.buildCannedSearchQuery({type: item.type, status: item.status, policyID});
Navigation.setParams({q: query});
});
const isActive = status === item.key;
const isActive = status === item.status;
const isFirstItem = index === 0;
const isLastItem = index === options.length - 1;

return (
<Button
key={item.key}
key={item.status}
onLayout={(e) => {
if (!isActive || isScrolledRef.current || !('left' in e.nativeEvent.layout)) {
return;
Expand Down
5 changes: 4 additions & 1 deletion src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function Search({queryJSON}: SearchProps) {
const [selectedTransactionsToDelete, setSelectedTransactionsToDelete] = useState<string[]>([]);
const [deleteExpensesConfirmModalVisible, setDeleteExpensesConfirmModalVisible] = useState(false);
const [downloadErrorModalVisible, setDownloadErrorModalVisible] = useState(false);
const {type, status, sortBy, sortOrder, hash} = queryJSON;
const {type, status, policyID, sortBy, sortOrder, hash} = queryJSON;

const [currentSearchResults] = useOnyx(`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`);

Expand Down Expand Up @@ -211,6 +211,7 @@ function Search({queryJSON}: SearchProps) {
<SearchStatusBar
type={type}
status={status}
policyID={policyID}
resetOffset={resetOffset}
/>
) : (
Expand Down Expand Up @@ -243,6 +244,7 @@ function Search({queryJSON}: SearchProps) {
<SearchStatusBar
type={type}
status={status}
policyID={policyID}
resetOffset={resetOffset}
/>
<EmptySearchView type={type} />
Expand Down Expand Up @@ -350,6 +352,7 @@ function Search({queryJSON}: SearchProps) {
<SearchStatusBar
type={type}
status={status}
policyID={policyID}
resetOffset={resetOffset}
/>
<SelectionListWithModal<ReportListItemType | TransactionListItemType | ReportActionListItemType>
Expand Down
3 changes: 1 addition & 2 deletions src/hooks/useDeleteSavedSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ConfirmModal from '@components/ConfirmModal';
import Navigation from '@libs/Navigation/Navigation';
import * as SearchUtils from '@libs/SearchUtils';
import * as SearchActions from '@userActions/Search';
import CONST from '@src/CONST';
import ROUTES from '@src/ROUTES';
import useLocalize from './useLocalize';

Expand All @@ -23,7 +22,7 @@ export default function useDeleteSavedSearch() {
SearchActions.clearAdvancedFilters();
Navigation.navigate(
ROUTES.SEARCH_CENTRAL_PANE.getRoute({
query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.EXPENSE, CONST.SEARCH.STATUS.EXPENSE.ALL),
query: SearchUtils.buildCannedSearchQuery(),
}),
);
};
Expand Down
24 changes: 21 additions & 3 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,7 @@ function buildSearchQueryString(queryJSON?: SearchQueryJSON) {
*/
function buildQueryStringFromFilterFormValues(filterValues: Partial<SearchAdvancedFiltersForm>) {
// We separate type and status filters from other filters to maintain hashes consistency for saved searches
const {type, status, ...otherFilters} = filterValues;
const {type, status, policyID, ...otherFilters} = filterValues;
const filtersString: string[] = [];

if (type) {
Expand All @@ -589,6 +589,11 @@ function buildQueryStringFromFilterFormValues(filterValues: Partial<SearchAdvanc
filtersString.push(`${CONST.SEARCH.SYNTAX_ROOT_KEYS.STATUS}:${sanitizedStatus}`);
}

if (policyID) {
const sanitizedPolicyID = sanitizeString(policyID);
filtersString.push(`${CONST.SEARCH.SYNTAX_ROOT_KEYS.POLICY_ID}:${sanitizedPolicyID}`);
}

const mappedFilters = Object.entries(otherFilters)
.map(([filterKey, filterValue]) => {
if ((filterKey === FILTER_KEYS.MERCHANT || filterKey === FILTER_KEYS.DESCRIPTION || filterKey === FILTER_KEYS.REPORT_ID) && filterValue) {
Expand Down Expand Up @@ -685,6 +690,9 @@ function buildFilterFormValuesFromQuery(queryJSON: SearchQueryJSON) {

filtersForm[FILTER_KEYS.TYPE] = queryJSON.type;
filtersForm[FILTER_KEYS.STATUS] = queryJSON.status;
if (queryJSON.policyID) {
filtersForm[FILTER_KEYS.POLICY_ID] = queryJSON.policyID;
}

return filtersForm;
}
Expand Down Expand Up @@ -772,8 +780,18 @@ function getSearchHeaderTitle(
return title;
}

function buildCannedSearchQuery(type: SearchDataTypes = CONST.SEARCH.DATA_TYPES.EXPENSE, status: SearchStatus = CONST.SEARCH.STATUS.EXPENSE.ALL): SearchQueryString {
return normalizeQuery(`type:${type} status:${status}`);
function buildCannedSearchQuery({
type = CONST.SEARCH.DATA_TYPES.EXPENSE,
status = CONST.SEARCH.STATUS.EXPENSE.ALL,
policyID,
}: {
type?: SearchDataTypes;
status?: SearchStatus;
policyID?: string;
} = {}): SearchQueryString {
const queryString = policyID ? `type:${type} status:${status} policyID:${policyID}` : `type:${type} status:${status}`;

return normalizeQuery(queryString);
}

function getOverflowMenu(itemName: string, hash: number, inputQuery: string, showDeleteModal: (hash: number) => void, isMobileMenu?: boolean, closeMenu?: () => void) {
Expand Down
13 changes: 7 additions & 6 deletions src/pages/Search/AdvancedSearchFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type {LocaleContextProps} from '@components/LocaleContextProvider';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import {usePersonalDetails} from '@components/OnyxProvider';
import ScrollView from '@components/ScrollView';
import type {AdvancedFiltersKeys, SearchQueryJSON} from '@components/Search/types';
import type {AdvancedFiltersKeys} from '@components/Search/types';
import useLocalize from '@hooks/useLocalize';
import useSingleExecution from '@hooks/useSingleExecution';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -230,8 +230,8 @@ function AdvancedSearchFilters() {
const personalDetails = usePersonalDetails();
const currentType = searchAdvancedFilters?.type ?? CONST.SEARCH.DATA_TYPES.EXPENSE;

const queryString = useMemo(() => SearchUtils.buildQueryStringFromFilterFormValues(searchAdvancedFilters) || '', [searchAdvancedFilters]);
const queryJSON = useMemo(() => SearchUtils.buildSearchQueryJSON(queryString || SearchUtils.buildCannedSearchQuery()) ?? ({} as SearchQueryJSON), [queryString]);
const queryString = useMemo(() => SearchUtils.buildQueryStringFromFilterFormValues(searchAdvancedFilters), [searchAdvancedFilters]);
const queryJSON = useMemo(() => SearchUtils.buildSearchQueryJSON(queryString || SearchUtils.buildCannedSearchQuery()), [queryString]);

const applyFiltersAndNavigate = () => {
SearchActions.clearAllFilters();
Expand All @@ -245,7 +245,7 @@ function AdvancedSearchFilters() {

const onSaveSearch = () => {
const savedSearchKeys = Object.keys(savedSearches ?? {});
if (savedSearches && savedSearchKeys.includes(String(queryJSON.hash))) {
if (!queryJSON || (savedSearches && savedSearchKeys.includes(String(queryJSON.hash)))) {
// If the search is already saved, return early to prevent unnecessary API calls
Navigation.dismissModal();
return;
Expand Down Expand Up @@ -299,6 +299,8 @@ function AdvancedSearchFilters() {
};
});

const displaySearchButton = queryJSON && !SearchUtils.isCannedSearchQuery(queryJSON);

return (
<>
<ScrollView contentContainerStyle={[styles.flexGrow1, styles.justifyContentBetween]}>
Expand All @@ -320,8 +322,7 @@ function AdvancedSearchFilters() {
})}
</View>
</ScrollView>

{!SearchUtils.isCannedSearchQuery(queryJSON) && (
{displaySearchButton && (
<Button
text={translate('search.saveSearch')}
onPress={onSaveSearch}
Expand Down
Loading
Loading