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

Refactor to remove usage of post related effects in packages/editor. #13716

Merged
merged 32 commits into from
Feb 28, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
7e1d857
add controls for fetch, select, and dispatch
nerrad Feb 7, 2019
77a812c
move _all_ constants into the constants file for better organization
nerrad Feb 7, 2019
ae3f60b
establish file structure, begin refactoring post effects/actions
nerrad Feb 7, 2019
a673b8e
Implement trashPost action-generator
nerrad Feb 8, 2019
25c74c2
fix import jsdocs
nerrad Feb 8, 2019
54fbc38
update controls to use new createRegistryControl interface
nerrad Feb 8, 2019
455d6a8
add action generator for refreshing the current post
nerrad Feb 8, 2019
5d246e5
add tests for new code
nerrad Feb 11, 2019
0008288
fix broken tests
nerrad Feb 11, 2019
de8e6d1
don’t continue with refreshPost if there’s no current post yet
nerrad Feb 11, 2019
64216f4
remove unnecessary bail
nerrad Feb 11, 2019
f80fae4
add changelog entry
nerrad Feb 11, 2019
97b1ed5
fix lint failure for import order
nerrad Feb 12, 2019
9bce642
fix incorrect method used for autosave requests
nerrad Feb 12, 2019
36f962d
remove another wayward period.
nerrad Feb 12, 2019
d69599f
Add resolveSelect control and implement
nerrad Feb 19, 2019
4cba06a
refactor to condense code and update tests.
nerrad Feb 22, 2019
b3beb7f
update docs
nerrad Feb 22, 2019
651216d
fix doc blocks
nerrad Feb 27, 2019
a455781
fix jsdocs for param object properties
nerrad Feb 27, 2019
1a4fc0c
simplify import statement
nerrad Feb 27, 2019
49f5af6
utilize constants from constants file
nerrad Feb 27, 2019
a41039c
remove unused constants (now in block-editor)
nerrad Feb 27, 2019
2c14df4
simplify import
nerrad Feb 27, 2019
f66af80
add doc blocks for control actions
nerrad Feb 27, 2019
5568ac3
update docs
nerrad Feb 27, 2019
6d74643
make sure test is importing used constant
nerrad Feb 27, 2019
ff70f2b
fix doc block spacing issues
nerrad Feb 28, 2019
1c11a5b
Swith constant name from `MODULE_KEY` to `STORE_KEY`
nerrad Feb 28, 2019
4cc4015
remove use of generatorActions default import ans switch to dispatch …
nerrad Feb 28, 2019
0ba7d0e
doc changes
nerrad Feb 28, 2019
c17179e
Fix docs spacing
aduth Feb 28, 2019
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
Prev Previous commit
Next Next commit
establish file structure, begin refactoring post effects/actions
  • Loading branch information
nerrad committed Feb 28, 2019
commit ae3f60b5899165b8907915410616464202b4b5d9
113 changes: 113 additions & 0 deletions packages/editor/src/store/actions/post-actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
/**
* External dependencies
*/
import { BEGIN, COMMIT, REVERT } from 'redux-optimist';

/**
* Internal dependencies
*/
import { POST_UPDATE_TRANSACTION_ID } from '../constants';

/**
* Optimistic action for requesting post update start.
*
* @param {Object} options
* @return {Object} An action object
*/
export function requestPostUpdateStart( options = {} ) {
return {
type: 'REQUEST_POST_UPDATE_START',
optimist: { type: BEGIN, id: POST_UPDATE_TRANSACTION_ID },
options,
};
}

/**
* Optimistic action for requesting post update success.
*
* @param {Object} previousPost The previous post prior to update.
* @param {Object} post The new post after update
* @param {boolean} isRevision Whether the post is a revision or not.
* @param {Object} options Options passed through from the original action
* dispatch.
* @param {Object} postType The post type object.
* @return {Object} Action object.
*/
export function requestPostUpdateSuccess( {
previousPost,
post,
isRevision,
options,
postType,
} ) {
return {
type: 'REQUEST_POST_UPDATE_SUCCESS',
previousPost,
post,
optimist: {
// Note: REVERT is not a failure case here. Rather, it
// is simply reversing the assumption that the updates
// were applied to the post proper, such that the post
// treated as having unsaved changes.
type: isRevision ? REVERT : COMMIT,
id: POST_UPDATE_TRANSACTION_ID,
},
options,
postType,
};
}

/**
* Optimistic action for requesting post update failure.
*
* @param {Object} post The post that failed updating.
* @param {Object} edits The fields that were being updated.
* @param {*} error The error from the failed call.
* @param {Object} options Options passed through from the original action
* dispatch.
* @return {Object} An action object
*/
export function requestPostUpdateFailure( {
post,
edits,
error,
options,
} ) {
return {
type: 'REQUEST_POST_UPDATE_FAILURE',
optimist: { type: REVERT, id: POST_UPDATE_TRANSACTION_ID },
post,
edits,
error,
options,
};
}

