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

Initialize new post with auto-draft #1967

Merged
merged 2 commits into from
Jul 24, 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
23 changes: 10 additions & 13 deletions editor/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,14 @@ export default {
const { dispatch, getState } = store;
const state = getState();
const post = getCurrentPost( state );
const isNew = ! post.id;
const edits = getPostEdits( state );
const toSend = {
...edits,
content: serialize( getBlocks( state ) ),
id: post.id,
};
const transactionId = uniqueId();

if ( ! isNew ) {
toSend.id = post.id;
}

dispatch( {
type: 'CLEAR_POST_EDITS',
optimist: { type: BEGIN, id: transactionId },
Expand All @@ -58,7 +54,6 @@ export default {
type: 'REQUEST_POST_UPDATE_SUCCESS',
previousPost: post,
post: newPost,
isNew,
optimist: { type: COMMIT, id: transactionId },
} );
} ).fail( ( err ) => {
Expand All @@ -75,7 +70,7 @@ export default {
} );
},
REQUEST_POST_UPDATE_SUCCESS( action, store ) {
const { previousPost, post, isNew } = action;
const { previousPost, post } = action;
const { dispatch } = store;

const publishStatus = [ 'publish', 'private', 'future' ];
Expand All @@ -102,13 +97,15 @@ export default {
) );
}

