diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 3060f53f12c3..47a76b0f75c2 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -254,6 +254,14 @@ Onyx.connect({ callback: (val) => (quickAction = val), }); +let allReportDraftComments: Record = {}; + +Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT, + waitForCollectionCallback: true, + callback: (value) => (allReportDraftComments = value), +}); + registerPaginationConfig({ initialCommand: WRITE_COMMANDS.OPEN_REPORT, previousCommand: READ_COMMANDS.GET_OLDER_ACTIONS, @@ -1321,6 +1329,7 @@ function handleReportChanged(report: OnyxEntry) { if (report?.reportID && report.preexistingReportID) { let callback = () => { Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, null); + Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`, null); }; // Only re-route them if they are still looking at the optimistically created report if (Navigation.getActiveRoute().includes(`/r/${report.reportID}`)) { @@ -1329,11 +1338,27 @@ function handleReportChanged(report: OnyxEntry) { currCallback(); Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.preexistingReportID ?? '-1'), CONST.NAVIGATION.TYPE.UP); }; + + // The report screen will listen to this event and transfer the draft comment to the existing report + // This will allow the newest draft comment to be transferred to the existing report + DeviceEventEmitter.emit(`switchToPreExistingReport_${report.reportID}`, { + preexistingReportID: report.preexistingReportID, + callback, + }); + + return; } - DeviceEventEmitter.emit(`switchToPreExistingReport_${report.reportID}`, { - preexistingReportID: report.preexistingReportID, - callback, - }); + + // In case the user is not on the report screen, we will transfer the report draft comment directly to the existing report + // after that clear the optimistically created report + const draftReportComment = allReportDraftComments?.[`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${report.reportID}`]; + if (!draftReportComment) { + callback(); + return; + } + + saveReportDraftComment(report.preexistingReportID ?? '-1', draftReportComment, callback); + return; }