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

Add support for Core Data Entities Actions #7175

Closed
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
39 changes: 38 additions & 1 deletion core-data/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* External dependencies
*/
import { castArray } from 'lodash';
import { castArray, find } from 'lodash';

/**
* WordPress dependencies
*/
import apiRequest from '@wordpress/api-request';

/**
* Internal dependencies
*/
import { getKindEntities } from './entities';

/**
* Returns an action object used in signalling that terms have been received
Expand Down Expand Up @@ -81,3 +91,30 @@ export function receiveThemeSupportsFromIndex( index ) {
themeSupports: index.theme_supports,
};
}

/**
* Action generator to trigger update an entity record.
*
* @param {string} kind Kind of the received entity.
* @param {string} name Name of the received entity.
* @param {Object} record Records to update.
*
* @return {Promise} Promise of the updated post
*/
export async function* updateEntityRecord( kind, name, record ) {
const entities = yield* getKindEntities( kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
}

const key = entity.key || 'id';
const updatedRecord = await apiRequest( {
path: `${ entity.baseUrl }/${ record[ key ] }?context=edit`,
method: 'PUT',
data: record,
} );
yield receiveEntityRecords( kind, name, updatedRecord );

return updatedRecord;
}
7 changes: 3 additions & 4 deletions core-data/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { upperFirst, camelCase, map, find } from 'lodash';
* WordPress dependencies
*/
import apiRequest from '@wordpress/api-request';
import { select } from '@wordpress/data';

/**
* Internal dependencies
*/
import { getEntitiesByKind } from './selectors';
import { addEntities } from './actions';

export const defaultEntities = [
Expand Down Expand Up @@ -61,13 +61,12 @@ export const getMethodName = ( kind, name, prefix = 'get', usePlural = false ) =
/**
* Loads the kind entities into the store.
*
* @param {Object} state Global state
* @param {string} kind Kind
*
* @return {Array} Entities
*/
export async function* getKindEntities( state, kind ) {
let entities = getEntitiesByKind( state, kind );
export async function* getKindEntities( kind ) {
let entities = select( 'core' ).getEntitiesByKind( kind );

if ( entities && entities.length !== 0 ) {
return entities;
Expand Down
7 changes: 6 additions & 1 deletion core-data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ const createEntityRecordGetter = ( source ) => defaultEntities.reduce( ( result,

const entityResolvers = createEntityRecordGetter( resolvers );
const entitySelectors = createEntityRecordGetter( selectors );
const entityActions = defaultEntities.reduce( ( result, entity ) => {
const { kind, name } = entity;
result[ getMethodName( kind, name, 'update' ) ] = actions.updateEntityRecord.bind( null, kind, name );
return result;
}, {} );

const store = registerStore( REDUCER_KEY, {
reducer,
actions,
actions: { ...actions, ...entityActions },
selectors: { ...selectors, ...entitySelectors },
resolvers: { ...resolvers, ...entityResolvers },
} );
Expand Down
4 changes: 2 additions & 2 deletions core-data/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function* getAuthors() {
* @param {number} key Record's key
*/
export async function* getEntityRecord( state, kind, name, key ) {
const entities = yield* await getKindEntities( state, kind );
const entities = yield* await getKindEntities( kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
Expand All @@ -62,7 +62,7 @@ export async function* getEntityRecord( state, kind, name, key ) {
* @param {string} name Entity name.
*/
export async function* getEntityRecords( state, kind, name ) {
const entities = yield* await getKindEntities( state, kind );
const entities = yield* await getKindEntities( kind );
const entity = find( entities, { kind, name } );
if ( ! entity ) {
return;
Expand Down
10 changes: 4 additions & 6 deletions editor/store/effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@wordpress/blocks';
import { __ } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { dispatch as dispatchAction } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -111,7 +112,7 @@ export default {
content: getEditedPostContent( state ),
id: post.id,
};
const basePath = wp.api.getPostTypeRoute( getCurrentPostType( state ) );
const postType = getCurrentPostType( state );

dispatch( {
type: 'REQUEST_POST_UPDATE_START',
Expand All @@ -130,6 +131,7 @@ export default {
parent: post.id,
};

const basePath = wp.api.getPostTypeRoute( postType );
request = wp.apiRequest( {
path: `/wp/v2/${ basePath }/${ post.id }/autosaves`,
method: 'POST',
Expand All @@ -145,11 +147,7 @@ export default {
dispatch( removeNotice( SAVE_POST_NOTICE_ID ) );
dispatch( removeNotice( AUTOSAVE_POST_NOTICE_ID ) );

request = wp.apiRequest( {
path: `/wp/v2/${ basePath }/${ post.id }`,
method: 'PUT',
data: toSend,
} );
request = dispatchAction( 'core' ).updateEntityRecord( 'postType', postType, toSend );
}

request.then(
Expand Down
14 changes: 9 additions & 5 deletions packages/data/src/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,20 @@ export default function createStoreRuntime( store ) {
return async ( actionCreator ) => {
if ( isActionLike( actionCreator ) ) {
store.dispatch( actionCreator );
return;
return actionCreator;
}

// Attempt to normalize the action creator as async iterable.
actionCreator = toAsyncIterable( actionCreator );
for await ( const maybeAction of actionCreator ) {
let ret;
do {
ret = await actionCreator.next();
// Dispatch if it quacks like an action.
if ( isActionLike( maybeAction ) ) {
store.dispatch( maybeAction );
if ( isActionLike( ret.value ) ) {
store.dispatch( ret.value );
}
}
} while ( ! ret.done );

return ret.value;
};
}