if ( ! isNew ) {
return;
if ( get( window.history.state, 'id' ) !== post.id ) {
window.history.replaceState(
{ id: post.id },
'Post ' + post.id,
getGutenbergURL( {
post_id: post.id,
} )
);
}
const newURL = getGutenbergURL( {
post_id: post.id,
} );
window.history.replaceState( {}, 'Post ' + post.id, newURL );
},
REQUEST_POST_UPDATE_FAILURE( action, store ) {
const { post, edits } = action;
Expand Down
5 changes: 4 additions & 1 deletion editor/header/saved-state/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ export function SavedState( { isNew, isDirty, isSaving, isSaveable, status, onSt
}

const onClick = () => {
onStatusChange( status || 'draft' );
if ( 'auto-draft' === status ) {
onStatusChange( 'draft' );
}

onSave();
};

Expand Down
23 changes: 22 additions & 1 deletion editor/header/saved-state/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,27 @@ describe( 'SavedState', () => {
expect( wrapper.childAt( 1 ).text() ).toBe( 'Saved' );
} );

it( 'should edit auto-draft post to draft before save', () => {
const statusSpy = jest.fn();
const saveSpy = jest.fn();
const wrapper = shallow(
<SavedState
isNew={ false }
isDirty={ true }
isSaving={ false }
isSaveable={ true }
onStatusChange={ statusSpy }
onSave={ saveSpy }
status="auto-draft" />
);

expect( wrapper.name() ).toBe( 'Button' );
expect( wrapper.childAt( 0 ).text() ).toBe( 'Save' );
wrapper.simulate( 'click' );
expect( statusSpy ).toHaveBeenCalledWith( 'draft' );
expect( saveSpy ).toHaveBeenCalled();
} );

it( 'should return Save button if edits to be saved', () => {
const statusSpy = jest.fn();
const saveSpy = jest.fn();
Expand All @@ -62,7 +83,7 @@ describe( 'SavedState', () => {
expect( wrapper.name() ).toBe( 'Button' );
expect( wrapper.childAt( 0 ).text() ).toBe( 'Save' );
wrapper.simulate( 'click' );
expect( statusSpy ).toHaveBeenCalledWith( 'draft' );
expect( statusSpy ).not.toHaveBeenCalled();
expect( saveSpy ).toHaveBeenCalled();
} );
} );
15 changes: 6 additions & 9 deletions editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import { Provider as ReduxProvider } from 'react-redux';
import { Provider as SlotFillProvider } from 'react-slot-fill';
import { omit } from 'lodash';
import moment from 'moment-timezone';
import 'moment-timezone/moment-timezone-utils';

Expand Down Expand Up @@ -44,28 +43,26 @@ if ( settings.timezone.string ) {
* @param {Object} post Bootstrapped post object
*/
function preparePostState( store, post ) {
// Set current post into state
store.dispatch( {
type: 'RESET_POST',
post,
} );

if ( post.content ) {
// Parse content as blocks
if ( post.content.raw ) {
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`
// because it is serialized when a save is requested) needs to be
// registered as an edit now. Otherwise the initial values of these
// properties will not be properly saved with the post.
// Include auto draft title in edits while not flagging post as dirty
if ( post.status === 'auto-draft' ) {
store.dispatch( {
type: 'SETUP_NEW_POST',
edits: {
title: post.title ? post.title.raw : undefined,
...omit( post, 'title', 'content', 'type' ),
title: post.title.raw,
},
} );
}
Expand Down
7 changes: 4 additions & 3 deletions editor/post-permalink/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Dashicon, ClipboardButton, Button } from 'components';
* Internal Dependencies
*/
import './style.scss';
import { getEditedPostAttribute } from '../selectors';
import { isEditedPostNew, getEditedPostAttribute } from '../selectors';

class PostPermalink extends Component {
constructor() {
Expand Down Expand Up @@ -43,8 +43,8 @@ class PostPermalink extends Component {
}

render() {
const { link } = this.props;
if ( ! link ) {
const { isNew, link } = this.props;
if ( isNew || ! link ) {
return null;
}

Expand All @@ -66,6 +66,7 @@ class PostPermalink extends Component {
export default connect(
( state ) => {
return {
isNew: isEditedPostNew( state ),
link: getEditedPostAttribute( state, 'link' ),
};
}
Expand Down
2 changes: 1 addition & 1 deletion editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export function hasEditorRedo( state ) {
* @return {Boolean} Whether the post is new
*/
export function isEditedPostNew( state ) {
return ! getCurrentPostId( state );
return getEditedPostAttribute( state, 'status' ) === 'auto-draft';
}

/**
Expand Down
20 changes: 13 additions & 7 deletions editor/sidebar/last-revision/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,12 @@ import PanelBody from 'components/panel/body';
* Internal dependencies
*/
import './style.scss';
import { getCurrentPostId, getCurrentPostType, isSavingPost } from '../../selectors';
import {
isEditedPostNew,
getCurrentPostId,
getCurrentPostType,
isSavingPost,
} from '../../selectors';
import { getWPAdminURL } from '../../utils/url';

class LastRevision extends Component {
Expand Down Expand Up @@ -51,19 +56,19 @@ class LastRevision extends Component {
}

fetchRevisions() {
if ( ! this.props.postId ) {
const { isNew, postId, postType } = this.props;
if ( isNew || ! postId ) {
this.setState( { loading: false } );
return;
}
this.setState( { loading: true } );
const postIdToLoad = this.props.postId;
const Collection = wp.api.getPostTypeRevisionsCollection( this.props.postType );
const Collection = wp.api.getPostTypeRevisionsCollection( postType );
if ( ! Collection ) {
return;
}
this.fetchRevisionsRequest = new Collection( {}, { parent: postIdToLoad } ).fetch()
this.fetchRevisionsRequest = new Collection( {}, { parent: postId } ).fetch()
.done( ( revisions ) => {
if ( this.props.postId !== postIdToLoad ) {
if ( this.props.postId !== postId ) {
return;
}
this.setState( {
Expand All @@ -72,7 +77,7 @@ class LastRevision extends Component {
} );
} )
.fail( () => {
if ( this.props.postId !== postIdToLoad ) {
if ( this.props.postId !== postId ) {
return;
}
this.setState( {
Expand Down Expand Up @@ -112,6 +117,7 @@ class LastRevision extends Component {
export default connect(
( state ) => {
return {
isNew: isEditedPostNew( state ),
postId: getCurrentPostId( state ),
postType: getCurrentPostType( state ),
isSaving: isSavingPost( state ),
Expand Down
11 changes: 8 additions & 3 deletions editor/sidebar/post-trash/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,15 @@ import { Button, Dashicon } from 'components';
* Internal dependencies
*/
import './style.scss';
import { getCurrentPostId, getCurrentPostType } from '../../selectors';
import {
isEditedPostNew,
getCurrentPostId,
getCurrentPostType,
} from '../../selectors';
import { trashPost } from '../../actions';

function PostTrash( { postId, postType, ...props } ) {
if ( ! postId ) {
function PostTrash( { isNew, postId, postType, ...props } ) {
if ( isNew || ! postId ) {
return null;
}

Expand All @@ -34,6 +38,7 @@ function PostTrash( { postId, postType, ...props } ) {
export default connect(
( state ) => {
return {
isNew: isEditedPostNew( state ),
postId: getCurrentPostId( state ),
postType: getCurrentPostType( state ),
};
Expand Down
37 changes: 31 additions & 6 deletions editor/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,16 +148,24 @@ describe( 'selectors', () => {
describe( 'isEditedPostNew', () => {
it( 'should return true when the post is new', () => {
const state = {
currentPost: {},
currentPost: {
status: 'auto-draft',
},
editor: {
edits: {},
},
};

expect( isEditedPostNew( state ) ).toBe( true );
} );

it( 'should return false when the post has an ID', () => {
it( 'should return false when the post is not new', () => {
const state = {
currentPost: {
id: 1,
status: 'draft',
},
editor: {
edits: {},
},
};

Expand Down Expand Up @@ -193,8 +201,12 @@ describe( 'selectors', () => {
const state = {
editor: {
dirty: false,
edits: {},
},
currentPost: {
id: 1,
status: 'auto-draft',
},
currentPost: {},
};

expect( isCleanNewPost( state ) ).toBe( true );
Expand All @@ -204,8 +216,12 @@ describe( 'selectors', () => {
const state = {
editor: {
dirty: false,
edits: {},
},
currentPost: {
id: 1,
status: 'draft',
},
currentPost: { id: 1 },
};

expect( isCleanNewPost( state ) ).toBe( false );
Expand All @@ -215,7 +231,11 @@ describe( 'selectors', () => {
const state = {
editor: {
dirty: true,
currentPost: {},
edits: {},
},
currentPost: {
id: 1,
status: 'auto-draft',
},
};

Expand Down Expand Up @@ -337,6 +357,8 @@ describe( 'selectors', () => {
it( 'should return new post title when new post is clean', () => {
const state = {
currentPost: {
id: 1,
status: 'auto-draft',
title: { raw: '' },
},
editor: {
Expand All @@ -351,6 +373,8 @@ describe( 'selectors', () => {
it( 'should return untitled title when new post is dirty', () => {
const state = {
currentPost: {
id: 1,
status: 'auto-draft',
title: { raw: '' },
},
editor: {
Expand All @@ -366,6 +390,7 @@ describe( 'selectors', () => {
const state = {
currentPost: {
id: 123,
status: 'draft',
title: { raw: '' },
},
editor: {
Expand Down
Loading