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 3 commits
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
82 changes: 39 additions & 43 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
apiFetch,
} from './controls';
import {
MODULE_KEY,
STORE_KEY,
POST_UPDATE_TRANSACTION_ID,
SAVE_POST_NOTICE_ID,
TRASH_POST_NOTICE_ID,
Expand Down Expand Up @@ -92,15 +92,16 @@ export function __experimentalRequestPostUpdateStart( options = {} ) {
/**
* Optimistic action for indicating that the request post update has completed
* successfully.
* @param {Object} data The data for the action.
* @param {Object} data.previousPost The previous post prior to update.
* @param {Object} data.post The new post after update
* @param {boolean} data.isRevision Whether the post is a revision or not.
* @param {Object} data.options Options passed through from the original
* action dispatch.
* @param {Object} data.postType The post type object.
*
* @return {Object} Action object.
* @param {Object} data The data for the action.
* @param {Object} data.previousPost The previous post prior to update.
* @param {Object} data.post The new post after update
* @param {boolean} data.isRevision Whether the post is a revision or not.
* @param {Object} data.options Options passed through from the original
* action dispatch.
* @param {Object} data.postType The post type object.
*
* @return {Object} Action object.
*/
export function __experimentalRequestPostUpdateSuccess( {
previousPost,
Expand Down Expand Up @@ -130,12 +131,12 @@ export function __experimentalRequestPostUpdateSuccess( {
* Optimistic action for indicating that the request post update has completed
* with a failure.
*
* @param {Object} data The data for the action
* @param {Object} data.post The post that failed updating.
* @param {Object} data.edits The fields that were being updated.
* @param {*} data.error The error from the failed call.
* @param {Object} data.options Options passed through from the original
* action dispatch.
* @param {Object} data The data for the action
nerrad marked this conversation as resolved.
Show resolved Hide resolved
* @param {Object} data.post The post that failed updating.
* @param {Object} data.edits The fields that were being updated.
* @param {*} data.error The error from the failed call.
* @param {Object} data.options Options passed through from the original
* action dispatch.
* @return {Object} An action object
*/
export function __experimentalRequestPostUpdateFailure( {
Expand Down Expand Up @@ -221,14 +222,14 @@ export function __experimentalOptimisticUpdatePost( edits ) {
*/
export function* savePost( options = {} ) {
const isEditedPostSaveable = yield select(
MODULE_KEY,
STORE_KEY,
'isEditedPostSaveable'
);
if ( ! isEditedPostSaveable ) {
return;
}
let edits = yield select(
MODULE_KEY,
STORE_KEY,
'getPostEdits'
);
const isAutosave = !! options.isAutosave;
Expand All @@ -238,7 +239,7 @@ export function* savePost( options = {} ) {
}

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

Expand All @@ -258,12 +259,12 @@ export function* savePost( options = {} ) {
}

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

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

Expand All @@ -274,7 +275,7 @@ export function* savePost( options = {} ) {
};

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

Expand All @@ -285,7 +286,7 @@ export function* savePost( options = {} ) {
);

yield dispatch(
MODULE_KEY,
STORE_KEY,
'__experimentalRequestPostUpdateStart',
options,
);
Expand All @@ -294,7 +295,7 @@ export function* savePost( options = {} ) {
// will be updated. See below logic in success resolution for revert
// if the autosave is applied as a revision.
yield dispatch(
MODULE_KEY,
STORE_KEY,
'__experimentalOptimisticUpdatePost',
toSend
);
Expand All @@ -303,7 +304,7 @@ export function* savePost( options = {} ) {
let method = 'PUT';
if ( isAutosave ) {
const autoSavePost = yield select(
MODULE_KEY,
STORE_KEY,
'getAutosave',
);
// Ensure autosaves contain all expected fields, using autosave or
Expand Down Expand Up @@ -336,10 +337,10 @@ export function* savePost( options = {} ) {
} );
const resetAction = isAutosave ? 'resetAutosave' : 'resetPost';

yield dispatch( MODULE_KEY, resetAction, newPost );
yield dispatch( STORE_KEY, resetAction, newPost );

yield dispatch(
MODULE_KEY,
STORE_KEY,
'__experimentalRequestPostUpdateSuccess',
{
previousPost: post,
Expand Down Expand Up @@ -368,7 +369,7 @@ export function* savePost( options = {} ) {
}
} catch ( error ) {
yield dispatch(
MODULE_KEY,
STORE_KEY,
'__experimentalRequestPostUpdateFailure',
{ post, edits, error, options }
);
Expand All @@ -392,11 +393,11 @@ export function* savePost( options = {} ) {
*/
export function* refreshPost() {
const post = yield select(
MODULE_KEY,
STORE_KEY,
'getCurrentPost'
);
const postTypeSlug = yield select(
MODULE_KEY,
STORE_KEY,
'getCurrentPostType'
);
const postType = yield resolveSelect(
Expand All @@ -413,7 +414,7 @@ export function* refreshPost() {
}
);
yield dispatch(
MODULE_KEY,
STORE_KEY,
'resetPost',
newPost
);
Expand All @@ -424,7 +425,7 @@ export function* refreshPost() {
*/
export function* trashPost() {
const postTypeSlug = yield select(
MODULE_KEY,
STORE_KEY,
'getCurrentPostType'
);
const postType = yield resolveSelect(
Expand All @@ -439,7 +440,7 @@ export function* trashPost() {
);
try {
const post = yield select(
MODULE_KEY,
STORE_KEY,
'getCurrentPost'
);
yield apiFetch(
Expand All @@ -452,7 +453,7 @@ export function* trashPost() {
// TODO: This should be an updatePost action (updating subsets of post
// properties), but right now editPost is tied with change detection.
yield dispatch(
MODULE_KEY,
STORE_KEY,
'resetPost',
{ ...post, status: 'trash' }
);
Expand All @@ -471,7 +472,11 @@ export function* trashPost() {
* @param {Object?} options Extra flags to identify the autosave.
*/
export function* autosave( options ) {
yield* generatorActions.savePost( { isAutosave: true, ...options } );
yield dispatch(
STORE_KEY,
'savePost',
{ isAutosave: true, ...options }
);
}

/**
Expand Down Expand Up @@ -733,12 +738,3 @@ export const exitFormattedText = getBlockEditorAction( 'exitFormattedText' );
export const insertDefaultBlock = getBlockEditorAction( 'insertDefaultBlock' );
export const updateBlockListSettings = getBlockEditorAction( 'updateBlockListSettings' );
export const updateEditorSettings = getBlockEditorAction( 'updateEditorSettings' );

// default export of generator actions.
const generatorActions = {
savePost,
autosave,
trashPost,
refreshPost,
};
export default generatorActions;
2 changes: 1 addition & 1 deletion packages/editor/src/store/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const EDIT_MERGE_PROPERTIES = new Set( [
* Constant for the store module (or reducer) key.
* @type {string}
*/
export const MODULE_KEY = 'core/editor';
export const STORE_KEY = 'core/editor';

export const POST_UPDATE_TRANSACTION_ID = 'post-update';
export const SAVE_POST_NOTICE_ID = 'SAVE_POST_NOTICE_ID';
Expand Down
6 changes: 3 additions & 3 deletions packages/editor/src/store/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export function select( storeKey, selectorName, ...args ) {
* Dispatches a control action for triggering a registry select that has a
* resolver.
*
* @param {string} storeKey
* @param {string} selectorName
* @param {Array} args Arguments for the select.
* @param {string} storeKey
* @param {string} selectorName
* @param {Array} args Arguments for the select.
*
* @return {Object} control descriptor.
*/
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import applyMiddlewares from './middlewares';
import * as selectors from './selectors';
import * as actions from './actions';
import controls from './controls';
import { MODULE_KEY } from './constants';
import { STORE_KEY } from './constants';

const store = registerStore( MODULE_KEY, {
const store = registerStore( STORE_KEY, {
reducer,
selectors,
actions,
Expand Down
4 changes: 2 additions & 2 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export function isEditedPostDirty( state ) {
return true;
}

// Edits and change detectiona are reset at the start of a save, but a post
// Edits and change detection are reset at the start of a save, but a post
// is still considered dirty until the point at which the save completes.
// Because the save is performed optimistically, the prior states are held
// until committed. These can be referenced to determine whether there's a
Expand Down Expand Up @@ -258,7 +258,7 @@ export function getCurrentPostAttribute( state, attributeName ) {

/**
* Returns a single attribute of the post being edited, preferring the unsaved
* edit if one exists, but mergiging with the attribute value for the last known
* edit if one exists, but merging with the attribute value for the last known
* saved state of the post (this is needed for some nested attributes like meta).
*
* @param {Object} state Global application state.
Expand Down
Loading