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 builtin data controls instead of deprecated data-controls ones #25772

Merged
merged 2 commits into from
Oct 26, 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
34 changes: 19 additions & 15 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { castArray, get, isEqual, find } from 'lodash';
/**
* WordPress dependencies
*/
import { apiFetch, syncSelect } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import { addQueryArgs } from '@wordpress/url';

/**
Expand Down Expand Up @@ -210,21 +211,21 @@ export function* deleteEntityRecord( kind, name, recordId, query ) {
* @return {Object} Action object.
*/
export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
const entity = yield syncSelect( 'core', 'getEntity', kind, name );
const entity = yield controls.select( '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 syncSelect(
const record = yield controls.select(
'core',
'getRawEntityRecord',
kind,
name,
recordId
);
const editedRecord = yield syncSelect(
const editedRecord = yield controls.select(
'core',
'getEditedEntityRecord',
kind,
Expand Down Expand Up @@ -270,7 +271,7 @@ export function* editEntityRecord( kind, name, recordId, edits, options = {} ) {
* an entity record, if any.
*/
export function* undo() {
const undoEdit = yield syncSelect( 'core', 'getUndoEdit' );
const undoEdit = yield controls.select( 'core', 'getUndoEdit' );
if ( ! undoEdit ) {
return;
}
Expand All @@ -288,7 +289,7 @@ export function* undo() {
* edit to an entity record, if any.
*/
export function* redo() {
const redoEdit = yield syncSelect( 'core', 'getRedoEdit' );
const redoEdit = yield controls.select( 'core', 'getRedoEdit' );
if ( ! redoEdit ) {
return;
}
Expand Down Expand Up @@ -338,7 +339,7 @@ export function* saveEntityRecord(
for ( const [ key, value ] of Object.entries( record ) ) {
if ( typeof value === 'function' ) {
const evaluatedValue = value(
yield syncSelect(
yield controls.select(
'core',
'getEditedEntityRecord',
kind,
Expand Down Expand Up @@ -372,7 +373,7 @@ export function* saveEntityRecord(
let currentEdits;
try {
const path = `${ entity.baseURL }${ recordId ? '/' + recordId : '' }`;
const persistedRecord = yield syncSelect(
const persistedRecord = yield controls.select(
'core',
'getRawEntityRecord',
kind,
Expand All @@ -385,9 +386,12 @@ 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 syncSelect( 'core', 'getCurrentUser' );
const currentUser = yield controls.select(
'core',
'getCurrentUser'
);
const currentUserId = currentUser ? currentUser.id : undefined;
const autosavePost = yield syncSelect(
const autosavePost = yield controls.select(
'core',
'getAutosave',
persistedRecord.type,
Expand Down Expand Up @@ -479,14 +483,14 @@ 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 syncSelect(
persistedEntity = yield controls.select(
'core',
'__experimentalGetEntityRecordNoResolver',
kind,
name,
recordId
);
currentEdits = yield syncSelect(
currentEdits = yield controls.select(
'core',
'getEntityRecordEdits',
kind,
Expand Down Expand Up @@ -533,7 +537,7 @@ export function* saveEntityRecord(
recordId,
{
...currentEdits,
...( yield syncSelect(
...( yield controls.select(
'core',
'getEntityRecordEdits',
kind,
Expand Down Expand Up @@ -567,7 +571,7 @@ export function* saveEntityRecord(
*/
export function* saveEditedEntityRecord( kind, name, recordId, options ) {
if (
! ( yield syncSelect(
! ( yield controls.select(
'core',
'hasEditsForEntityRecord',
kind,
Expand All @@ -577,7 +581,7 @@ export function* saveEditedEntityRecord( kind, name, recordId, options ) {
) {
return;
}
const edits = yield syncSelect(
const edits = yield controls.select(
'core',
'getEntityRecordNonTransientEdits',
kind,
Expand Down
5 changes: 3 additions & 2 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { upperFirst, camelCase, map, find, get, startCase } from 'lodash';
/**
* WordPress dependencies
*/
import { apiFetch, syncSelect } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import { __ } from '@wordpress/i18n';

/**
Expand Down Expand Up @@ -188,7 +189,7 @@ export const getMethodName = (
* @return {Array} Entities
*/
export function* getKindEntities( kind ) {
let entities = yield syncSelect( 'core', 'getEntitiesByKind', kind );
let entities = yield controls.select( 'core', 'getEntitiesByKind', kind );
if ( entities && entities.length !== 0 ) {
return entities;
}
Expand Down
9 changes: 5 additions & 4 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { find, includes, get, hasIn, compact, uniq } from 'lodash';
*/
import { addQueryArgs } from '@wordpress/url';
import deprecated from '@wordpress/deprecated';
import { apiFetch, select, syncSelect } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';

/**
* Internal dependencies
Expand Down Expand Up @@ -91,7 +92,7 @@ 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 syncSelect(
const hasRecords = yield controls.select(
'core',
'hasEntityRecords',
kind,
Expand Down Expand Up @@ -297,7 +298,7 @@ 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 select(
const { rest_base: restBase } = yield controls.resolveSelect(
'core',
'getPostType',
postType
Expand All @@ -321,5 +322,5 @@ export function* getAutosaves( postType, postId ) {
* @param {number} postId The id of the parent post.
*/
export function* getAutosave( postType, postId ) {
yield select( 'core', 'getAutosaves', postType, postId );
yield controls.resolveSelect( 'core', 'getAutosaves', postType, postId );
}
4 changes: 2 additions & 2 deletions packages/core-data/src/test/actions.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { syncSelect } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';

/**
* Internal dependencies
Expand All @@ -26,7 +26,7 @@ describe( 'editEntityRecord', () => {
{}
);
expect( fulfillment.next().value ).toEqual(
syncSelect( 'core', 'getEntity', entity.kind, entity.name )
controls.select( 'core', 'getEntity', entity.kind, entity.name )
);
// Don't pass back an entity config.
expect( fulfillment.next.bind( fulfillment ) ).toThrow(
Expand Down
4 changes: 2 additions & 2 deletions packages/core-data/src/utils/if-not-resolved.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';

/**
* Higher-order function which invokes the given resolver only if it has not
Expand All @@ -20,7 +20,7 @@ const ifNotResolved = ( resolver, selectorName ) =>
* @param {...any} args Original resolver arguments.
*/
function* resolveIfNotResolved( ...args ) {
const hasStartedResolution = yield select(
const hasStartedResolution = yield controls.select(
'core',
'hasStartedResolution',
selectorName,
Expand Down
14 changes: 8 additions & 6 deletions packages/core-data/src/utils/test/if-not-resolved.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
/**
* WordPress dependencies
*/
import { select } from '@wordpress/data-controls';
import { controls } from '@wordpress/data';

/**
* Internal dependencies
*/
import ifNotResolved from '../if-not-resolved';

jest.mock( '@wordpress/data-controls', () => ( {
select: jest.fn(),
jest.mock( '@wordpress/data', () => ( {
controls: {
select: jest.fn(),
},
} ) );

describe( 'ifNotResolved', () => {
beforeEach( () => {
select.mockReset();
controls.select.mockReset();
} );

it( 'returns a new function', () => {
Expand All @@ -26,7 +28,7 @@ describe( 'ifNotResolved', () => {
} );

it( 'triggers original resolver if not already resolved', () => {
select.mockImplementation( ( _storeKey, selectorName ) => ( {
controls.select.mockImplementation( ( _storeKey, selectorName ) => ( {
_nextValue:
selectorName === 'hasStartedResolution' ? false : undefined,
} ) );
Expand All @@ -49,7 +51,7 @@ describe( 'ifNotResolved', () => {
} );

it( 'does not trigger original resolver if already resolved', () => {
select.mockImplementation( ( _storeKey, selectorName ) => ( {
controls.select.mockImplementation( ( _storeKey, selectorName ) => ( {
_nextValue:
selectorName === 'hasStartedResolution' ? true : undefined,
} ) );
Expand Down