Skip to content

Commit

Permalink
Merge pull request #41338 from nkdengineer/fix/38983
Browse files Browse the repository at this point in the history
Remain draft comment when switching to current chat
  • Loading branch information
bondydaa authored May 7, 2024
2 parents 9f4bf60 + fc7df47 commit 55e146a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1220,8 +1220,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. */
Expand Down Expand Up @@ -1252,13 +1252,17 @@ function handleReportChanged(report: OnyxEntry<Report>) {
// 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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.
*
Expand Down

0 comments on commit 55e146a

Please sign in to comment.