Skip to content

Commit

Permalink
Core Data: Update method generating plural names (#59881)
Browse files Browse the repository at this point in the history
* Core Data: Update method generating plural names

* Remove plural flag
  • Loading branch information
Mamaduka authored Mar 18, 2024
1 parent 9289b2e commit 6aaa368
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 35 deletions.
30 changes: 11 additions & 19 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const rootEntitiesConfig = [
'url',
].join( ',' ),
},
plural: '__unstableBases',
syncConfig: {
fetch: async () => {
return apiFetch( { path: '/' } );
Expand All @@ -63,6 +64,7 @@ export const rootEntitiesConfig = [
name: 'site',
kind: 'root',
baseURL: '/wp/v2/settings',
plural: 'sites',
getTitle: ( record ) => {
return record?.title ?? __( 'Site Title' );
},
Expand Down Expand Up @@ -92,6 +94,7 @@ export const rootEntitiesConfig = [
key: 'slug',
baseURL: '/wp/v2/types',
baseURLParams: { context: 'edit' },
plural: 'postTypes',
syncConfig: {
fetch: async ( id ) => {
return apiFetch( {
Expand Down Expand Up @@ -220,6 +223,7 @@ export const rootEntitiesConfig = [
kind: 'root',
baseURL: '/wp/v2/themes',
baseURLParams: { context: 'edit' },
plural: 'themes',
key: 'stylesheet',
},
{
Expand All @@ -228,6 +232,7 @@ export const rootEntitiesConfig = [
kind: 'root',
baseURL: '/wp/v2/plugins',
baseURLParams: { context: 'edit' },
plural: 'plugins',
key: 'plugin',
},
{
Expand Down Expand Up @@ -408,32 +413,19 @@ async function loadTaxonomyEntities() {
* const nameSingular = getMethodName( 'root', 'theme', 'get' );
* // nameSingular is getRootTheme
*
* const namePlural = getMethodName( 'root', 'theme', 'set' );
* const namePlural = getMethodName( 'root', 'themes', 'set' );
* // namePlural is setRootThemes
* ```
*
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {string} prefix Function prefix.
* @param {boolean} usePlural Whether to use the plural form or not.
* @param {string} kind Entity kind.
* @param {string} name Entity name.
* @param {string} prefix Function prefix.
*
* @return {string} Method name
*/
export const getMethodName = (
kind,
name,
prefix = 'get',
usePlural = false
) => {
const entityConfig = rootEntitiesConfig.find(
( config ) => config.kind === kind && config.name === name
);
export const getMethodName = ( kind, name, prefix = 'get' ) => {
const kindPrefix = kind === 'root' ? '' : pascalCase( kind );
const nameSuffix = pascalCase( name ) + ( usePlural ? 's' : '' );
const suffix =
usePlural && 'plural' in entityConfig && entityConfig?.plural
? pascalCase( entityConfig.plural )
: nameSuffix;
const suffix = pascalCase( name );
return `${ prefix }${ kindPrefix }${ suffix }`;
};

Expand Down
24 changes: 15 additions & 9 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,29 @@ import { unlock } from './private-apis';
// The "kind" and the "name" of the entity are combined to generate these shortcuts.

const entitySelectors = rootEntitiesConfig.reduce( ( result, entity ) => {
const { kind, name } = entity;
const { kind, name, plural } = entity;
result[ getMethodName( kind, name ) ] = ( state, key, query ) =>
selectors.getEntityRecord( state, kind, name, key, query );
result[ getMethodName( kind, name, 'get', true ) ] = ( state, query ) =>
selectors.getEntityRecords( state, kind, name, query );

if ( plural ) {
result[ getMethodName( kind, plural, 'get' ) ] = ( state, query ) =>
selectors.getEntityRecords( state, kind, name, query );
}
return result;
}, {} );

const entityResolvers = rootEntitiesConfig.reduce( ( result, entity ) => {
const { kind, name } = entity;
const { kind, name, plural } = entity;
result[ getMethodName( kind, name ) ] = ( key, query ) =>
resolvers.getEntityRecord( kind, name, key, query );
const pluralMethodName = getMethodName( kind, name, 'get', true );
result[ pluralMethodName ] = ( ...args ) =>
resolvers.getEntityRecords( kind, name, ...args );
result[ pluralMethodName ].shouldInvalidate = ( action ) =>
resolvers.getEntityRecords.shouldInvalidate( action, kind, name );

if ( plural ) {
const pluralMethodName = getMethodName( kind, plural, 'get' );
result[ pluralMethodName ] = ( ...args ) =>
resolvers.getEntityRecords( kind, name, ...args );
result[ pluralMethodName ].shouldInvalidate = ( action ) =>
resolvers.getEntityRecords.shouldInvalidate( action, kind, name );
}
return result;
}, {} );

Expand Down
8 changes: 1 addition & 7 deletions packages/core-data/src/test/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,8 @@ describe( 'getMethodName', () => {
expect( methodName ).toEqual( 'setPostType' );
} );

it( 'should use the plural form', () => {
const methodName = getMethodName( 'root', 'postType', 'get', true );

expect( methodName ).toEqual( 'getPostTypes' );
} );

it( 'should use the given plural form', () => {
const methodName = getMethodName( 'root', 'taxonomy', 'get', true );
const methodName = getMethodName( 'root', 'taxonomies', 'get' );

expect( methodName ).toEqual( 'getTaxonomies' );
} );
Expand Down

0 comments on commit 6aaa368

Please sign in to comment.