Skip to content

Commit

Permalink
Send all fields when transmitting an autosave. (#7092)
Browse files Browse the repository at this point in the history
* Ensure autosaves contain all fields (title, content & excerpt), not just edited fields

* use a new data to send to autosave

* State: Account for autosave values in fallback spread

* Fix renamed selector
  • Loading branch information
Adam Silverstein authored and youknowriad committed Jun 4, 2018
1 parent 9a10190 commit f897235
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
14 changes: 11 additions & 3 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';
import { get, includes, last, map, castArray, uniqueId } from 'lodash';
import { get, includes, last, map, castArray, uniqueId, pick } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -63,6 +63,7 @@ import {
isBlockSelected,
getTemplate,
getTemplateLock,
getAutosave,
POST_UPDATE_TRANSACTION_ID,
} from './selectors';

Expand Down Expand Up @@ -105,7 +106,7 @@ export default {

const post = getCurrentPost( state );
const edits = getPostEdits( state );
const toSend = {
let toSend = {
...edits,
content: getEditedPostContent( state ),
id: post.id,
Expand All @@ -120,7 +121,14 @@ export default {

let request;
if ( isAutosave ) {
toSend.parent = post.id;
// Ensure autosaves contain all expected fields, using autosave or
// post values as fallback if not otherwise included in edits.
toSend = {
...pick( post, [ 'title', 'content', 'excerpt' ] ),
...getAutosave( state ),
...toSend,
parent: post.id,
};

request = wp.apiRequest( {
path: `/wp/v2/${ basePath }/${ post.id }/autosaves`,
Expand Down
18 changes: 16 additions & 2 deletions editor/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,25 @@ export function isEditedPostAutosaveable( state ) {
}

// If the title, excerpt or content has changed, the post is autosaveable.
const autosave = getAutosave( state );
return [ 'title', 'excerpt', 'content' ].some( ( field ) => (
state.autosave[ field ] !== getEditedPostAttribute( state, field )
autosave[ field ] !== getEditedPostAttribute( state, field )
) );
}

/**
* Returns the current autosave, or null if one is not set (i.e. if the post
* has yet to be autosaved, or has been saved or published since the last
* autosave).
*
* @param {Object} state Editor state.
*
* @return {?Object} Current autosave, if exists.
*/
export function getAutosave( state ) {
return state.autosave;
}

/**
* Returns the true if there is an existing autosave, otherwise false.
*
Expand All @@ -358,7 +372,7 @@ export function isEditedPostAutosaveable( state ) {
* @return {boolean} Whether there is an existing autosave.
*/
export function hasAutosave( state ) {
return !! state.autosave;
return !! getAutosave( state );
}

/**
Expand Down
45 changes: 45 additions & 0 deletions editor/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const {
isEditedPostPublishable,
isEditedPostSaveable,
isEditedPostAutosaveable,
getAutosave,
hasAutosave,
isEditedPostEmpty,
isEditedPostBeingScheduled,
getEditedPostPreviewLink,
Expand Down Expand Up @@ -1108,6 +1110,49 @@ describe( 'selectors', () => {
} );
} );

describe( 'getAutosave', () => {
it( 'returns null if there is no autosave', () => {
const state = {
autosave: null,
};

const result = getAutosave( state );

expect( result ).toBe( null );
} );

it( 'returns the autosave', () => {
const autosave = { title: '', excerpt: '', content: '' };
const state = { autosave };

const result = getAutosave( state );

expect( result ).toEqual( autosave );
} );
} );

describe( 'hasAutosave', () => {
it( 'returns false if there is no autosave', () => {
const state = {
autosave: null,
};

const result = hasAutosave( state );

expect( result ).toBe( false );
} );

it( 'returns true if there is a autosave', () => {
const state = {
autosave: { title: '', excerpt: '', content: '' },
};

const result = hasAutosave( state );

expect( result ).toBe( true );
} );
} );

describe( 'isEditedPostEmpty', () => {
it( 'should return true if no blocks and no content', () => {
const state = {
Expand Down

0 comments on commit f897235

Please sign in to comment.