Repository navigation
Move deleteReport to a separate file and use Onyx.connectWithoutView #82481
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
Merged
luacmartins
merged 15 commits into
Expensify:main
from
bernhardoj:chore/73651-use-connect-without-view
Jun 12, 2026
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
e0c0dfc
replace with connectWithoutView
bernhardoj e9e6333
lint
bernhardoj d092763
lint
bernhardoj 59e41f1
prettier
bernhardoj e681c5e
add test
bernhardoj 37f7779
lint
bernhardoj e2219c7
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj eb18bcf
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj 131434f
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj 936bad7
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj 40e0df0
import it correctly
bernhardoj f966310
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj f1d6f68
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj af51bcf
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj b82bdf2
Merge branch 'main' into chore/73651-use-connect-without-view
bernhardoj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import Onyx from 'react-native-onyx'; | ||
| import type {OnyxCollection} from 'react-native-onyx'; | ||
| import Log from '@libs/Log'; | ||
| import {getOriginalMessage, isMoneyRequestAction} from '@libs/ReportActionsUtils'; | ||
| import type CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; | ||
|
|
||
| // We use connectWithoutView because deleteReport doesn't affect the UI rendering | ||
| // and this avoids unnecessary re-rendering because we recursively delete the report and its children | ||
| // which requires us to subscribe to the whole report and report actions collection. | ||
| let allReports: OnyxCollection<Report>; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.COLLECTION.REPORT, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => (allReports = value), | ||
| }); | ||
|
|
||
| let allReportActions: OnyxCollection<ReportActions>; | ||
| Onyx.connectWithoutView({ | ||
| key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, | ||
| waitForCollectionCallback: true, | ||
| callback: (value) => (allReportActions = value), | ||
| }); | ||
|
|
||
| /** Deletes a report, along with its reportActions, any linked reports, and any linked IOU report. */ | ||
| function deleteReport(reportID: string | undefined, shouldDeleteChildReports = false) { | ||
| if (!reportID) { | ||
| Log.warn('[Report] deleteReport called with no reportID'); | ||
| return; | ||
| } | ||
| const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; | ||
| const onyxData: Record<string, null> = { | ||
| [`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]: null, | ||
| [`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]: null, | ||
| }; | ||
|
|
||
| // Delete linked transactions | ||
| const reportActionsForReport = allReportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`]; | ||
|
|
||
| const transactionIDs = Object.values(reportActionsForReport ?? {}) | ||
| .filter((reportAction): reportAction is ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> => isMoneyRequestAction(reportAction)) | ||
| .map((reportAction) => getOriginalMessage(reportAction)?.IOUTransactionID); | ||
|
|
||
| for (const transactionID of [...new Set(transactionIDs)]) { | ||
| onyxData[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] = null; | ||
| } | ||
|
|
||
| Onyx.multiSet(onyxData); | ||
|
|
||
| if (shouldDeleteChildReports) { | ||
| for (const reportAction of Object.values(reportActionsForReport ?? {})) { | ||
| if (!reportAction.childReportID) { | ||
| continue; | ||
| } | ||
| deleteReport(reportAction.childReportID, shouldDeleteChildReports); | ||
| } | ||
| } | ||
|
|
||
| // Delete linked IOU report | ||
| if (report?.iouReportID) { | ||
| deleteReport(report.iouReportID, shouldDeleteChildReports); | ||
| } | ||
| } | ||
|
|
||
| export default deleteReport; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| import Onyx from 'react-native-onyx'; | ||
| import deleteReport from '@libs/actions/Report/DeleteReport'; | ||
| import CONST from '@src/CONST'; | ||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {ReportAction} from '@src/types/onyx'; | ||
| import createRandomReportAction from '../utils/collections/reportActions'; | ||
| import {createRandomReport} from '../utils/collections/reports'; | ||
| import createRandomTransaction from '../utils/collections/transaction'; | ||
| import getOnyxValue from '../utils/getOnyxValue'; | ||
| import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; | ||
|
|
||
| // Mock Log to avoid noise in tests | ||
| jest.mock('@libs/Log'); | ||
|
|
||
| describe('actions/Report/DeleteReport', () => { | ||
| beforeAll(() => { | ||
| Onyx.init({ | ||
| keys: ONYXKEYS, | ||
| }); | ||
| }); | ||
|
|
||
| beforeEach(() => { | ||
| return Onyx.clear(); | ||
| }); | ||
|
|
||
| it('should delete a report, its actions, and linked transactions', async () => { | ||
| // Given a report with an IOU action and a linked transaction | ||
| const reportID = '1'; | ||
| const transactionID = '123'; | ||
| const report = createRandomReport(1, undefined); | ||
| const transaction = createRandomTransaction(Number(transactionID)); | ||
| const reportAction: ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.IOU> = { | ||
| ...createRandomReportAction(100), | ||
| actionName: CONST.REPORT.ACTIONS.TYPE.IOU, | ||
| previousMessage: [], | ||
| message: [{text: '', html: 'iou', type: 'COMMENT'}], | ||
| originalMessage: { | ||
| type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, | ||
| IOUTransactionID: transaction.transactionID, | ||
| IOUDetails: { | ||
| amount: transaction.amount, | ||
| currency: transaction.currency, | ||
| comment: '', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, { | ||
| [reportAction.reportActionID]: reportAction, | ||
| }); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`, transaction); | ||
|
|
||
| // When we delete the report | ||
| deleteReport(reportID); | ||
|
|
||
| await waitForBatchedUpdates(); | ||
|
|
||
| // Then the report, its actions, and the linked transaction should be deleted | ||
| expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`)).toBeUndefined(); | ||
| expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`)).toBeUndefined(); | ||
| expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should delete child reports recursively when shouldDeleteChildReports is true', async () => { | ||
| // Given a parent report with a child report | ||
| const parentReportID = '1'; | ||
| const childReportID = '2'; | ||
| const parentReport = createRandomReport(Number(parentReportID), undefined); | ||
| const childReport = createRandomReport(Number(childReportID), undefined); | ||
| const parentAction = { | ||
| ...createRandomReportAction(100), | ||
| childReportID, | ||
| }; | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`, childReport); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${parentReportID}`, { | ||
| [parentAction.reportActionID]: parentAction, | ||
| }); | ||
|
|
||
| // When we delete the parent report with shouldDeleteChildReports set to true | ||
| deleteReport(parentReportID, true); | ||
|
|
||
| await waitForBatchedUpdates(); | ||
|
|
||
| // Then both the parent and child reports should be deleted | ||
| expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`)).toBeUndefined(); | ||
| expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`)).toBeUndefined(); | ||
| }); | ||
|
|
||
| it('should delete linked IOU reports recursively', async () => { | ||
| // Given a report with a linked IOU report | ||
| const reportID = '1'; | ||
| const iouReportID = '2'; | ||
| const report = { | ||
| ...createRandomReport(Number(reportID), undefined), | ||
| iouReportID, | ||
| }; | ||
| const iouReport = createRandomReport(Number(iouReportID), undefined); | ||
|
|
||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, report); | ||
| await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`, iouReport); | ||
|
|
||
| // When we delete the report | ||
| deleteReport(reportID); | ||
|
|
||
| await waitForBatchedUpdates(); | ||
|
|
||
| // Then both reports should be deleted | ||
| expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`)).toBeUndefined(); | ||
| expect(await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`)).toBeUndefined(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.