Skip to content

Commit

Permalink
Trying a new approach
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Dec 22, 2016
1 parent 4c6278d commit bcfc667
Show file tree
Hide file tree
Showing 12 changed files with 158 additions and 96 deletions.
15 changes: 11 additions & 4 deletions client/post-editor/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ var actions = require( 'lib/posts/actions' ),
userUtils = require( 'lib/user/utils' ),
analytics = require( 'lib/analytics' );
import PostEditor from './post-editor';
import { startPostEditor } from 'state/ui/editor/actions';
import { startEditingPost, stopEditingPost } from 'state/ui/editor/actions';
import { getSelectedSiteId } from 'state/ui/selectors';
import { getEditorPostId, getEditorPath } from 'state/ui/editor/selectors';
import { editPost } from 'state/posts/actions';

function getPostID( context ) {
if ( ! context.params.post || 'new' === context.params.post ) {
Expand Down Expand Up @@ -114,8 +113,7 @@ module.exports = {
const postID = getPostID( context );

function startEditing( siteId ) {
context.store.dispatch( startPostEditor( siteId, postID ) );
context.store.dispatch( editPost( siteId, postID, { type: postType } ) );
context.store.dispatch( startEditingPost( siteId, postID, postType ) );

if ( maybeRedirect( context ) ) {
return;
Expand Down Expand Up @@ -177,6 +175,15 @@ module.exports = {
renderEditor( context, postType );
},

exitPost: function( context, next ) {
const postId = getPostID( context );
const siteId = getSelectedSiteId( context.store.getState() );
if ( siteId ) {
context.store.dispatch( stopEditingPost( siteId, postId ) );
}
next();
},

pressThis: function( context, next ) {
context.getSiteSelectionHeaderText = function() {
return i18n.translate( 'Select a site to start writing' );
Expand Down
3 changes: 3 additions & 0 deletions client/post-editor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ module.exports = function() {
page( '/post', controller.pressThis, sitesController.siteSelection, sitesController.sites );
page( '/post/new', () => page.redirect( '/post' ) ); // redirect from beep-beep-boop
page( '/post/:site?/:post?', sitesController.siteSelection, sitesController.fetchJetpackSettings, controller.post );
page.exit( '/post/:site?/:post?', controller.exitPost );

page( '/page', sitesController.siteSelection, sitesController.sites );
page( '/page/new', () => page.redirect( '/page' ) ); // redirect from beep-beep-boop
page( '/page/:site?/:post?', sitesController.siteSelection, sitesController.fetchJetpackSettings, controller.post );
page.exit( '/page/:site?/:post?', controller.exitPost );

if ( config.isEnabled( 'manage/custom-post-types' ) ) {
page( '/edit/:type', sitesController.siteSelection, sitesController.sites );
page( '/edit/:type/new', ( context ) => page.redirect( `/edit/${ context.params.type }` ) );
page( '/edit/:type/:site?/:post?', sitesController.siteSelection, sitesController.fetchJetpackSettings, controller.post );
page.exit( '/edit/:type/:site?/:post?', controller.exitPost );
}
};
21 changes: 5 additions & 16 deletions client/post-editor/post-editor.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ import AsyncLoad from 'components/async-load';
import { getSelectedSiteId } from 'state/ui/selectors';
import { setEditorLastDraft, resetEditorLastDraft } from 'state/ui/editor/last-draft/actions';
import { isEditorDraftsVisible, getEditorPostId, getEditorPath } from 'state/ui/editor/selectors';
import { toggleEditorDraftsVisible, setEditorPostId } from 'state/ui/editor/actions';
import { receivePost, resetPostEdits } from 'state/posts/actions';
import { toggleEditorDraftsVisible } from 'state/ui/editor/actions';
import { receivePost, savePostSuccess } from 'state/posts/actions';
import { getPostEdits, isEditedPostDirty } from 'state/posts/selectors';
import EditorSidebarHeader from 'post-editor/editor-sidebar/header';
import EditorDocumentHead from 'post-editor/editor-document-head';
Expand Down Expand Up @@ -140,9 +140,6 @@ export const PostEditor = React.createClass( {
componentWillUnmount: function() {
PostEditStore.removeListener( 'change', this.onEditedPostChange );

// Reset post edits after leaving editor
this.props.resetPostEdits( this.props.siteId, this.props.postId );

// TODO: REDUX - remove flux actions when whole post-editor is reduxified
actions.stopEditing();

Expand Down Expand Up @@ -695,19 +692,12 @@ export const PostEditor = React.createClass( {
this.props.resetEditorLastDraft();
}

// Assign editor post ID to saved value (especially important when
// transitioning from an unsaved post to a saved one)
if ( post.ID !== this.props.postId ) {
this.props.setEditorPostId( post.ID );
}
// Remove this when the editor is completely reduxified ( When using Redux actions for all post saving requests )
this.props.savePostSuccess( post.site_ID, this.props.postId, post, {} );

// Receive updated post into state
this.props.receivePost( post );

// Reset previous edits, preserving type
this.props.resetPostEdits( this.props.siteId );
this.props.resetPostEdits( post.site_ID, post.ID );

const nextState = {
isSaving: false,
isPublishing: false
Expand Down Expand Up @@ -790,8 +780,7 @@ export default connect(
setEditorLastDraft,
resetEditorLastDraft,
receivePost,
resetPostEdits,
setEditorPostId,
savePostSuccess,
setEditorModePreference: savePreference.bind( null, 'editor-mode' ),
setLayoutFocus,
}, dispatch );
Expand Down
3 changes: 1 addition & 2 deletions client/state/action-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ export const EDITOR_CONTACT_FORM_LOAD = 'EDITOR_CONTACT_FORM_LOAD';
export const EDITOR_CONTACT_FORM_SETTINGS_UPDATE = 'EDITOR_CONTACT_FORM_SETTINGS_UPDATE';
export const EDITOR_LAST_DRAFT_SET = 'EDITOR_LAST_DRAFT_SET';
export const EDITOR_MEDIA_EDIT_ITEM_SET = 'EDITOR_MEDIA_EDIT_ITEM_SET';
export const EDITOR_POST_ID_SET = 'EDITOR_POST_ID_SET';
export const EDITOR_SHOW_DRAFTS_TOGGLE = 'EDITOR_SHOW_DRAFTS_TOGGLE';
export const EDITOR_START = 'EDITOR_START';
export const EDITOR_STOP = 'EDITOR_STOP';
export const EXPORT_ADVANCED_SETTINGS_FETCH = 'EXPORT_ADVANCED_SETTINGS_FETCH';
export const EXPORT_ADVANCED_SETTINGS_FETCH_FAIL = 'EXPORT_ADVANCED_SETTINGS_FETCH_FAIL';
export const EXPORT_ADVANCED_SETTINGS_RECEIVE = 'EXPORT_ADVANCED_SETTINGS_RECEIVE';
Expand Down Expand Up @@ -291,7 +291,6 @@ export const POST_DELETE = 'POST_DELETE';
export const POST_DELETE_FAILURE = 'POST_DELETE_FAILURE';
export const POST_DELETE_SUCCESS = 'POST_DELETE_SUCCESS';
export const POST_EDIT = 'POST_EDIT';
export const POST_EDITS_RESET = 'POST_EDITS_RESET';
export const POST_FORMATS_RECEIVE = 'POST_FORMATS_RECEIVE';
export const POST_FORMATS_REQUEST = 'POST_FORMATS_REQUEST';
export const POST_FORMATS_REQUEST_FAILURE = 'POST_FORMATS_REQUEST_FAILURE';
Expand Down
28 changes: 12 additions & 16 deletions client/state/posts/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
POST_DELETE_SUCCESS,
POST_DELETE_FAILURE,
POST_EDIT,
POST_EDITS_RESET,
POST_REQUEST,
POST_REQUEST_SUCCESS,
POST_REQUEST_FAILURE,
Expand Down Expand Up @@ -164,18 +163,21 @@ export function editPost( siteId, postId = null, post ) {
}

/**
* Returns an action object to be used in signalling that any post edits for
* the specified post should be discarded.
* Returns an action object to be used in signalling that a post has been saved
*
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @return {Object} Action object
* @param {Number} siteId Site ID
* @param {Number} postId Post ID
* @param {Object} savedPost Updated post
* @param {Object} post Post attributes
* @return {Object} Action thunk
*/
export function resetPostEdits( siteId, postId ) {
export function savePostSuccess( siteId, postId = null, savedPost, post ) {
return {
type: POST_EDITS_RESET,
type: POST_SAVE_SUCCESS,
siteId,
postId
postId,
savedPost,
post
};
}

Expand All @@ -201,13 +203,7 @@ export function savePost( siteId, postId = null, post ) {
const normalizedPost = normalizePostForApi( post );
postHandle = postHandle[ postId ? 'update' : 'add' ].bind( postHandle );
return postHandle( { apiVersion: '1.2' }, normalizedPost ).then( ( savedPost ) => {
dispatch( {
type: POST_SAVE_SUCCESS,
siteId,
postId,
savedPost,
post
} );
dispatch( savePostSuccess( siteId, postId, savedPost, post ) );
dispatch( receivePost( savedPost ) );
} ).catch( ( error ) => {
dispatch( {
Expand Down
13 changes: 11 additions & 2 deletions client/state/posts/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import { get, set, omit, omitBy, isEqual, reduce, merge, findKey, mapValues } fr
import PostQueryManager from 'lib/query-manager/post';
import {
EDITOR_START,
EDITOR_STOP,
POST_DELETE,
POST_DELETE_SUCCESS,
POST_DELETE_FAILURE,
POST_EDIT,
POST_EDITS_RESET,
POST_REQUEST,
POST_REQUEST_SUCCESS,
POST_REQUEST_FAILURE,
POST_RESTORE,
POST_RESTORE_FAILURE,
POST_SAVE,
POST_SAVE_SUCCESS,
POSTS_RECEIVE,
POSTS_REQUEST,
POSTS_REQUEST_SUCCESS,
Expand Down Expand Up @@ -262,8 +263,16 @@ export function edits( state = {}, action ) {
}
} );

case POST_EDITS_RESET:
case EDITOR_START:
return Object.assign( {}, state, {
[ action.siteId ]: {
...state[ action.siteId ],
[ action.postId || '' ]: { type: action.postType }
}
} );

case EDITOR_STOP:
case POST_SAVE_SUCCESS:
if ( ! state.hasOwnProperty( action.siteId ) ) {
break;
}
Expand Down
31 changes: 17 additions & 14 deletions client/state/posts/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
POST_DELETE_SUCCESS,
POST_DELETE_FAILURE,
POST_EDIT,
POST_EDITS_RESET,
POST_REQUEST,
POST_REQUEST_SUCCESS,
POST_REQUEST_FAILURE,
Expand All @@ -35,8 +34,8 @@ import {
requestSitePost,
requestPosts,
editPost,
resetPostEdits,
savePost,
savePostSuccess,
trashPost,
deletePost,
restorePost,
Expand Down Expand Up @@ -75,6 +74,22 @@ describe( 'actions', () => {
} );
} );

describe( 'savePostSuccess()', () => {
it( 'should return an action object', () => {
const savedPost = { ID: 841, title: 'Hello World' };
const attributes = { status: 'draft' };
const action = savePostSuccess( 10, 841, savedPost, attributes );

expect( action ).to.eql( {
type: POST_SAVE_SUCCESS,
siteId: 10,
postId: 841,
savedPost: savedPost,
post: attributes,
} );
} );
} );

describe( '#requestSitePosts()', () => {
useNock( ( nock ) => {
nock( 'https://public-api.wordpress.com:443' )
Expand Down Expand Up @@ -274,18 +289,6 @@ describe( 'actions', () => {
} );
} );

describe( '#resetPostEdits()', () => {
it( 'should return an action object', () => {
const action = resetPostEdits( 2916284 );

expect( action ).to.eql( {
type: POST_EDITS_RESET,
siteId: 2916284,
postId: undefined
} );
} );
} );

describe( 'savePost()', () => {
useNock( ( nock ) => {
nock( 'https://public-api.wordpress.com:443' )
Expand Down
46 changes: 38 additions & 8 deletions client/state/posts/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,18 @@ import deepFreeze from 'deep-freeze';
import { useSandbox } from 'test/helpers/use-sinon';
import {
EDITOR_START,
EDITOR_STOP,
POST_DELETE,
POST_DELETE_SUCCESS,
POST_DELETE_FAILURE,
POST_EDIT,
POST_EDITS_RESET,
POST_REQUEST,
POST_REQUEST_SUCCESS,
POST_REQUEST_FAILURE,
POST_RESTORE,
POST_RESTORE_FAILURE,
POST_SAVE,
POST_SAVE_SUCCESS,
POSTS_RECEIVE,
POSTS_REQUEST,
POSTS_REQUEST_FAILURE,
Expand Down Expand Up @@ -893,15 +894,15 @@ describe( 'reducer', () => {
it( 'should ignore reset edits action when discarded site doesn\'t exist', () => {
const original = deepFreeze( {} );
const state = edits( original, {
type: POST_EDITS_RESET,
type: POST_SAVE_SUCCESS,
siteId: 2916284,
postId: 841
} );

expect( state ).to.equal( original );
} );

it( 'should discard edits when reset edits action dispatched', () => {
it( 'should discard edits when the post is saved', () => {
const state = edits( deepFreeze( {
2916284: {
841: {
Expand All @@ -912,7 +913,7 @@ describe( 'reducer', () => {
}
}
} ), {
type: POST_EDITS_RESET,
type: POST_SAVE_SUCCESS,
siteId: 2916284,
postId: 841
} );
Expand All @@ -926,18 +927,18 @@ describe( 'reducer', () => {
} );
} );

it( 'should ignore start editor action when site doesn\'t exist', () => {
it( 'should ignore stop editor action when site doesn\'t exist', () => {
const original = deepFreeze( {} );
const state = edits( original, {
type: EDITOR_START,
type: EDITOR_STOP,
siteId: 2916284,
postId: 841
} );

expect( state ).to.equal( original );
} );

it( 'should discard edits when start editor action is dispatched', () => {
it( 'should discard edits when we stop editing the post', () => {
const state = edits( deepFreeze( {
2916284: {
841: {
Expand All @@ -948,7 +949,7 @@ describe( 'reducer', () => {
}
}
} ), {
type: EDITOR_START,
type: EDITOR_STOP,
siteId: 2916284,
postId: 841
} );
Expand All @@ -962,6 +963,35 @@ describe( 'reducer', () => {
} );
} );

it( 'should reset edits when we start editing a post', () => {
const state = edits( deepFreeze( {
2916284: {
841: {
title: 'Hello World'
},
'': {
title: 'Ribs & Chicken'
}
}
} ), {
type: EDITOR_START,
siteId: 2916284,
postId: 841,
postType: 'jetpack-testimonial',
} );

expect( state ).to.eql( {
2916284: {
841: {
type: 'jetpack-testimonial'
},
'': {
title: 'Ribs & Chicken'
}
}
} );
} );

it( 'should not persist state', () => {
const state = edits( deepFreeze( {
2916284: {
Expand Down
Loading

0 comments on commit bcfc667

Please sign in to comment.