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

Send all fields when transmitting an autosave. #7092

Merged
merged 5 commits into from
Jun 4, 2018
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
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 @@ -62,6 +62,7 @@ import {
isBlockSelected,
getTemplate,
getTemplateLock,
getAutosave,
POST_UPDATE_TRANSACTION_ID,
} from './selectors';

Expand Down Expand Up @@ -103,7 +104,7 @@ export default {

const post = getCurrentPost( state );
const edits = getPostEdits( state );
const toSend = {
let toSend = {
...edits,
content: getEditedPostContent( state ),
id: post.id,
Expand All @@ -118,7 +119,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