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

Editor: Fix move to trash redirect save race conditions. #18275

Merged
merged 5 commits into from
Nov 5, 2019
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
9 changes: 6 additions & 3 deletions packages/edit-post/src/components/browser-url/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 );
10 changes: 7 additions & 3 deletions packages/editor/src/components/unsaved-changes-warning/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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 );