Skip to content

Commit

Permalink
Trying tuple approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 15, 2023
1 parent d5c7945 commit b9d768c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
10 changes: 4 additions & 6 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,10 @@ async function loadPostTypeRevisionsEntities() {
const namespace = postType?.rest_namespace ?? 'wp/v2';
return {
kind: 'revisions',
getBaseUrl: ( { parent } ) =>
`/${ namespace }/${ postType.rest_base }/${ parent }/revisions`,
getQueryArgs: ( query ) => {
const { parent, ...rest } = query;
return rest;
},
getBaseUrl: ( { name: revisionPostTypeAndParentId } ) =>
`/${ namespace }/${ postType.rest_base }/${
revisionPostTypeAndParentId.split( ':' )[ 1 ]
}/revisions`,
baseURLParams: { context: 'view' },
name,
label: postType.name,
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function entity( entityConfig ) {
( action ) =>
action.name &&
action.kind &&
action.name === entityConfig.name &&
action.name.split( ':' )[ 0 ] === entityConfig.name &&
action.kind === entityConfig.kind
),

Expand Down
31 changes: 15 additions & 16 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,9 +191,11 @@ export const getEntityRecords =
( kind, name, query = {} ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
const splitName = name.split( ':' );
const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
( config ) => config.name === splitName[ 0 ] && config.kind === kind
);

if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand All @@ -204,43 +206,40 @@ export const getEntityRecords =
{ exclusive: false }
);

let queryArgs = !! entityConfig.getQueryArgs
? entityConfig.getQueryArgs( query )
: query;

try {
if ( queryArgs._fields ) {
if ( query._fields ) {
// If requesting specific fields, items and query association to said
// records are stored by ID reference. Thus, fields must always include
// the ID.
queryArgs = {
...queryArgs,
query = {
...query,
_fields: [
...new Set( [
...( getNormalizedCommaSeparable(
queryArgs._fields
) || [] ),
...( getNormalizedCommaSeparable( query._fields ) ||
[] ),
entityConfig.key || DEFAULT_ENTITY_KEY,
] ),
].join(),
};
}

const baseUrl = !! entityConfig.getBaseUrl
? entityConfig.getBaseUrl( query )
? entityConfig.getBaseUrl( { kind, name, query } )
: entityConfig.baseURL;
const path = addQueryArgs( baseUrl, {
...entityConfig.baseURLParams,
...queryArgs,
...query,
} );



let records = Object.values( await apiFetch( { path } ) );
// If we request fields but the result doesn't contain the fields,
// explicitly set these fields as "undefined"
// that way we consider the query "fullfilled".
if ( queryArgs._fields ) {
if ( query._fields ) {
records = records.map( ( record ) => {
queryArgs._fields.split( ',' ).forEach( ( field ) => {
query._fields.split( ',' ).forEach( ( field ) => {
if ( ! record.hasOwnProperty( field ) ) {
record[ field ] = undefined;
}
Expand All @@ -255,7 +254,7 @@ export const getEntityRecords =
// When requesting all fields, the list of results can be used to
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
// See https://github.com/WordPress/gutenberg/pull/26575
if ( ! queryArgs?._fields && ! queryArgs.context ) {
if ( ! query?._fields && ! query.context ) {
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
Expand Down
2 changes: 2 additions & 0 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,8 @@ export const getEntityRecords = ( <
): EntityRecord[] | null => {
// Queried data state is prepopulated for all known entities. If this is not
// assigned for the given parameters, then it is known to not exist.
console.log( 'state.entities.records', state.entities.records );

const queriedState =
state.entities.records?.[ kind ]?.[ name ]?.queriedData;
if ( ! queriedState ) {
Expand Down

0 comments on commit b9d768c

Please sign in to comment.