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

[Search] Fix sorting not working on Search #46804

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
10 changes: 7 additions & 3 deletions src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import ROUTES from '@src/ROUTES';
import type SearchResults from '@src/types/onyx/SearchResults';
import {useSearchContext} from './SearchContext';
import SearchPageHeader from './SearchPageHeader';
import type {SearchColumnType, SearchQueryJSON, SelectedTransactionInfo, SelectedTransactions, SortOrder} from './types';
import type {SearchColumnType, SearchQueryJSON, SearchStatus, SelectedTransactionInfo, SelectedTransactions, SortOrder} from './types';

type SearchProps = {
queryJSON: SearchQueryJSON;
Expand All @@ -42,6 +42,7 @@ const transactionItemMobileHeight = 100;
const reportItemTransactionHeight = 52;
const listItemPadding = 12; // this is equivalent to 'mb3' on every transaction/report list item
const searchHeaderHeight = 54;
const sortableSearchStatuses: SearchStatus[] = [CONST.SEARCH.STATUS.ALL];

function mapTransactionItemToSelectedEntry(item: TransactionListItemType): [string, SelectedTransactionInfo] {
return [item.keyForList, {isSelected: true, canDelete: item.canDelete, canHold: item.canHold, canUnhold: item.canUnhold, action: item.action}];
Expand Down Expand Up @@ -86,7 +87,7 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
const [selectedTransactionsToDelete, setSelectedTransactionsToDelete] = useState<string[]>([]);
const [deleteExpensesConfirmModalVisible, setDeleteExpensesConfirmModalVisible] = useState(false);
const [downloadErrorModalVisible, setDownloadErrorModalVisible] = useState(false);
const {sortBy, sortOrder, hash} = queryJSON;
const {status, sortBy, sortOrder, hash} = queryJSON;

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

Expand Down Expand Up @@ -265,7 +266,8 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
const ListItem = SearchUtils.getListItem(type);

const data = SearchUtils.getSections(searchResults?.data ?? {}, searchResults?.search ?? {}, type);
const sortedSelectedData = data.map((item) => mapToItemWithSelectionInfo(item, selectedTransactions));
const sortedData = SearchUtils.getSortedSections(type, data, sortBy, sortOrder);
const sortedSelectedData = sortedData.map((item) => mapToItemWithSelectionInfo(item, selectedTransactions));

const toggleAllTransactions = () => {
const areItemsOfReportType = searchResults?.search.type === CONST.SEARCH.DATA_TYPES.REPORT;
Expand All @@ -292,6 +294,7 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
};

const shouldShowYear = SearchUtils.shouldShowYear(searchResults?.data);
const shouldShowSorting = sortableSearchStatuses.includes(status);

const canSelectMultiple = isSmallScreenWidth ? selectionMode?.isEnabled : true;

Expand Down Expand Up @@ -322,6 +325,7 @@ function Search({queryJSON, policyIDs, isCustomQuery}: SearchProps) {
sortOrder={sortOrder}
sortBy={sortBy}
shouldShowYear={shouldShowYear}
shouldShowSorting={shouldShowSorting}
/>
)
}
Expand Down
6 changes: 4 additions & 2 deletions src/components/SelectionList/SearchTableHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,10 @@ type SearchTableHeaderProps = {
sortOrder?: SortOrder;
onSortPress: (column: SearchColumnType, order: SortOrder) => void;
shouldShowYear: boolean;
shouldShowSorting: boolean;
};

function SearchTableHeader({data, metadata, sortBy, sortOrder, onSortPress, shouldShowYear}: SearchTableHeaderProps) {
function SearchTableHeader({data, metadata, sortBy, sortOrder, onSortPress, shouldShowYear, shouldShowSorting}: SearchTableHeaderProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {isSmallScreenWidth, isMediumScreenWidth} = useWindowDimensions();
Expand All @@ -113,6 +114,7 @@ function SearchTableHeader({data, metadata, sortBy, sortOrder, onSortPress, shou
return null;
}

const isSortable = shouldShowSorting && isColumnSortable;
const isActive = sortBy === columnName;
const textStyle = columnName === CONST.SEARCH.TABLE_COLUMNS.RECEIPT ? StyleUtils.getTextOverflowStyle('clip') : null;

Expand All @@ -124,7 +126,7 @@ function SearchTableHeader({data, metadata, sortBy, sortOrder, onSortPress, shou
sortOrder={sortOrder ?? CONST.SEARCH.SORT_ORDER.ASC}
isActive={isActive}
containerStyle={[StyleUtils.getSearchTableColumnStyles(columnName, shouldShowYear)]}
isSortable={isColumnSortable}
isSortable={isSortable}
onPress={(order: SortOrder) => onSortPress(columnName, order)}
/>
);
Expand Down
Loading