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

Try: Introduce 'getEntityConfig' resolver #61101

Closed
wants to merge 2 commits into from
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
36 changes: 10 additions & 26 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import deprecated from '@wordpress/deprecated';
*/
import { getNestedValue, setNestedValue } from './utils';
import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
import { DEFAULT_ENTITY_KEY } from './entities';
import { createBatch } from './batch';
import { STORE_NAME } from './name';
import { getSyncProvider } from './sync';
Expand Down Expand Up @@ -285,11 +285,8 @@ export const deleteEntityRecord =
query,
{ __unstableFetch = apiFetch, throwOnError = false } = {}
) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);
async ( { dispatch, resolveSelect } ) => {
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
let error;
let deletedRecord = false;
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
Expand Down Expand Up @@ -503,10 +500,7 @@ export const saveEntityRecord =
} = {}
) =>
async ( { select, resolveSelect, dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand Down Expand Up @@ -776,14 +770,11 @@ export const __experimentalBatch =
*/
export const saveEditedEntityRecord =
( kind, name, recordId, options ) =>
async ( { select, dispatch } ) => {
async ( { select, dispatch, resolveSelect } ) => {
if ( ! select.hasEditsForEntityRecord( kind, name, recordId ) ) {
return;
}
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
if ( ! entityConfig ) {
return;
}
Expand All @@ -809,7 +800,7 @@ export const saveEditedEntityRecord =
*/
export const __experimentalSaveSpecifiedEntityEdits =
( kind, name, recordId, itemsToSave, options ) =>
async ( { select, dispatch } ) => {
async ( { select, dispatch, resolveSelect } ) => {
if ( ! select.hasEditsForEntityRecord( kind, name, recordId ) ) {
return;
}
Expand All @@ -824,11 +815,7 @@ export const __experimentalSaveSpecifiedEntityEdits =
setNestedValue( editsToSave, item, getNestedValue( edits, item ) );
}

const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);

const entityConfig = await resolveSelect.getEntityConfig( kind, name );
const entityIdKey = entityConfig?.key || DEFAULT_ENTITY_KEY;

// If a record key is provided then update the existing record.
Expand Down Expand Up @@ -947,11 +934,8 @@ export function receiveDefaultTemplateId( query, templateId ) {
*/
export const receiveRevisions =
( kind, name, recordKey, records, query, invalidateCache = false, meta ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);
async ( { dispatch, resolveSelect } ) => {
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
const key =
entityConfig && entityConfig?.revisionKey
? entityConfig.revisionKey
Expand Down
64 changes: 36 additions & 28 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import apiFetch from '@wordpress/api-fetch';
* Internal dependencies
*/
import { STORE_NAME } from './name';
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
import { DEFAULT_ENTITY_KEY, additionalEntityConfigLoaders } from './entities';
import { forwardResolver, getNormalizedCommaSeparable } from './utils';
import { getSyncProvider } from './sync';
import { fetchBlockPatterns } from './fetch';
Expand Down Expand Up @@ -58,11 +58,8 @@ export const getCurrentUser =
*/
export const getEntityRecord =
( kind, name, key = '', query ) =>
async ( { select, dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
);
async ( { select, dispatch, resolveSelect } ) => {
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand Down Expand Up @@ -193,11 +190,8 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' );
*/
export const getEntityRecords =
( kind, name, query = {} ) =>
async ( { dispatch, registry } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
);
async ( { dispatch, registry, resolveSelect } ) => {
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand Down Expand Up @@ -432,11 +426,8 @@ export const canUser =
*/
export const canUserEditEntityRecord =
( kind, name, recordId ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
);
async ( { dispatch, resolveSelect } ) => {
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
if ( ! entityConfig ) {
return;
}
Expand Down Expand Up @@ -729,12 +720,8 @@ export const getDefaultTemplateId =
*/
export const getRevisions =
( kind, name, recordKey, query = {} ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
);

async ( { dispatch, resolveSelect } ) => {
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand Down Expand Up @@ -854,12 +841,8 @@ getRevisions.shouldInvalidate = ( action, kind, name, recordKey ) =>
*/
export const getRevision =
( kind, name, recordKey, revisionKey, query ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind, name ) );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
);

async ( { dispatch, resolveSelect } ) => {
const entityConfig = await resolveSelect.getEntityConfig( kind, name );
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand Down Expand Up @@ -896,3 +879,28 @@ export const getRevision =
dispatch.receiveRevisions( kind, name, recordKey, record, query );
}
};

export const getEntityConfig =
( kind, name ) =>
async ( { dispatch } ) => {
const loader = additionalEntityConfigLoaders.find( ( l ) => {
if ( ! name || ! l.name ) {
return l.kind === kind;
}

return l.kind === kind && l.name === name;
} );

if ( ! loader ) {
return;
}

// @todo: Prevent resolving the same resource twice.
// See `canUser` resolver for an example.

const configs = await loader.loadEntities();

if ( configs.length > 0 ) {
dispatch.addEntities( configs );
}
};
Loading
Loading