Skip to content

Commit

Permalink
- Moving revisions url params to config
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 26, 2023
1 parent 8702134 commit 21fcb95
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 71 deletions.
1 change: 1 addition & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,7 @@ async function loadPostTypeEntities() {
}/${ parentId }/revisions${
revisionId ? '/' + revisionId : ''
}`,
revisionURLParams: { context: 'view' },
};
} );
}
Expand Down
89 changes: 39 additions & 50 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@ import apiFetch from '@wordpress/api-fetch';
*/
import { STORE_NAME } from './name';
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
import { forwardResolver, getNormalizedCommaSeparable } from './utils';
import {
forwardResolver,
getNormalizedCommaSeparable,
parseEntityName,
} from './utils';
import { getSyncProvider } from './sync';

/**
Expand Down Expand Up @@ -58,14 +62,19 @@ export const getEntityRecord =
( kind, name, key = '', query ) =>
async ( { select, dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
const splitName = name.split( ':' )[ 0 ];
const {
name: parsedName,
key: parsedKey,
isRevision,
} = parseEntityName( name );
const entityConfig = configs.find(
( config ) => config.name === splitName && config.kind === kind
( config ) => config.name === parsedName && config.kind === kind
);
const isRevisionEntityRecord =
entityConfig?.supports?.revisions &&
name.split( ':' )?.[ 2 ] === 'revisions';

if ( isRevision && ! entityConfig?.supports?.revisions ) {
return;
}

if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand All @@ -83,7 +92,7 @@ export const getEntityRecord =
if (
window.__experimentalEnableSync &&
entityConfig.syncConfig &&
! isRevisionEntityRecord &&
! isRevision &&
! query
) {
if ( process.env.IS_GUTENBERG_PLUGIN ) {
Expand Down Expand Up @@ -149,13 +158,11 @@ export const getEntityRecord =
// @TODO Create predictable URL building rules for names like post:[key]:revisions.
// @TODO Possibly `entityConfig.getRevisionsUrl( { name } )?
let path;
if ( isRevisionEntityRecord ) {
const [ , parentKey ] = name.split( ':' );
if ( isRevision ) {
path = addQueryArgs(
entityConfig.getRevisionsUrl( parentKey, key ),
entityConfig.getRevisionsUrl( parsedKey, key ),
{
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
context: 'view',
...entityConfig.revisionURLParams,
...query,
}
);
Expand All @@ -170,9 +177,7 @@ export const getEntityRecord =
}

if ( query !== undefined ) {
query = isRevisionEntityRecord
? { context: 'view', ...query, include: [ key ] }
: { ...query, include: [ key ] };
query = { ...query, include: [ key ] };

// The resolution cache won't consider query as reusable based on the
// fields, so it's tested here, prior to initiating the REST request,
Expand All @@ -192,17 +197,13 @@ export const getEntityRecord =

const record = await apiFetch( { path } );
// @TODO just dispatching here to send the action type.
if ( isRevisionEntityRecord ) {
if ( isRevision ) {
dispatch( {
type: 'RECEIVE_ITEM_REVISIONS',
kind,
name,
items: [ record ],
query: {
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
context: 'view',
...query,
},
query,
} );
} else {
dispatch.receiveEntityRecords( kind, name, record, query );
Expand Down Expand Up @@ -235,15 +236,20 @@ export const getEntityRecords =
( kind, name, query = {} ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
const splitName = name.split( ':' )[ 0 ];
const {
name: parsedName,
key: parsedKey,
isRevision,
} = parseEntityName( name );

const entityConfig = configs.find(
( config ) => config.name === splitName && config.kind === kind
( config ) => config.name === parsedName && config.kind === kind
);
const isRevisionEntityRecords =
entityConfig?.supports?.revisions &&
name.split( ':' )?.[ 2 ] === 'revisions';

if ( isRevision && ! entityConfig?.supports?.revisions ) {
return;
}

if ( ! entityConfig || entityConfig?.__experimentalNoFetch ) {
return;
}
Expand Down Expand Up @@ -271,20 +277,12 @@ export const getEntityRecords =
};
}

// @TODO this is a mess.
// @TODO Create predictable URL building rules for names like post:[key]:revisions.
// @TODO Possibly `entityConfig.getRevisionsUrl( { name } )?
let path;
if ( isRevisionEntityRecords ) {
const [ , parentKey ] = name.split( ':' );
if ( isRevision ) {
path = addQueryArgs(
entityConfig.getRevisionsUrl( parentKey ),
entityConfig.getRevisionsUrl( parsedKey ),
{
// @TODO Default query params for revisions should be defined in the entity config?
order: 'desc',
orderby: 'date',
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
context: 'view',
...entityConfig.revisionURLParams,
...query,
}
);
Expand Down Expand Up @@ -313,22 +311,13 @@ export const getEntityRecords =
}

// @TODO just dispatching here to send the action type.
if ( isRevisionEntityRecords ) {
if ( isRevision ) {
dispatch( {
type: 'RECEIVE_ITEM_REVISIONS',
kind,
name,
items: records,
query: {
// @TODO Default query params for revisions should be defined in the entity config?
order: 'desc',
orderby: 'date',
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
context: 'view',
...query,
},
invalidateCache: true,

query,
} );
} else {
dispatch.receiveEntityRecords( kind, name, records, query );
Expand Down
29 changes: 9 additions & 20 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,6 @@ export const getEntityRecord = createSelector(
key: parsedKey,
isRevision,
} = parseEntityName( name );
const isRevision = splitName?.[ 2 ] === 'revisions';
const queryParams = isRevision
? {
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
Expand Down Expand Up @@ -561,30 +560,20 @@ 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.
// @TODO this is a mess.
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
const splitName = name?.split( ':' );
if ( splitName?.[ 2 ] === 'revisions' ) {
const {
name: parsedName,
key: parsedKey,
isRevision,
} = parseEntityName( name );
if ( isRevision ) {
const queriedStateRevisions =
state.entities.records?.[ kind ]?.[ splitName[ 0 ] ]?.revisions[
splitName[ 1 ]
state.entities.records?.[ kind ]?.[ parsedName ]?.revisions[
parsedKey
];

if ( ! queriedStateRevisions ) {
return null;
}
const defaultQueryParams = {
// @TODO Default query params for revisions should be defined in the entity config?
order: 'desc',
orderby: 'date',
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
context: 'view',
};

return getQueriedItems( queriedStateRevisions, {
...query,
...defaultQueryParams,
} );
return getQueriedItems( queriedStateRevisions, query );
}

const queriedState =
Expand Down
4 changes: 3 additions & 1 deletion packages/core-data/src/utils/parse-entity-name.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export default function parseEntityName( name = '' ) {
const [ postType, key, revisions ] = name?.split( ':' );
const [ postType, key, revisions ] = (
typeof name === 'string' ? name : ''
)?.split( ':' );

return {
name: postType,
Expand Down

0 comments on commit 21fcb95

Please sign in to comment.