Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
ShridharGoel committed Jun 13, 2024
1 parent d80faaf commit 9322531
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 32 deletions.
9 changes: 2 additions & 7 deletions src/components/Search.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ type SearchProps = {

const sortableSearchTabs: SearchQuery[] = [CONST.TAB_SEARCH.ALL];

function isTransactionListItemType(item: TransactionListItemType | ReportListItemType): item is TransactionListItemType {
const transactionListItem = item as TransactionListItemType;
return transactionListItem.transactionID !== undefined;
}

function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) {
const {isOffline} = useNetwork();
const styles = useThemeStyles();
Expand Down Expand Up @@ -122,7 +117,7 @@ function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) {

const isSortingAllowed = sortableSearchTabs.includes(query);

const doesAtleastOneExpenseBelongToAPastYear = SearchUtils.doesAtleastOneExpenseBelongToAPastYear(searchResults?.data);
const shouldShowYear = SearchUtils.shouldShowYear(searchResults?.data);

return (
<SelectionList<ReportListItemType | TransactionListItemType>
Expand All @@ -133,7 +128,7 @@ function Search({query, policyIDs, sortBy, sortOrder}: SearchProps) {
sortOrder={sortOrder}
isSortingAllowed={isSortingAllowed}
sortBy={sortBy}
doesAtleastOneExpenseBelongToAPastYear={doesAtleastOneExpenseBelongToAPastYear}
shouldShowYear={shouldShowYear}
/>
}
// To enhance the smoothness of scrolling and minimize the risk of encountering blank spaces during scrolling,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ function ReceiptCell({transactionItem, isHovered = false}: ReceiptCellProps) {
function DateCell({transactionItem, showTooltip, isLargeScreenWidth}: TransactionCellProps) {
const styles = useThemeStyles();

const created = TransactionUtils.getCreatedDate(transactionItem);
const created = TransactionUtils.getCreated(transactionItem);
const date = DateUtils.formatWithUTCTimeZone(created, DateUtils.doesDateBelongToAPastYear(created) ? CONST.DATE.MONTH_DAY_YEAR_ABBR_FORMAT : CONST.DATE.MONTH_DAY_ABBR_FORMAT);

return (
Expand Down Expand Up @@ -302,7 +302,7 @@ function TransactionListItemRow({
isHovered={isHovered}
/>
</View>
<View style={[StyleUtils.getSearchTableColumnStyles(CONST.SEARCH_TABLE_COLUMNS.DATE, item.doesAtleastOneExpenseBelongToAPastYear)]}>
<View style={[StyleUtils.getSearchTableColumnStyles(CONST.SEARCH_TABLE_COLUMNS.DATE, item.shouldShowYear)]}>
<DateCell
transactionItem={item}
showTooltip={showTooltip}
Expand Down
6 changes: 3 additions & 3 deletions src/components/SelectionList/SearchTableHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ type SearchTableHeaderProps = {
sortOrder?: SortOrder;
isSortingAllowed: boolean;
onSortPress: (column: SearchColumnType, order: SortOrder) => void;
doesAtleastOneExpenseBelongToAPastYear: boolean;
shouldShowYear: boolean;
};

function SearchTableHeader({data, sortBy, sortOrder, isSortingAllowed, onSortPress, doesAtleastOneExpenseBelongToAPastYear}: SearchTableHeaderProps) {
function SearchTableHeader({data, sortBy, sortOrder, isSortingAllowed, onSortPress, shouldShowYear}: SearchTableHeaderProps) {
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const {isSmallScreenWidth, isMediumScreenWidth} = useWindowDimensions();
Expand Down Expand Up @@ -124,7 +124,7 @@ function SearchTableHeader({data, sortBy, sortOrder, isSortingAllowed, onSortPre
textStyle={textStyle}
sortOrder={sortOrder ?? CONST.SORT_ORDER.ASC}
isActive={isActive}
containerStyle={[StyleUtils.getSearchTableColumnStyles(columnName, doesAtleastOneExpenseBelongToAPastYear)]}
containerStyle={[StyleUtils.getSearchTableColumnStyles(columnName, shouldShowYear)]}
isSortable={isSortable}
onPress={(order: SortOrder) => onSortPress(columnName, order)}
/>
Expand Down
6 changes: 4 additions & 2 deletions src/components/SelectionList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,10 @@ type TransactionListItemType = ListItem &
/** Whether we should show the tax column */
shouldShowTax: boolean;

/** Info about whether there's atleast one transaction that doesn't belong to the present year */
doesAtleastOneExpenseBelongToAPastYear: boolean;
/** Whether we should show the transaction year.
* This is true if at least one transaction in the data set was create in a previous year
*/
shouldShowYear: boolean;
};

type ReportListItemType = ListItem &
Expand Down
4 changes: 2 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2558,7 +2558,7 @@ function getTransactionDetails(transaction: OnyxEntry<Transaction>, createdDateF
}
const report = getReport(transaction?.reportID);
return {
created: TransactionUtils.getCreated(transaction, createdDateFormat),
created: TransactionUtils.getFormattedCreated(transaction, createdDateFormat),
amount: TransactionUtils.getAmount(transaction, !isEmptyObject(report) && isExpenseReport(report)),
taxAmount: TransactionUtils.getTaxAmount(transaction, !isEmptyObject(report) && isExpenseReport(report)),
taxCode: TransactionUtils.getTaxCode(transaction),
Expand Down Expand Up @@ -3011,7 +3011,7 @@ function getModifiedExpenseOriginalMessage(
originalMessage.newComment = transactionChanges?.comment;
}
if ('created' in transactionChanges) {
originalMessage.oldCreated = TransactionUtils.getCreated(oldTransaction);
originalMessage.oldCreated = TransactionUtils.getFormattedCreated(oldTransaction);
originalMessage.created = transactionChanges?.created;
}
if ('merchant' in transactionChanges) {
Expand Down
22 changes: 14 additions & 8 deletions src/libs/SearchUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,19 @@ function getShouldShowMerchant(data: OnyxTypes.SearchResults['data']): boolean {

const currentYear = new Date().getFullYear();

function doesAtleastOneExpenseBelongToAPastYear(data: TransactionListItemType[] | ReportListItemType[] | OnyxTypes.SearchResults['data']): boolean {

function isTransactionListItemType(item: TransactionListItemType | ReportListItemType): item is TransactionListItemType {
const transactionListItem = item as TransactionListItemType;
return transactionListItem.transactionID !== undefined;
}

function shouldShowYear(data: TransactionListItemType[] | ReportListItemType[] | OnyxTypes.SearchResults['data']): boolean {
if (Array.isArray(data)) {
return data.some((item: TransactionListItemType | ReportListItemType) => {
if ('transactions' in item) {
// If the item is a ReportListItemType, iterate over its transactions and check them
return item.transactions.some((transaction) => {
const transactionYear = new Date(TransactionUtils.getCreatedDate(transaction)).getFullYear();
const transactionYear = new Date(TransactionUtils.getCreated(transaction)).getFullYear();
return transactionYear !== currentYear;
});
}
Expand All @@ -98,7 +104,7 @@ function doesAtleastOneExpenseBelongToAPastYear(data: TransactionListItemType[]
for (const [key, transactionItem] of Object.entries(data)) {
if (key.startsWith(ONYXKEYS.COLLECTION.TRANSACTION)) {
const item = transactionItem as SearchTransaction;
const date = TransactionUtils.getCreatedDate(item);
const date = TransactionUtils.getCreated(item);

if (DateUtils.doesDateBelongToAPastYear(date)) {
return true;
Expand All @@ -111,7 +117,7 @@ function doesAtleastOneExpenseBelongToAPastYear(data: TransactionListItemType[]
function getTransactionsSections(data: OnyxTypes.SearchResults['data']): TransactionListItemType[] {
const shouldShowMerchant = getShouldShowMerchant(data);

const doesDataContainAPastYearTransaction = doesAtleastOneExpenseBelongToAPastYear(data);
const doesDataContainAPastYearTransaction = shouldShowYear(data);

return Object.entries(data)
.filter(([key]) => key.startsWith(ONYXKEYS.COLLECTION.TRANSACTION))
Expand All @@ -138,15 +144,15 @@ function getTransactionsSections(data: OnyxTypes.SearchResults['data']): Transac
shouldShowTag: true,
shouldShowTax: true,
keyForList: transactionItem.transactionID,
doesAtleastOneExpenseBelongToAPastYear: doesDataContainAPastYearTransaction,
shouldShowYear: doesDataContainAPastYearTransaction,
};
});
}

function getReportSections(data: OnyxTypes.SearchResults['data']): ReportListItemType[] {
const shouldShowMerchant = getShouldShowMerchant(data);

const doesDataContainAPastYearTransaction = doesAtleastOneExpenseBelongToAPastYear(data);
const doesDataContainAPastYearTransaction = shouldShowYear(data);

const reportIDToTransactions: Record<string, ReportListItemType> = {};
for (const key in data) {
Expand Down Expand Up @@ -183,7 +189,7 @@ function getReportSections(data: OnyxTypes.SearchResults['data']): ReportListIte
shouldShowTag: true,
shouldShowTax: true,
keyForList: transactionItem.transactionID,
doesAtleastOneExpenseBelongToAPastYear: doesDataContainAPastYearTransaction,
shouldShowYear: doesDataContainAPastYearTransaction,
};
if (reportIDToTransactions[reportKey]?.transactions) {
reportIDToTransactions[reportKey].transactions.push(transaction);
Expand Down Expand Up @@ -268,5 +274,5 @@ function getSearchParams() {
return topmostCentralPaneRoute?.params as CentralPaneNavigatorParamList['Search_Central_Pane'];
}

export {getListItem, getQueryHash, getSections, getSortedSections, getShouldShowMerchant, getSearchType, getSearchParams, doesAtleastOneExpenseBelongToAPastYear};
export {getListItem, getQueryHash, getSections, getSortedSections, getShouldShowMerchant, getSearchType, getSearchParams, shouldShowYear};
export type {SearchColumnType, SortOrder};
8 changes: 4 additions & 4 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,16 +459,16 @@ function getTagForDisplay(transaction: OnyxEntry<Transaction>, tagIndex?: number
return getCleanedTagName(getTag(transaction, tagIndex));
}

function getCreatedDate(transaction: OnyxEntry<Transaction>): string {
function getCreated(transaction: OnyxEntry<Transaction>): string {
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
return transaction?.modifiedCreated ? transaction.modifiedCreated : transaction?.created || '';
}

/**
* Return the created field from the transaction, return the modifiedCreated if present.
*/
function getCreated(transaction: OnyxEntry<Transaction>, dateFormat: string = CONST.DATE.FNS_FORMAT_STRING): string {
const created = getCreatedDate(transaction);
function getFormattedCreated(transaction: OnyxEntry<Transaction>, dateFormat: string = CONST.DATE.FNS_FORMAT_STRING): string {
const created = getCreated(transaction);
return DateUtils.formatWithUTCTimeZone(created, dateFormat);
}

Expand Down Expand Up @@ -815,8 +815,8 @@ export {
getOriginalAmount,
getMerchant,
getMCCGroup,
getCreatedDate,
getCreated,
getFormattedCreated,
getCategory,
getBillable,
getTag,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/iou/request/step/IOURequestStepDate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function IOURequestStepDate({
const isEditing = action === CONST.IOU.ACTION.EDIT;
// In the split flow, when editing we use SPLIT_TRANSACTION_DRAFT to save draft value
const isEditingSplitBill = iouType === CONST.IOU.TYPE.SPLIT && isEditing;
const currentCreated = isEditingSplitBill && !lodashIsEmpty(splitDraftTransaction) ? TransactionUtils.getCreated(splitDraftTransaction) : TransactionUtils.getCreated(transaction);
const currentCreated = isEditingSplitBill && !lodashIsEmpty(splitDraftTransaction) ? TransactionUtils.getFormattedCreated(splitDraftTransaction) : TransactionUtils.getFormattedCreated(transaction);
const parentReportAction = reportActions?.[(isEditingSplitBill ? reportActionID : report?.parentReportActionID) ?? 0];
const canEditingSplitBill =
isEditingSplitBill && session && parentReportAction && session.accountID === parentReportAction.actorAccountID && TransactionUtils.areRequiredFieldsEmpty(transaction);
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/TransactionUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('TransactionUtils', () => {
it('returns the "modifiedCreated" date with the correct format', () => {
const expectedResult = '2023-10-25';

const result = TransactionUtils.getCreated(transaction);
const result = TransactionUtils.getFormattedCreated(transaction);

expect(result).toEqual(expectedResult);
});
Expand All @@ -39,7 +39,7 @@ describe('TransactionUtils', () => {
it('returns the "created" date with the correct format', () => {
const expectedResult = '2023-10-01';

const result = TransactionUtils.getCreated(transaction);
const result = TransactionUtils.getFormattedCreated(transaction);

expect(result).toEqual(expectedResult);
});
Expand All @@ -54,7 +54,7 @@ describe('TransactionUtils', () => {
it('returns an empty string', () => {
const expectedResult = '';

const result = TransactionUtils.getCreated(transaction);
const result = TransactionUtils.getFormattedCreated(transaction);

expect(result).toEqual(expectedResult);
});
Expand Down

0 comments on commit 9322531

Please sign in to comment.