Skip to content

Commit

Permalink
Testing out parsing name params, e.g., post:id:revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 18, 2023
1 parent be66282 commit 0edea5d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 13 deletions.
4 changes: 3 additions & 1 deletion packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,9 @@ export const prePersistPostType = ( persistedRecord, edits ) => {
* @return {Promise} Entities promise
*/
async function loadPostTypeEntities() {
// @TODO 'edit' context required to get supports collection.
const postTypes = await apiFetch( {
path: '/wp/v2/types?context=view',
path: '/wp/v2/types?context=edit',
} );
return Object.entries( postTypes ?? {} ).map( ( [ name, postType ] ) => {
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
Expand All @@ -285,6 +286,7 @@ async function loadPostTypeEntities() {
selection: true,
},
mergedEdits: { meta: true },
supports: postType?.supports,
rawAttributes: POST_RAW_ATTRIBUTES,
getTitle: ( record ) =>
record?.title?.rendered ||
Expand Down
8 changes: 6 additions & 2 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,8 @@ function entity( entityConfig ) {
( action ) =>
action.name &&
action.kind &&
action.name === entityConfig.name &&
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
action.name.split( ':' )[ 0 ] === entityConfig.name &&
action.kind === entityConfig.kind
),

Expand All @@ -245,7 +246,10 @@ function entity( entityConfig ) {
] )(
combineReducers( {
queriedData: queriedDataReducer,

// @TODO can this be filtered by supports above?
...( entityConfig?.supports?.revisions
? { revisions: queriedDataReducer }
: {} ),
edits: ( state = {}, action ) => {
switch ( action.type ) {
case 'RECEIVE_ITEMS':
Expand Down
67 changes: 59 additions & 8 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,17 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' );
*/
export const getEntityRecords =
( kind, name, query = {} ) =>
async ( { dispatch } ) => {
async ( { dispatch, select } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
const splitName = name.split( ':' )[ 0 ];

const entityConfig = configs.find(
( config ) => config.name === name && config.kind === kind
( config ) => config.name === splitName && config.kind === kind
);
const isRevisionEntityRecords =
entityConfig?.supports?.revisions &&
name.split( ':' )?.[ 2 ] === 'revisions';
if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand Down Expand Up @@ -221,12 +227,36 @@ export const getEntityRecords =
};
}

const path = addQueryArgs( entityConfig.baseURL, {
...entityConfig.baseURLParams,
...query,
} );
// @TODO Create predictable URL building rules for names like post:[key]:revisions.
// @TODO Possibly `entityConfig.getRevisionsUrl( { name } )?
let path, records;
if ( isRevisionEntityRecords ) {
const [ parentName, parentKey ] = name.split( ':' );
const parent = await select.getEntityRecord(
kind,
parentName,
parentKey
);
const revisionsURL =
parent?._links?.[ 'version-history' ]?.[ 0 ]?.href;
const url = addQueryArgs( revisionsURL, {
...{
// @TODO Default query params for revisions should be defined in the entity config?
context: 'view',
order: 'desc',
orderby: 'date',
},
...query,
} );
records = Object.values( await apiFetch( { url } ) );
} else {
path = addQueryArgs( entityConfig.baseURL, {
...entityConfig.baseURLParams,
...query,
} );
records = Object.values( await apiFetch( { path } ) );
}

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".
Expand All @@ -242,7 +272,28 @@ export const getEntityRecords =
} );
}

dispatch.receiveEntityRecords( kind, name, records, query );
// @TODO just dispatching here to send the default query params.
if ( isRevisionEntityRecords ) {
dispatch( {
type: 'RECEIVE_ITEMS',
kind,
name,
items: records,
query: {
...{
// @TODO Default query params for revisions should be defined in the entity config?
order: 'desc',
orderby: 'date',
},
...query,
},
invalidateCache: false,
} );
} else {
dispatch.receiveEntityRecords( kind, name, records, query );
}



// When requesting all fields, the list of results can be used to
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
Expand Down
15 changes: 13 additions & 2 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,22 @@ 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.
const queriedState =
state.entities.records?.[ kind ]?.[ name ]?.queriedData;
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
const splitName = name.split( ':' );
let queriedState = null;

if ( splitName?.[ 2 ] === 'revisions' ) {
queriedState =
state.entities.records?.[ kind ]?.[ splitName[ 0 ] ]?.revisions?.[
splitName?.[ 1 ]
];
} else {
queriedState = state.entities.records?.[ kind ]?.[ name ]?.queriedData;
}
if ( ! queriedState ) {
return null;
}
// @TODO this is not returning anything yet.
return getQueriedItems( queriedState, query );
} ) as GetEntityRecords;

Expand Down

0 comments on commit 0edea5d

Please sign in to comment.