From 489584ae3a2de5931a9e43e31257c15abec759dc Mon Sep 17 00:00:00 2001 From: Olgierd Date: Fri, 5 Dec 2025 16:30:57 +0100 Subject: [PATCH 1/3] Make AddUnreportedExpense use new SelectionList --- src/libs/TransactionUtils/index.ts | 27 ++++------ src/pages/AddUnreportedExpense.tsx | 70 ++++++++++++++++---------- tests/unit/AddUnreportedExpenseTest.ts | 30 +++++++---- 3 files changed, 75 insertions(+), 52 deletions(-) diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index e84930250856..19a8a22d58b7 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -2204,22 +2204,17 @@ function getChildTransactions(transactions: OnyxCollection, reports /** * Creates sections data for unreported expenses, marking transactions with DELETE pending action as disabled */ -function createUnreportedExpenseSections(transactions: Array): Array<{shouldShow: boolean; data: UnreportedExpenseListItemType[]}> { - return [ - { - shouldShow: true, - data: transactions - .filter((t): t is Transaction => t !== undefined) - .map( - (transaction): UnreportedExpenseListItemType => ({ - ...transaction, - isDisabled: isTransactionPendingDelete(transaction), - keyForList: transaction.transactionID, - errors: transaction.errors as Errors | undefined, - }), - ), - }, - ]; +function createUnreportedExpenseSections(transactions: Array): Array { + return transactions + .filter((t): t is Transaction => t !== undefined) + .map( + (transaction): UnreportedExpenseListItemType => ({ + ...transaction, + isDisabled: isTransactionPendingDelete(transaction), + keyForList: transaction.transactionID, + errors: transaction.errors as Errors | undefined, + }), + ); } function isExpenseUnreported(transaction?: Transaction): transaction is UnreportedTransaction { diff --git a/src/pages/AddUnreportedExpense.tsx b/src/pages/AddUnreportedExpense.tsx index 3e882d195719..4138d2d05f5d 100644 --- a/src/pages/AddUnreportedExpense.tsx +++ b/src/pages/AddUnreportedExpense.tsx @@ -8,8 +8,8 @@ import HeaderWithBackButton from '@components/HeaderWithBackButton'; import LottieAnimations from '@components/LottieAnimations'; import {useSession} from '@components/OnyxListItemProvider'; import ScreenWrapper from '@components/ScreenWrapper'; -import SelectionList from '@components/SelectionListWithSections'; -import type {ListItem, SectionListDataType, SelectionListHandle} from '@components/SelectionListWithSections/types'; +import SelectionList from '@components/SelectionList'; +import type {ListItem, SelectionListHandle} from '@components/SelectionList/types'; import UnreportedExpensesSkeleton from '@components/Skeletons/UnreportedExpensesSkeleton'; import useDebouncedState from '@hooks/useDebouncedState'; import useLocalize from '@hooks/useLocalize'; @@ -70,7 +70,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { return []; } return Object.values(transactions || {}).filter((item) => { - const isUnreported = item?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID || item?.reportID === ''; + const isUnreported = true; if (!isUnreported) { return false; } @@ -153,7 +153,12 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { }); }, [debouncedSearchValue, shouldShowTextInput, transactions]); - const sections: Array> = useMemo(() => createUnreportedExpenseSections(filteredTransactions), [filteredTransactions]); + const unreportedExpenses = useMemo(() => { + return createUnreportedExpenseSections(filteredTransactions).map((item) => ({ + ...item, + isSelected: selectedIds.has(item.transactionID), + })); + }, [filteredTransactions, selectedIds]); const handleConfirm = useCallback(() => { if (selectedIds.size === 0) { @@ -212,11 +217,39 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { }, [errorMessage, styles, translate, handleConfirm]); const headerMessage = useMemo(() => { - if (debouncedSearchValue.trim() && sections.at(0)?.data.length === 0) { + if (debouncedSearchValue.trim() && unreportedExpenses?.length === 0) { return translate('common.noResultsFound'); } return ''; - }, [debouncedSearchValue, sections, translate]); + }, [debouncedSearchValue, unreportedExpenses, translate]); + + const textInputOptions = useMemo( + () => ({ + value: searchValue, + label: shouldShowTextInput ? translate('iou.findExpense') : undefined, + onChangeText: setSearchValue, + headerMessage, + }), + [searchValue, shouldShowTextInput, translate, setSearchValue, headerMessage], + ); + + const onSelectRow = useCallback( + (item: {transactionID: string}) => { + setSelectedIds((prevIds) => { + const newIds = new Set(prevIds); + if (newIds.has(item.transactionID)) { + newIds.delete(item.transactionID); + } else { + newIds.add(item.transactionID); + if (errorMessage) { + setErrorMessage(''); + } + } + return newIds; + }); + }, + [errorMessage], + ); const hasSearchTerm = debouncedSearchValue.trim().length > 0; const isShowingEmptyState = !hasSearchTerm && transactions.length === 0; @@ -298,36 +331,19 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { onBackButtonPress={Navigation.goBack} /> + data={unreportedExpenses} ref={selectionListRef} - onSelectRow={(item) => { - setSelectedIds((prevIds) => { - const newIds = new Set(prevIds); - if (newIds.has(item.transactionID)) { - newIds.delete(item.transactionID); - } else { - newIds.add(item.transactionID); - if (errorMessage) { - setErrorMessage(''); - } - } - - return newIds; - }); - }} - isSelected={(item) => selectedIds.has(item.transactionID)} + onSelectRow={onSelectRow} + textInputOptions={textInputOptions} shouldShowTextInput={shouldShowTextInput} - textInputValue={searchValue} - textInputLabel={shouldShowTextInput ? translate('iou.findExpense') : undefined} - onChangeText={setSearchValue} - headerMessage={headerMessage} canSelectMultiple - sections={sections} ListItem={UnreportedExpenseListItem} onEndReached={fetchMoreUnreportedTransactions} onEndReachedThreshold={0.75} addBottomSafeAreaPadding listFooterContent={shouldShowUnreportedTransactionsSkeletons ? : undefined} footerContent={footerContent} + disableMaintainingScrollPosition /> ); diff --git a/tests/unit/AddUnreportedExpenseTest.ts b/tests/unit/AddUnreportedExpenseTest.ts index 06c3cbdafcc4..59e1552abb95 100644 --- a/tests/unit/AddUnreportedExpenseTest.ts +++ b/tests/unit/AddUnreportedExpenseTest.ts @@ -44,15 +44,15 @@ describe('AddUnreportedExpense', () => { const sections = createUnreportedExpenseSections(transactions); // Should create one section - expect(sections).toHaveLength(1); - expect(sections.at(0)?.shouldShow).toBe(true); - expect(sections.at(0)?.data).toHaveLength(2); + expect(sections).toHaveLength(2); - const processedNormalTransaction = sections.at(0)?.data.find((t) => t.transactionID === '123'); + const processedNormalTransaction = sections.find((t) => t.transactionID === '123'); expect(processedNormalTransaction?.isDisabled).toBe(false); + expect(processedNormalTransaction?.keyForList).toBe('123'); - const processedDeletedTransaction = sections.at(0)?.data.find((t) => t.transactionID === '456'); + const processedDeletedTransaction = sections.find((t) => t.transactionID === '456'); expect(processedDeletedTransaction?.isDisabled).toBe(true); + expect(processedDeletedTransaction?.keyForList).toBe('456'); }); it('should not mark transactions without DELETE pendingAction as disabled', () => { @@ -80,9 +80,9 @@ describe('AddUnreportedExpense', () => { const transactions = [normalTransaction, updateTransaction, addTransaction]; const sections = createUnreportedExpenseSections(transactions); - expect(sections.at(0)?.data).toHaveLength(3); + expect(sections).toHaveLength(3); // eslint-disable-next-line unicorn/no-array-for-each - sections.at(0)?.data.forEach((transaction) => { + sections.forEach((transaction) => { expect(transaction.isDisabled).toBe(false); }); }); @@ -105,12 +105,24 @@ describe('AddUnreportedExpense', () => { const transactions = [deletedTransaction1, deletedTransaction2]; const sections = createUnreportedExpenseSections(transactions); - expect(sections.at(0)?.data).toHaveLength(2); + expect(sections).toHaveLength(2); // eslint-disable-next-line unicorn/no-array-for-each - sections.at(0)?.data.forEach((transaction) => { + sections.forEach((transaction) => { expect(transaction.isDisabled).toBe(true); expect(transaction.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); }); }); + + it('should filter out undefined transactions', () => { + const normalTransaction = generateTransaction({ + transactionID: '123', + }); + + const transactions = [normalTransaction, undefined]; + const result = createUnreportedExpenseSections(transactions); + + expect(result).toHaveLength(1); + expect(result[0].transactionID).toBe('123'); + }); }); }); From 408e21545071a23e0b5c86e33f3b445dc2ac2364 Mon Sep 17 00:00:00 2001 From: Olgierd Date: Fri, 5 Dec 2025 17:18:24 +0100 Subject: [PATCH 2/3] Bring back correct const and make 'unreportedExpenses' prop dependency more specific --- src/pages/AddUnreportedExpense.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pages/AddUnreportedExpense.tsx b/src/pages/AddUnreportedExpense.tsx index 4138d2d05f5d..733ddae0db16 100644 --- a/src/pages/AddUnreportedExpense.tsx +++ b/src/pages/AddUnreportedExpense.tsx @@ -70,7 +70,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { return []; } return Object.values(transactions || {}).filter((item) => { - const isUnreported = true; + const isUnreported = item?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID || item?.reportID === ''; if (!isUnreported) { return false; } @@ -221,7 +221,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { return translate('common.noResultsFound'); } return ''; - }, [debouncedSearchValue, unreportedExpenses, translate]); + }, [debouncedSearchValue, unreportedExpenses?.length, translate]); const textInputOptions = useMemo( () => ({ From de8556f3557953c6f8eb691097212baee080ffd8 Mon Sep 17 00:00:00 2001 From: Olgierd Date: Mon, 8 Dec 2025 14:09:44 +0100 Subject: [PATCH 3/3] Change function name to createUnreportedExpenses and fix eslint checks --- src/libs/TransactionUtils/index.ts | 4 ++-- src/pages/AddUnreportedExpense.tsx | 4 ++-- tests/unit/AddUnreportedExpenseTest.ts | 31 +++++++++++++------------- 3 files changed, 19 insertions(+), 20 deletions(-) diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index eef9fc0542d6..09e69adc58ce 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -2236,7 +2236,7 @@ function getChildTransactions(transactions: OnyxCollection, reports /** * Creates sections data for unreported expenses, marking transactions with DELETE pending action as disabled */ -function createUnreportedExpenseSections(transactions: Array): Array { +function createUnreportedExpenses(transactions: Array): UnreportedExpenseListItemType[] { return transactions .filter((t): t is Transaction => t !== undefined) .map( @@ -2359,7 +2359,7 @@ export { getTransactionPendingAction, isTransactionPendingDelete, getChildTransactions, - createUnreportedExpenseSections, + createUnreportedExpenses, isDemoTransaction, shouldShowViolation, isUnreportedAndHasInvalidDistanceRateTransaction, diff --git a/src/pages/AddUnreportedExpense.tsx b/src/pages/AddUnreportedExpense.tsx index a220166f56c2..6e8dbe781c0e 100644 --- a/src/pages/AddUnreportedExpense.tsx +++ b/src/pages/AddUnreportedExpense.tsx @@ -27,7 +27,7 @@ import {canSubmitPerDiemExpenseFromWorkspace, getPerDiemCustomUnit} from '@libs/ import {getTransactionDetails, isIOUReport} from '@libs/ReportUtils'; import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; import tokenizedSearch from '@libs/tokenizedSearch'; -import {createUnreportedExpenseSections, getAmount, getCurrency, getDescription, getMerchant, isPerDiemRequest} from '@libs/TransactionUtils'; +import {createUnreportedExpenses, getAmount, getCurrency, getDescription, getMerchant, isPerDiemRequest} from '@libs/TransactionUtils'; import Navigation from '@navigation/Navigation'; import type {PlatformStackScreenProps} from '@navigation/PlatformStackNavigation/types'; import {convertBulkTrackedExpensesToIOU, startMoneyRequest} from '@userActions/IOU'; @@ -154,7 +154,7 @@ function AddUnreportedExpense({route}: AddUnreportedExpensePageType) { }, [debouncedSearchValue, shouldShowTextInput, transactions]); const unreportedExpenses = useMemo(() => { - return createUnreportedExpenseSections(filteredTransactions).map((item) => ({ + return createUnreportedExpenses(filteredTransactions).map((item) => ({ ...item, isSelected: selectedIds.has(item.transactionID), })); diff --git a/tests/unit/AddUnreportedExpenseTest.ts b/tests/unit/AddUnreportedExpenseTest.ts index 59e1552abb95..b0ccbe60cc06 100644 --- a/tests/unit/AddUnreportedExpenseTest.ts +++ b/tests/unit/AddUnreportedExpenseTest.ts @@ -1,4 +1,4 @@ -import {createUnreportedExpenseSections} from '@libs/TransactionUtils'; +import {createUnreportedExpenses} from '@libs/TransactionUtils'; import CONST from '@src/CONST'; import type Transaction from '@src/types/onyx/Transaction'; @@ -24,7 +24,7 @@ function generateTransaction(values: Partial = {}): Transaction { } describe('AddUnreportedExpense', () => { - describe('createUnreportedExpenseSections', () => { + describe('createUnreportedExpenses', () => { it('should mark transactions with DELETE pendingAction as disabled', () => { const normalTransaction = generateTransaction({ transactionID: '123', @@ -41,16 +41,15 @@ describe('AddUnreportedExpense', () => { }); const transactions = [normalTransaction, deletedTransaction]; - const sections = createUnreportedExpenseSections(transactions); + const unreportedExpenses = createUnreportedExpenses(transactions); - // Should create one section - expect(sections).toHaveLength(2); + expect(unreportedExpenses).toHaveLength(2); - const processedNormalTransaction = sections.find((t) => t.transactionID === '123'); + const processedNormalTransaction = unreportedExpenses.find((t) => t.transactionID === '123'); expect(processedNormalTransaction?.isDisabled).toBe(false); expect(processedNormalTransaction?.keyForList).toBe('123'); - const processedDeletedTransaction = sections.find((t) => t.transactionID === '456'); + const processedDeletedTransaction = unreportedExpenses.find((t) => t.transactionID === '456'); expect(processedDeletedTransaction?.isDisabled).toBe(true); expect(processedDeletedTransaction?.keyForList).toBe('456'); }); @@ -78,11 +77,11 @@ describe('AddUnreportedExpense', () => { }); const transactions = [normalTransaction, updateTransaction, addTransaction]; - const sections = createUnreportedExpenseSections(transactions); + const unreportedExpenses = createUnreportedExpenses(transactions); - expect(sections).toHaveLength(3); + expect(unreportedExpenses).toHaveLength(3); // eslint-disable-next-line unicorn/no-array-for-each - sections.forEach((transaction) => { + unreportedExpenses.forEach((transaction) => { expect(transaction.isDisabled).toBe(false); }); }); @@ -103,11 +102,11 @@ describe('AddUnreportedExpense', () => { }); const transactions = [deletedTransaction1, deletedTransaction2]; - const sections = createUnreportedExpenseSections(transactions); + const unreportedExpenses = createUnreportedExpenses(transactions); - expect(sections).toHaveLength(2); + expect(unreportedExpenses).toHaveLength(2); // eslint-disable-next-line unicorn/no-array-for-each - sections.forEach((transaction) => { + unreportedExpenses.forEach((transaction) => { expect(transaction.isDisabled).toBe(true); expect(transaction.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE); }); @@ -119,10 +118,10 @@ describe('AddUnreportedExpense', () => { }); const transactions = [normalTransaction, undefined]; - const result = createUnreportedExpenseSections(transactions); + const unreportedExpenses = createUnreportedExpenses(transactions); - expect(result).toHaveLength(1); - expect(result[0].transactionID).toBe('123'); + expect(unreportedExpenses).toHaveLength(1); + expect(unreportedExpenses.at(0)?.transactionID).toBe('123'); }); }); });