Skip to content

Commit

Permalink
Fix sorting by merchant column
Browse files Browse the repository at this point in the history
  • Loading branch information
Kicu committed May 28, 2024
1 parent 5a71c3f commit fea6522
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
10 changes: 1 addition & 9 deletions src/components/SelectionList/TransactionListItem.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import Avatar from '@components/Avatar';
import Button from '@components/Button';
import Icon from '@components/Icon';
Expand All @@ -16,7 +15,6 @@ import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type {Transaction} from '@src/types/onyx';
import type {SearchAccountDetails, SearchTransactionType} from '@src/types/onyx/SearchResults';
import BaseListItem from './BaseListItem';
import TextWithIconCell from './TextWithIconCell';
Expand Down Expand Up @@ -54,17 +52,11 @@ function TransactionListItem<TItem extends ListItem>({
const {isLargeScreenWidth} = useWindowDimensions();
const StyleUtils = useStyleUtils();

function getMerchant() {
const merchant = TransactionUtils.getMerchant(transactionItem as OnyxEntry<Transaction>);
return merchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT || merchant === CONST.TRANSACTION.DEFAULT_MERCHANT ? '' : merchant;
}

const isFromExpenseReport = transactionItem.reportType === CONST.REPORT.TYPE.EXPENSE;
const date = TransactionUtils.getCreated(transactionItem, CONST.DATE.MONTH_DAY_ABBR_FORMAT);
const taxAmount = TransactionUtils.getTaxAmount(transactionItem, isFromExpenseReport);
const currency = TransactionUtils.getCurrency(transactionItem);
const description = TransactionUtils.getDescription(transactionItem);
const merchant = getMerchant();
const typeIcon = getTypeIcon(transactionItem.type);

const dateCell = (
Expand All @@ -78,7 +70,7 @@ function TransactionListItem<TItem extends ListItem>({
const merchantCell = (
<TextWithTooltip
shouldShowTooltip={showTooltip}
text={transactionItem.shouldShowMerchant ? merchant : description}
text={transactionItem.shouldShowMerchant ? transactionItem.formattedMerchant : description}
style={[styles.label, styles.pre, styles.justifyContentCenter]}
/>
);
Expand Down
3 changes: 3 additions & 0 deletions src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ type TransactionListItemType = ListItem &
/** final and formatted "total" value used for displaying and sorting */
formattedTotal: number;

/** final and formatted "merchant" value used for displaying and sorting */
formattedMerchant: string;

/** final "date" value used for sorting */
date: string;

Expand Down
9 changes: 6 additions & 3 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const columnNamesToSortingProperty = {
[CONST.SEARCH_TABLE_COLUMNS.FROM]: 'formattedFrom' as const,
[CONST.SEARCH_TABLE_COLUMNS.DATE]: 'date' as const,
[CONST.SEARCH_TABLE_COLUMNS.TAG]: 'tag' as const,
[CONST.SEARCH_TABLE_COLUMNS.MERCHANT]: 'merchant' as const,
[CONST.SEARCH_TABLE_COLUMNS.MERCHANT]: 'formattedMerchant' as const,
[CONST.SEARCH_TABLE_COLUMNS.TOTAL]: 'formattedTotal' as const,
[CONST.SEARCH_TABLE_COLUMNS.CATEGORY]: 'category' as const,
[CONST.SEARCH_TABLE_COLUMNS.TYPE]: 'type' as const,
Expand Down Expand Up @@ -61,9 +61,11 @@ function getTransactionsSections(data: OnyxTypes.SearchResults['data']): Transac
: (data.personalDetailsList?.[transactionItem.managerID] as SearchAccountDetails);

const formattedFrom = from.displayName ?? from.login ?? '';
const formattedTo = to.name ?? to.displayName ?? to.login ?? '';
const formattedTo = to?.name ?? to?.displayName ?? to?.login ?? '';
const formattedTotal = TransactionUtils.getAmount(transactionItem, isExpenseReport);
const date = transactionItem?.modifiedCreated ? transactionItem.modifiedCreated : transactionItem?.created;
const merchant = TransactionUtils.getMerchant(transactionItem);
const formattedMerchant = merchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT || merchant === CONST.TRANSACTION.DEFAULT_MERCHANT ? '' : merchant;

return {
...transactionItem,
Expand All @@ -73,6 +75,7 @@ function getTransactionsSections(data: OnyxTypes.SearchResults['data']): Transac
formattedTo,
date,
formattedTotal,
formattedMerchant,
shouldShowMerchant,
shouldShowCategory,
shouldShowTag,
Expand Down Expand Up @@ -117,7 +120,7 @@ function getSortedData(data: TransactionListItemType[], sortBy?: SearchColumnTyp
const aValue = a[sortingProperty];
const bValue = b[sortingProperty];

if (!aValue || !bValue) {
if (aValue === undefined || bValue === undefined) {
return 0;
}

Expand Down

0 comments on commit fea6522

Please sign in to comment.