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 3 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
22 changes: 22 additions & 0 deletions packages/e2e-tests/specs/editor/various/change-detection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
pressKeyWithModifier,
ensureSidebarOpened,
publishPost,
openDocumentSettingsSidebar,
} from '@wordpress/e2e-test-utils';

describe( 'Change detection', () => {
Expand Down Expand Up @@ -348,4 +349,25 @@ describe( 'Change detection', () => {
// Verify that the post is not dirty.
await assertIsDirty( false );
} );

it( 'should not prompt to confirm unsaved changes when trashing an existing post', async () => {
// Enter title.
await page.type( '.editor-post-title__input', 'Hello World' );

// Save
await Promise.all( [
Copy link
Member

Choose a reason for hiding this comment

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

Aside: There's a helper for this saveDraft. The file as a whole could do for some refactoring to use it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good catch.

// Wait for "Saved" to confirm save complete.
page.waitForSelector( '.editor-post-saved-state.is-saved' ),

// Keyboard shortcut Ctrl+S save.
pressKeyWithModifier( 'primary', 'S' ),
] );

// Trash post.
await openDocumentSettingsSidebar();
await page.click( '.editor-post-trash.components-button' );

// Check that the dialog didn't show.
await assertIsDirty( false );
Copy link
Member

Choose a reason for hiding this comment

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

assertIsDirty does its check by performing a reload, which I sense would conflict with clicking the trash button. Rather, I think we probably want to press Trash and wait for the page to navigate to the post list (maybe check the success notice as well), optionally adding a 'dialog' even to short-cut the test failure when encountered.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This works because refreshing a page with a dialog doesn't get rid of the dialog. So if navigation didn't happen because the dialog stopped it, assertIsDirty wouldn't assert false.

} );
} );
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
aduth marked this conversation as resolved.
Show resolved Hide resolved
// 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 );