diff --git a/src/libs/actions/Report.ts b/src/libs/actions/Report.ts index 0172b137d8ca..a20dde56b4aa 100644 --- a/src/libs/actions/Report.ts +++ b/src/libs/actions/Report.ts @@ -1214,8 +1214,8 @@ function togglePinnedState(reportID: string, isPinnedChat: boolean) { * tab, refresh etc without worrying about loosing what they typed out. * When empty string or null is passed, it will delete the draft comment from Onyx store. */ -function saveReportDraftComment(reportID: string, comment: string | null) { - Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, prepareDraftComment(comment)); +function saveReportDraftComment(reportID: string, comment: string | null, callback: () => void = () => {}) { + Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_DRAFT_COMMENT}${reportID}`, prepareDraftComment(comment)).then(callback); } /** Broadcasts whether or not a user is typing on a report over the report's private pusher channel. */ @@ -1246,13 +1246,17 @@ function handleReportChanged(report: OnyxEntry) { // In this case, the API will let us know by returning a preexistingReportID. // We should clear out the optimistically created report and re-route the user to the preexisting report. if (report?.reportID && report.preexistingReportID) { - Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, null); - + let callback = () => {}; // Only re-route them if they are still looking at the optimistically created report if (Navigation.getActiveRoute().includes(`/r/${report.reportID}`)) { - // Pass 'FORCED_UP' type to replace new report on second login with proper one in the Navigation - Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.preexistingReportID), CONST.NAVIGATION.TYPE.FORCED_UP); + callback = () => { + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(report.preexistingReportID ?? ''), CONST.NAVIGATION.TYPE.FORCED_UP); + }; } + DeviceEventEmitter.emit(`switchToPreExistingReport_${report.reportID}`, { + preexistingReportID: report.preexistingReportID, + callback, + }); return; } diff --git a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx index 469a7300a84f..3120bbe9bed2 100644 --- a/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx +++ b/src/pages/home/report/ReportActionCompose/ComposerWithSuggestions/ComposerWithSuggestions.tsx @@ -12,7 +12,7 @@ import type { TextInputKeyPressEventData, TextInputSelectionChangeEventData, } from 'react-native'; -import {findNodeHandle, InteractionManager, NativeModules, View} from 'react-native'; +import {DeviceEventEmitter, findNodeHandle, InteractionManager, NativeModules, View} from 'react-native'; import type {OnyxEntry} from 'react-native-onyx'; import {withOnyx} from 'react-native-onyx'; import type {useAnimatedRef} from 'react-native-reanimated'; @@ -344,6 +344,20 @@ function ComposerWithSuggestions( [], ); + useEffect(() => { + const switchToCurrentReport = DeviceEventEmitter.addListener(`switchToPreExistingReport_${reportID}`, ({preexistingReportID, callback}) => { + if (!commentRef.current) { + callback(); + return; + } + Report.saveReportDraftComment(preexistingReportID, commentRef.current, callback); + }); + + return () => { + switchToCurrentReport.remove(); + }; + }, [reportID]); + /** * Find the newly added characters between the previous text and the new text based on the selection. *