From 0d1f913be5b5b7aaa9f95790e4175f2402480d80 Mon Sep 17 00:00:00 2001 From: Enrique Piqueras Date: Tue, 5 Nov 2019 07:13:22 -0800 Subject: [PATCH] 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 4d58c79ff412ec1d47c036f5a3deac24a8d8657a. --- packages/edit-post/src/components/browser-url/index.js | 9 ++++++--- .../src/components/unsaved-changes-warning/index.js | 10 +++++++--- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/packages/edit-post/src/components/browser-url/index.js b/packages/edit-post/src/components/browser-url/index.js index ccaa0bcaa98f7..34a8e889cefd4 100644 --- a/packages/edit-post/src/components/browser-url/index.js +++ b/packages/edit-post/src/components/browser-url/index.js @@ -42,10 +42,12 @@ export class BrowserURL extends Component { } componentDidUpdate( prevProps ) { - const { postId, postStatus, postType } = this.props; + const { postId, postStatus, postType, isSavingPost } = this.props; const { historyId } = this.state; - if ( postStatus === 'trash' ) { + // Posts are still dirty while saving so wait for saving to finish + // to avoid the unsaved changes warning when trashing posts. + if ( postStatus === 'trash' && ! isSavingPost ) { this.setTrashURL( postId, postType ); return; } @@ -92,12 +94,13 @@ export class BrowserURL extends Component { } export default withSelect( ( select ) => { - const { getCurrentPost } = select( 'core/editor' ); + const { getCurrentPost, isSavingPost } = select( 'core/editor' ); const { id, status, type } = getCurrentPost(); return { postId: id, postStatus: status, postType: type, + isSavingPost: isSavingPost(), }; } )( BrowserURL ); diff --git a/packages/editor/src/components/unsaved-changes-warning/index.js b/packages/editor/src/components/unsaved-changes-warning/index.js index 24599cc107104..66cd294bd61fb 100644 --- a/packages/editor/src/components/unsaved-changes-warning/index.js +++ b/packages/editor/src/components/unsaved-changes-warning/index.js @@ -27,9 +27,9 @@ class UnsavedChangesWarning extends Component { * @return {?string} Warning prompt message, if unsaved changes exist. */ warnIfUnsavedChanges( event ) { - const { isDirty } = this.props; + const { isEditedPostDirty } = this.props; - if ( isDirty ) { + if ( isEditedPostDirty() ) { event.returnValue = __( 'You have unsaved changes. If you proceed, they will be lost.' ); return event.returnValue; } @@ -41,5 +41,9 @@ class UnsavedChangesWarning extends Component { } export default withSelect( ( select ) => ( { - isDirty: select( 'core/editor' ).isEditedPostDirty(), + // We need to call the selector directly in the listener to avoid race + // conditions with `BrowserURL` where `componentDidUpdate` gets the + // new value of `isEditedPostDirty` before this component does, + // causing this component to incorrectly think a trashed post is still dirty. + isEditedPostDirty: select( 'core/editor' ).isEditedPostDirty, } ) )( UnsavedChangesWarning );