/**
* Returns an action object used in signalling that a patch of updates for the
* latest version of the post have been received.
*
* @param {Object} edits Updated post fields.
*
* @return {Object} Action object.
*/
export function updatePost( edits ) {
return {
type: 'UPDATE_POST',
edits,
};
}

/**
* Returns action object produced by the updatePost creator augmented by
* an optimist option that signals optimistically applying updates.
*
* @param {Object} edits Updated post fields.
* @return {Object} Action object.
*/
export function optimisticUpdatePost( edits ) {
return {
...updatePost( edits ),
optimist: { id: POST_UPDATE_TRANSACTION_ID },
};
}
184 changes: 184 additions & 0 deletions packages/editor/src/store/actions/post-generators.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
/**
* External dependencies
*/
import { pick } from 'lodash';

/**
* Internal dependencies
*/
import { select, dispatch, apiFetch } from '../controls';
import { MODULE_KEY, SAVE_POST_NOTICE_ID } from '../constants';
import {
getNotifyOnSuccessNotificationArguments,
getNotifyOnFailNotificationArguments,
} from './utils/notice-builder';

/**
* Action generator for saving a post.
*
* @param {Object} options
*/
export function* savePost( options = {} ) {
const isEditedPostSaveable = yield select(
MODULE_KEY,
'isEditedPostSaveable'
);
if ( ! isEditedPostSaveable ) {
return;
}
let edits = yield select(
MODULE_KEY,
'getPostEdits'
);
const isAutosave = !! options.isAutosave;

if ( isAutosave ) {
edits = pick( edits, [ 'title', 'content', 'excerpt' ] );
}

const isEditedPostNew = yield select(
MODULE_KEY,
'isEditedPostNew',
);

// New posts (with auto-draft status) must be explicitly assigned draft
// status if there is not already a status assigned in edits (publish).
// Otherwise, they are wrongly left as auto-draft. Status is not always
// respected for autosaves, so it cannot simply be included in the pick
// above. This behavior relies on an assumption that an auto-draft post
// would never be saved by anyone other than the owner of the post, per
// logic within autosaves REST controller to save status field only for
// draft/auto-draft by current user.
//
// See: https://core.trac.wordpress.org/ticket/43316#comment:88
// See: https://core.trac.wordpress.org/ticket/43316#comment:89
if ( isEditedPostNew ) {
edits = { status: 'draft', ...edits };
}

const post = yield select(
MODULE_KEY,
'getCurrentPost'
);

const editedPostContent = yield select(
MODULE_KEY,
'getEditedPostContent'
);

let toSend = {
...edits,
content: editedPostContent,
id: post.id,
};

const currentPostType = yield select(
MODULE_KEY,
'getCurrentPostType'
);

const postType = yield select(
'core',
'getPostType',
currentPostType
);

yield dispatch(
MODULE_KEY,
'requestPostUpdateStart',
options,
);

// Optimistically apply updates under the assumption that the post
// will be updated. See below logic in success resolution for revert
// if the autosave is applied as a revision.
yield dispatch(
MODULE_KEY,
'optimisticUpdatePost',
toSend
);

let path = `/wp/v2/${ postType.rest_base }/${ post.id }`;
if ( isAutosave ) {
const autoSavePost = yield select(
MODULE_KEY,
'getAutosave',
);
// 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' ] ),
...autoSavePost,
...toSend,
};
path += '/autosaves';
} else {
yield dispatch(
'core/notices',
'removeNotice',
SAVE_POST_NOTICE_ID,
);
yield dispatch(
'core/notices',
'removeNotice',
'autosave-exists',
);
}

try {
const newPost = yield apiFetch( {
path,
method: 'PUT',
data: toSend,
} );
const resetAction = isAutosave ? 'resetAutosave' : 'resetPost';

yield dispatch( MODULE_KEY, resetAction, newPost );

yield dispatch(
MODULE_KEY,
'requestPostUpdateSuccess',
{
previousPost: post,
post: newPost,
options,
postType,
// An autosave may be processed by the server as a regular save
// when its update is requested by the author and the post was
// draft or auto-draft.
isRevision: newPost.id !== post.id,
}
);

const notifySuccessArgs = getNotifyOnSuccessNotificationArguments( {
previousPost: post,
post: newPost,
postType,
} );
if ( notifySuccessArgs.length > 0 ) {
yield dispatch(
'core/notices',
'createSuccessNotice',
...notifySuccessArgs
);
}
} catch ( error ) {
yield dispatch(
MODULE_KEY,
'requestPostUpdateFailure',
{ post, edits, error, options }
);
const notifyFailArgs = getNotifyOnFailNotificationArguments( {
post,
edits,
error,
} );
if ( notifyFailArgs.length > 0 ) {
yield dispatch(
'core/notices',
'createErrorNotice',
...notifyFailArgs
);
}
}
}
Loading