Skip to content

Commit 0d1f913

Browse files
epiquerasjorgefilipecosta
authored andcommitted
Editor: Fix move to trash redirect save race conditions. (#18275)
* Editor: Fix move to trash redirect save race conditions. * Editor: Add e2e test. * Editor: Expand comment. * Fix BrowserURL capitalization * Revert "Editor: Add e2e test." This reverts commit 4d58c79.
1 parent 75a7c95 commit 0d1f913

File tree

2 files changed

+13
-6
lines changed
  • packages
    • edit-post/src/components/browser-url
    • editor/src/components/unsaved-changes-warning

2 files changed

+13
-6
lines changed

packages/edit-post/src/components/browser-url/index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,12 @@ export class BrowserURL extends Component {
4242
}
4343

4444
componentDidUpdate( prevProps ) {
45-
const { postId, postStatus, postType } = this.props;
45+
const { postId, postStatus, postType, isSavingPost } = this.props;
4646
const { historyId } = this.state;
4747

48-
if ( postStatus === 'trash' ) {
48+
// Posts are still dirty while saving so wait for saving to finish
49+
// to avoid the unsaved changes warning when trashing posts.
50+
if ( postStatus === 'trash' && ! isSavingPost ) {
4951
this.setTrashURL( postId, postType );
5052
return;
5153
}
@@ -92,12 +94,13 @@ export class BrowserURL extends Component {
9294
}
9395

9496
export default withSelect( ( select ) => {
95-
const { getCurrentPost } = select( 'core/editor' );
97+
const { getCurrentPost, isSavingPost } = select( 'core/editor' );
9698
const { id, status, type } = getCurrentPost();
9799

98100
return {
99101
postId: id,
100102
postStatus: status,
101103
postType: type,
104+
isSavingPost: isSavingPost(),
102105
};
103106
} )( BrowserURL );

packages/editor/src/components/unsaved-changes-warning/index.js

+7-3
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class UnsavedChangesWarning extends Component {
2727
* @return {?string} Warning prompt message, if unsaved changes exist.
2828
*/
2929
warnIfUnsavedChanges( event ) {
30-
const { isDirty } = this.props;
30+
const { isEditedPostDirty } = this.props;
3131

32-
if ( isDirty ) {
32+
if ( isEditedPostDirty() ) {
3333
event.returnValue = __( 'You have unsaved changes. If you proceed, they will be lost.' );
3434
return event.returnValue;
3535
}
@@ -41,5 +41,9 @@ class UnsavedChangesWarning extends Component {
4141
}
4242

4343
export default withSelect( ( select ) => ( {
44-
isDirty: select( 'core/editor' ).isEditedPostDirty(),
44+
// We need to call the selector directly in the listener to avoid race
45+
// conditions with `BrowserURL` where `componentDidUpdate` gets the
46+
// new value of `isEditedPostDirty` before this component does,
47+
// causing this component to incorrectly think a trashed post is still dirty.
48+
isEditedPostDirty: select( 'core/editor' ).isEditedPostDirty,
4549
} ) )( UnsavedChangesWarning );

0 commit comments

Comments
 (0)