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

core-data: use controls from data-controls in favor of the package-local ones #25235

Merged
merged 1 commit into from
Sep 14, 2020
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
63 changes: 48 additions & 15 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import { castArray, get, isEqual, find } from 'lodash';
/**
* WordPress dependencies
*/
import { apiFetch, __unstableSyncSelect } from '@wordpress/data-controls';
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities';
import { select, apiFetch } from './controls';

/**
* Returns an action object used in signalling that authors have been received.
Expand Down Expand Up @@ -210,15 +210,27 @@ export function* deleteEntityRecord( kind, name, recordId, query ) {
* @return {Object} Action object.
*/
export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
const entity = yield select( 'getEntity', kind, name );
const entity = yield __unstableSyncSelect(
'core',
'getEntity',
kind,
name
);
if ( ! entity ) {
throw new Error(
`The entity being edited (${ kind }, ${ name }) does not have a loaded config.`
);
}
const { transientEdits = {}, mergedEdits = {} } = entity;
const record = yield select( 'getRawEntityRecord', kind, name, recordId );
const editedRecord = yield select(
const record = yield __unstableSyncSelect(
'core',
'getRawEntityRecord',
kind,
name,
recordId
);
const editedRecord = yield __unstableSyncSelect(
'core',
'getEditedEntityRecord',
kind,
name,
Expand Down Expand Up @@ -263,7 +275,7 @@ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
* an entity record, if any.
*/
export function* undo() {
const undoEdit = yield select( 'getUndoEdit' );
const undoEdit = yield __unstableSyncSelect( 'core', 'getUndoEdit' );
if ( ! undoEdit ) {
return;
}
Expand All @@ -281,7 +293,7 @@ export function* undo() {
* edit to an entity record, if any.
*/
export function* redo() {
const redoEdit = yield select( 'getRedoEdit' );
const redoEdit = yield __unstableSyncSelect( 'core', 'getRedoEdit' );
if ( ! redoEdit ) {
return;
}
Expand Down Expand Up @@ -331,7 +343,13 @@ export function* saveEntityRecord(
for ( const [ key, value ] of Object.entries( record ) ) {
if ( typeof value === 'function' ) {
const evaluatedValue = value(
yield select( 'getEditedEntityRecord', kind, name, recordId )
yield __unstableSyncSelect(
'core',
'getEditedEntityRecord',
kind,
name,
recordId
)
);
yield editEntityRecord(
kind,
Expand Down Expand Up @@ -359,7 +377,8 @@ export function* saveEntityRecord(
let currentEdits;
try {
const path = `${ entity.baseURL }${ recordId ? '/' + recordId : '' }`;
const persistedRecord = yield select(
const persistedRecord = yield __unstableSyncSelect(
'core',
'getRawEntityRecord',
kind,
name,
Expand All @@ -371,9 +390,13 @@ export function* saveEntityRecord(
// This is fine for now as it is the only supported autosave,
// but ideally this should all be handled in the back end,
// so the client just sends and receives objects.
const currentUser = yield select( 'getCurrentUser' );
const currentUser = yield __unstableSyncSelect(
'core',
'getCurrentUser'
);
const currentUserId = currentUser ? currentUser.id : undefined;
const autosavePost = yield select(
const autosavePost = yield __unstableSyncSelect(
'core',
'getAutosave',
persistedRecord.type,
persistedRecord.id,
Expand Down Expand Up @@ -464,13 +487,15 @@ export function* saveEntityRecord(

// Get the full local version of the record before the update,
// to merge it with the edits and then propagate it to subscribers
persistedEntity = yield select(
persistedEntity = yield __unstableSyncSelect(
'core',
'__experimentalGetEntityRecordNoResolver',
kind,
name,
recordId
);
currentEdits = yield select(
currentEdits = yield __unstableSyncSelect(
'core',
'getEntityRecordEdits',
kind,
name,
Expand Down Expand Up @@ -516,7 +541,8 @@ export function* saveEntityRecord(
recordId,
{
...currentEdits,
...( yield select(
...( yield __unstableSyncSelect(
'core',
'getEntityRecordEdits',
kind,
name,
Expand Down Expand Up @@ -549,11 +575,18 @@ export function* saveEntityRecord(
*/
export function* saveEditedEntityRecord( kind, name, recordId, options ) {
if (
! ( yield select( 'hasEditsForEntityRecord', kind, name, recordId ) )
! ( yield __unstableSyncSelect(
'core',
'hasEditsForEntityRecord',
kind,
name,
recordId
) )
) {
return;
}
const edits = yield select(
const edits = yield __unstableSyncSelect(
'core',
'getEntityRecordNonTransientEdits',
kind,
name,
Expand Down
73 changes: 0 additions & 73 deletions packages/core-data/src/controls.js

This file was deleted.

8 changes: 6 additions & 2 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import { upperFirst, camelCase, map, find, get, startCase } from 'lodash';
/**
* WordPress dependencies
*/
import { apiFetch, __unstableSyncSelect } from '@wordpress/data-controls';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { addEntities } from './actions';
import { apiFetch, select } from './controls';

export const DEFAULT_ENTITY_KEY = 'id';

Expand Down Expand Up @@ -188,7 +188,11 @@ export const getMethodName = (
* @return {Array} Entities
*/
export function* getKindEntities( kind ) {
let entities = yield select( 'getEntitiesByKind', kind );
let entities = yield __unstableSyncSelect(
'core',
'getEntitiesByKind',
kind
);
if ( entities && entities.length !== 0 ) {
return entities;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* WordPress dependencies
*/
import { registerStore } from '@wordpress/data';
import { controls } from '@wordpress/data-controls';

/**
* Internal dependencies
*/
import reducer from './reducer';
import controls from './controls';
import * as selectors from './selectors';
import * as actions from './actions';
import * as resolvers from './resolvers';
Expand Down
14 changes: 10 additions & 4 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import { find, includes, get, hasIn, compact, uniq } from 'lodash';
*/
import { addQueryArgs } from '@wordpress/url';
import deprecated from '@wordpress/deprecated';
import {
apiFetch,
select,
__unstableSyncSelect,
} from '@wordpress/data-controls';

/**
* Internal dependencies
Expand All @@ -23,7 +28,6 @@ import {
receiveAutosaves,
} from './actions';
import { getKindEntities, DEFAULT_ENTITY_KEY } from './entities';
import { apiFetch, select, resolveSelect } from './controls';
import { ifNotResolved, getNormalizedCommaSeparable } from './utils';

/**
Expand Down Expand Up @@ -91,7 +95,8 @@ export function* getEntityRecord( kind, name, key = '', query ) {
// The resolution cache won't consider query as reusable based on the
// fields, so it's tested here, prior to initiating the REST request,
// and without causing `getEntityRecords` resolution to occur.
const hasRecords = yield select(
const hasRecords = yield __unstableSyncSelect(
'core',
'hasEntityRecords',
kind,
name,
Expand Down Expand Up @@ -296,7 +301,8 @@ export function* canUser( action, resource, id ) {
* @param {number} postId The id of the parent post.
*/
export function* getAutosaves( postType, postId ) {
const { rest_base: restBase } = yield resolveSelect(
const { rest_base: restBase } = yield select(
'core',
'getPostType',
postType
);
Expand All @@ -319,5 +325,5 @@ export function* getAutosaves( postType, postId ) {
* @param {number} postId The id of the parent post.
*/
export function* getAutosave( postType, postId ) {
yield resolveSelect( 'getAutosaves', postType, postId );
yield select( 'core', 'getAutosaves', postType, postId );
}
29 changes: 19 additions & 10 deletions packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { __unstableSyncSelect } from '@wordpress/data-controls';

/**
* Internal dependencies
*/
Expand All @@ -10,7 +15,6 @@ import {
receiveAutosaves,
receiveCurrentUser,
} from '../actions';
import { select } from '../controls';

describe( 'editEntityRecord', () => {
it( 'throws when the edited entity does not have a loaded config.', () => {
Expand All @@ -22,7 +26,12 @@ describe( 'editEntityRecord', () => {
{}
);
expect( fulfillment.next().value ).toEqual(
select( 'getEntity', entity.kind, entity.name )
__unstableSyncSelect(
'core',
'getEntity',
entity.kind,
entity.name
)
);
// Don't pass back an entity config.
expect( fulfillment.next.bind( fulfillment ) ).toThrow(
Expand Down Expand Up @@ -83,11 +92,11 @@ describe( 'saveEntityRecord', () => {

// Should select __experimentalGetEntityRecordNoResolver selector (as opposed to getEntityRecord)
// see https://github.com/WordPress/gutenberg/pull/19752#discussion_r368498318.
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.selectorName ).toBe(
'__experimentalGetEntityRecordNoResolver'
);
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.type ).toBe( 'RECEIVE_ITEMS' );
const { value: apiFetchAction } = fulfillment.next( {} );
expect( apiFetchAction.request ).toEqual( {
Expand Down Expand Up @@ -125,9 +134,9 @@ describe( 'saveEntityRecord', () => {
expect( fulfillment.next( entities ).value.type ).toBe(
'SAVE_ENTITY_RECORD_START'
);
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.type ).toBe( 'RECEIVE_ITEMS' );
const { value: apiFetchAction } = fulfillment.next( {} );
expect( apiFetchAction.request ).toEqual( {
Expand Down Expand Up @@ -162,9 +171,9 @@ describe( 'saveEntityRecord', () => {
expect( fulfillment.next( entities ).value.type ).toBe(
'SAVE_ENTITY_RECORD_START'
);
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.type ).toBe( 'SYNC_SELECT' );
expect( fulfillment.next().value.type ).toBe( 'RECEIVE_ITEMS' );
const { value: apiFetchAction } = fulfillment.next( {} );
expect( apiFetchAction.request ).toEqual( {
Expand Down
Loading