Skip to content

Commit

Permalink
Don't show an error if autosave runs and there are no changes to save (
Browse files Browse the repository at this point in the history
  • Loading branch information
notnownikki authored Jun 18, 2018
1 parent a292eb5 commit 55b7706
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
9 changes: 8 additions & 1 deletion editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,14 @@ export default {
}
},
REQUEST_POST_UPDATE_FAILURE( action, store ) {
const { post, edits } = action;
const { post, edits, error } = action;

if ( error && 'rest_autosave_no_changes' === error.code ) {
// Autosave requested a new autosave, but there were no changes. This shouldn't
// result in an error notice for the user.
return;
}

const { dispatch } = store;

const publishStatus = [ 'publish', 'private', 'future' ];
Expand Down
29 changes: 29 additions & 0 deletions editor/store/test/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,35 @@ describe( 'effects', () => {
expect( dispatch ).toHaveBeenCalledWith( createErrorNotice( 'Publishing failed', { id: 'SAVE_POST_NOTICE_ID' } ) );
} );

it( 'should not dispatch a notice when there were no changes for autosave to save.', () => {
const handler = effects.REQUEST_POST_UPDATE_FAILURE;
const dispatch = jest.fn();
const store = { getState: () => {}, dispatch };

const action = {
post: {
id: 1,
title: {
raw: 'A History of Pork',
},
content: {
raw: '',
},
status: 'draft',
},
edits: {
status: 'publish',
},
error: {
code: 'rest_autosave_no_changes',
},
};

handler( action, store );

expect( dispatch ).toHaveBeenCalledTimes( 0 );
} );

it( 'should dispatch a notice on failure when trying to update a draft.', () => {
const handler = effects.REQUEST_POST_UPDATE_FAILURE;
const dispatch = jest.fn();
Expand Down
2 changes: 1 addition & 1 deletion lib/class-wp-rest-autosaves-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ public function create_post_autosave( $post_data ) {

if ( ! $autosave_is_different ) {
wp_delete_post_revision( $old_autosave->ID );
return new WP_Error( 'rest_autosave_no_changes', __( 'There is nothing to save. The autosave and the post content are the same.', 'gutenberg' ) );
return new WP_Error( 'rest_autosave_no_changes', __( 'There is nothing to save. The autosave and the post content are the same.', 'gutenberg' ), array( 'status' => 400 ) );
}

/**
Expand Down

0 comments on commit 55b7706

Please sign in to comment.