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 1 commit
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
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
14 changes: 12 additions & 2 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,8 +772,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
24 changes: 18 additions & 6 deletions src/pages/Search/SearchTypeMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type SearchTypeMenuItem = {
title: string;
type: SearchDataTypes;
icon: IconAsset;
route?: Route;
getRoute: (policyID?: string) => Route;
};

function SearchTypeMenu({queryJSON}: SearchTypeMenuProps) {
Expand All @@ -72,25 +72,37 @@ function SearchTypeMenu({queryJSON}: SearchTypeMenuProps) {
title: translate('common.expenses'),
type: CONST.SEARCH.DATA_TYPES.EXPENSE,
icon: Expensicons.Receipt,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery()}),
getRoute: (policyID?: string) => {
const query = SearchUtils.buildCannedSearchQuery({policyID});
return ROUTES.SEARCH_CENTRAL_PANE.getRoute({query});
},
},
{
title: translate('common.chats'),
type: CONST.SEARCH.DATA_TYPES.CHAT,
icon: Expensicons.ChatBubbles,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.CHAT, CONST.SEARCH.STATUS.TRIP.ALL)}),
getRoute: (policyID?: string) => {
const query = SearchUtils.buildCannedSearchQuery({type: CONST.SEARCH.DATA_TYPES.CHAT, status: CONST.SEARCH.STATUS.CHAT.ALL, policyID});
return ROUTES.SEARCH_CENTRAL_PANE.getRoute({query});
},
},
{
title: translate('workspace.common.invoices'),
type: CONST.SEARCH.DATA_TYPES.INVOICE,
icon: Expensicons.InvoiceGeneric,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.INVOICE, CONST.SEARCH.STATUS.INVOICE.ALL)}),
getRoute: (policyID?: string) => {
const query = SearchUtils.buildCannedSearchQuery({type: CONST.SEARCH.DATA_TYPES.INVOICE, status: CONST.SEARCH.STATUS.INVOICE.ALL, policyID});
return ROUTES.SEARCH_CENTRAL_PANE.getRoute({query});
},
},
{
title: translate('travel.trips'),
type: CONST.SEARCH.DATA_TYPES.TRIP,
icon: Expensicons.Suitcase,
route: ROUTES.SEARCH_CENTRAL_PANE.getRoute({query: SearchUtils.buildCannedSearchQuery(CONST.SEARCH.DATA_TYPES.TRIP, CONST.SEARCH.STATUS.TRIP.ALL)}),
getRoute: (policyID?: string) => {
const query = SearchUtils.buildCannedSearchQuery({type: CONST.SEARCH.DATA_TYPES.TRIP, status: CONST.SEARCH.STATUS.TRIP.ALL, policyID});
return ROUTES.SEARCH_CENTRAL_PANE.getRoute({query});
},
},
];

Expand Down Expand Up @@ -221,7 +233,7 @@ function SearchTypeMenu({queryJSON}: SearchTypeMenuProps) {
{typeMenuItems.map((item, index) => {
const onPress = singleExecution(() => {
SearchActions.clearAllFilters();
Navigation.navigate(item.route);
Navigation.navigate(item.getRoute(queryJSON.policyID));
});

return (
Expand Down
6 changes: 3 additions & 3 deletions src/pages/Search/SearchTypeMenuNarrow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ function SearchTypeMenuNarrow({typeMenuItems, activeItemIndex, queryJSON, title,
const {singleExecution} = useSingleExecution();
const {windowHeight} = useWindowDimensions();
const {translate} = useLocalize();
const {hash} = queryJSON;
const {hash, policyID} = queryJSON;
const {showDeleteModal, DeleteConfirmModal} = useDeleteSavedSearch();

const [isPopoverVisible, setIsPopoverVisible] = useState(false);
Expand All @@ -72,7 +72,7 @@ function SearchTypeMenuNarrow({typeMenuItems, activeItemIndex, queryJSON, title,
text: item.title,
onSelected: singleExecution(() => {
SearchActions.clearAllFilters();
Navigation.navigate(item.route);
Navigation.navigate(item.getRoute(policyID));
}),
isSelected,
icon: item.icon,
Expand All @@ -99,7 +99,7 @@ function SearchTypeMenuNarrow({typeMenuItems, activeItemIndex, queryJSON, title,
}

return items;
}, [typeMenuItems, activeItemIndex, title, theme, singleExecution, closeMenu, currentSavedSearch]);
}, [typeMenuItems, title, activeItemIndex, singleExecution, theme, policyID, closeMenu, currentSavedSearch]);

const menuIcon = useMemo(() => (title ? Expensicons.Filters : popoverMenuItems[activeItemIndex]?.icon ?? Expensicons.Receipt), [activeItemIndex, popoverMenuItems, title]);
const menuTitle = useMemo(() => title ?? popoverMenuItems[activeItemIndex]?.text, [activeItemIndex, popoverMenuItems, title]);
Expand Down
Loading