From 21fcb958bba0ac19f5dc9aaa6816f36254f18898 Mon Sep 17 00:00:00 2001 From: ramon Date: Tue, 26 Sep 2023 11:47:13 +1000 Subject: [PATCH] - Moving revisions url params to config --- packages/core-data/src/entities.js | 1 + packages/core-data/src/resolvers.js | 89 ++++++++----------- packages/core-data/src/selectors.ts | 29 ++---- .../core-data/src/utils/parse-entity-name.js | 4 +- 4 files changed, 52 insertions(+), 71 deletions(-) diff --git a/packages/core-data/src/entities.js b/packages/core-data/src/entities.js index 32fc616b339dc..c940b4ce478bf 100644 --- a/packages/core-data/src/entities.js +++ b/packages/core-data/src/entities.js @@ -325,6 +325,7 @@ async function loadPostTypeEntities() { }/${ parentId }/revisions${ revisionId ? '/' + revisionId : '' }`, + revisionURLParams: { context: 'view' }, }; } ); } diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 210e289978daa..5fdab80df2898 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -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'; /** @@ -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; } @@ -83,7 +92,7 @@ export const getEntityRecord = if ( window.__experimentalEnableSync && entityConfig.syncConfig && - ! isRevisionEntityRecord && + ! isRevision && ! query ) { if ( process.env.IS_GUTENBERG_PLUGIN ) { @@ -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, } ); @@ -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, @@ -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 ); @@ -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; } @@ -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, } ); @@ -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 ); diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index 78f962a9212a8..75c0517e71955 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -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? @@ -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 = diff --git a/packages/core-data/src/utils/parse-entity-name.js b/packages/core-data/src/utils/parse-entity-name.js index 5e751469227d4..4cdc014266595 100644 --- a/packages/core-data/src/utils/parse-entity-name.js +++ b/packages/core-data/src/utils/parse-entity-name.js @@ -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,