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

Plugin: Fix saving new posts by making the post type mandatory #1395

Merged
merged 2 commits into from
Jun 23, 2017
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
13 changes: 9 additions & 4 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import { __ } from 'i18n';
*/
import { getGutenbergURL, getWPAdminURL } from './utils/url';
import { focusBlock, replaceBlocks } from './actions';
import { getCurrentPost, getCurrentPostId, getBlocks, getPostEdits } from './selectors';
import {
getCurrentPostId,
getCurrentPostType,
getBlocks,
getPostEdits,
} from './selectors';

export default {
REQUEST_POST_UPDATE( action, store ) {
Expand Down Expand Up @@ -43,7 +48,7 @@ export default {
edits: toSend,
optimist: { id: transactionId },
} );
const Model = wp.api.getPostTypeModel( getCurrentPost( state ).type );
const Model = wp.api.getPostTypeModel( getCurrentPostType( state ) );
new Model( toSend ).save().done( ( newPost ) => {
dispatch( {
type: 'REQUEST_POST_UPDATE_SUCCESS',
Expand Down Expand Up @@ -78,9 +83,9 @@ export default {
window.history.replaceState( {}, 'Post ' + post.id, newURL );
},
TRASH_POST( action, store ) {
const { dispatch } = store;
const { dispatch, getState } = store;
const { postId } = action;
const Model = wp.api.getPostTypeModel( getCurrentPost( store.getState() ).type );
const Model = wp.api.getPostTypeModel( getCurrentPostType( getState() ) );
new Model( { id: postId } ).destroy().done( () => {
dispatch( {
...action,
Expand Down
23 changes: 11 additions & 12 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,20 @@ if ( settings.timezone.string ) {
* Initializes Redux state with bootstrapped post, if provided.
*
* @param {Redux.Store} store Redux store instance
* @param {?Object} post Bootstrapped post object
* @param {Object} post Bootstrapped post object
*/
function preparePostState( store, post ) {
if ( ! post ) {
return;
}

store.dispatch( {
type: 'RESET_POST',
post,
} );
store.dispatch( {
type: 'RESET_BLOCKS',
blocks: parse( post.content.raw ),
} );

if ( post.content ) {
store.dispatch( {
type: 'RESET_BLOCKS',
blocks: parse( post.content.raw ),
} );
}

if ( ! post.id ) {
// Each property that is set in `post-content.js` (other than `content`
Expand All @@ -65,8 +64,8 @@ function preparePostState( store, post ) {
store.dispatch( {
type: 'SETUP_NEW_POST',
edits: {
title: post.title.raw,
...omit( post, 'title', 'content' ),
title: post.title ? post.title.raw : undefined,
...omit( post, 'title', 'content', 'type' ),
},
} );
}
Expand All @@ -76,7 +75,7 @@ function preparePostState( store, post ) {
* Initializes and returns an instance of Editor.
*
* @param {String} id Unique identifier for editor instance
* @param {Object} post API entity for post to edit
* @param {Object} post API entity for post to edit (type required)
*/
export function createEditorInstance( id, post ) {
const store = createReduxStore();
Expand Down
10 changes: 10 additions & 0 deletions editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,16 @@ export function getCurrentPost( state ) {
return state.currentPost;
}

/**
* Returns the post type of the post currently being edited
*
* @param {Object} state Global application state
* @return {String} Post type
*/
export function getCurrentPostType( state ) {
return state.currentPost.type;
}

/**
* Returns the ID of the post currently being edited, or null if the post has
* not yet been saved.
Expand Down
6 changes: 3 additions & 3 deletions editor/sidebar/last-revision/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import PanelBody from 'components/panel/body';
* Internal dependencies
*/
import './style.scss';
import { getCurrentPost, isSavingPost } from '../../selectors';
import { getCurrentPostId, getCurrentPostType, isSavingPost } from '../../selectors';
import { getWPAdminURL } from '../../utils/url';

class LastRevision extends Component {
Expand Down Expand Up @@ -112,8 +112,8 @@ class LastRevision extends Component {
export default connect(
( state ) => {
return {
postId: getCurrentPost( state ).id,
postType: getCurrentPost( state ).type,
postId: getCurrentPostId( state ),
postType: getCurrentPostType( state ),
isSaving: isSavingPost( state ),
};
}
Expand Down
7 changes: 3 additions & 4 deletions editor/sidebar/post-trash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Button, Dashicon } from 'components';
* Internal dependencies
*/
import './style.scss';
import { getCurrentPost } from '../../selectors';
import { getCurrentPostId, getCurrentPostType } from '../../selectors';
import { trashPost } from '../../actions';

function PostTrash( { postId, postType, ...props } ) {
Expand All @@ -33,10 +33,9 @@ function PostTrash( { postId, postType, ...props } ) {

export default connect(
( state ) => {
const post = getCurrentPost( state );
return {
postId: post.id,
postType: post.type,
postId: getCurrentPostId( state ),
postType: getCurrentPostType( state ),
};
},
{ trashPost }
Expand Down
13 changes: 13 additions & 0 deletions editor/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isEditedPostDirty,
getCurrentPost,
getCurrentPostId,
getCurrentPostType,
getPostEdits,
getEditedPostTitle,
getEditedPostExcerpt,
Expand Down Expand Up @@ -207,6 +208,18 @@ describe( 'selectors', () => {
} );
} );

describe( 'getCurrentPostType', () => {
it( 'should return the post type', () => {
const state = {
currentPost: {
type: 'post',
},
};

expect( getCurrentPostType( state ) ).to.equal( 'post' );
} );
} );

describe( 'getPostEdits', () => {
it( 'should return the post edits', () => {
const state = {
Expand Down
10 changes: 9 additions & 1 deletion lib/client-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,15 @@ function gutenberg_scripts_and_styles( $hook ) {
file_get_contents( gutenberg_dir_path() . 'post-content.js' )
);
} else {
// TODO: Error handling.
// ...with a new empty post
// TODO: Error handling if we tried and failed to get a post above
$empty_post = array(
'type' => 'post',
);
wp_add_inline_script(
'wp-editor',
'window._wpGutenbergPost = ' . wp_json_encode( $empty_post ) . ';'
);
Copy link
Member

@nylen nylen Jun 23, 2017

Choose a reason for hiding this comment

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

Error handling is still TODO here. Let's not remove that comment yet. Edit: Done in db4d9a1

}

// Prepare Jed locale data.
Expand Down
1 change: 1 addition & 0 deletions post-content.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Temporary test post content
*/
window._wpGutenbergPost = {
type: 'post',
title: {
raw: 'Welcome to the Gutenberg Editor',
},
Expand Down