Skip to content
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

Remain draft comment when switching to current chat #41338

Merged
merged 6 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/libs/actions/Report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -1246,13 +1246,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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line removal caused regression.
We added back in #43633.


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
Loading