-
Notifications
You must be signed in to change notification settings - Fork 4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Core Data: Update method generating plural names #59881
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,6 +39,7 @@ export const rootEntitiesConfig = [ | |
'url', | ||
].join( ',' ), | ||
}, | ||
plural: '__unstableBases', | ||
syncConfig: { | ||
fetch: async () => { | ||
return apiFetch( { path: '/' } ); | ||
|
@@ -63,6 +64,7 @@ export const rootEntitiesConfig = [ | |
name: 'site', | ||
kind: 'root', | ||
baseURL: '/wp/v2/settings', | ||
plural: 'sites', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here it seems this entity doesn't really need plural. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Needed for BC as well. |
||
getTitle: ( record ) => { | ||
return record?.title ?? __( 'Site Title' ); | ||
}, | ||
|
@@ -92,6 +94,7 @@ export const rootEntitiesConfig = [ | |
key: 'slug', | ||
baseURL: '/wp/v2/types', | ||
baseURLParams: { context: 'edit' }, | ||
plural: 'postTypes', | ||
syncConfig: { | ||
fetch: async ( id ) => { | ||
return apiFetch( { | ||
|
@@ -220,6 +223,7 @@ export const rootEntitiesConfig = [ | |
kind: 'root', | ||
baseURL: '/wp/v2/themes', | ||
baseURLParams: { context: 'edit' }, | ||
plural: 'themes', | ||
key: 'stylesheet', | ||
}, | ||
{ | ||
|
@@ -228,6 +232,7 @@ export const rootEntitiesConfig = [ | |
kind: 'root', | ||
baseURL: '/wp/v2/plugins', | ||
baseURLParams: { context: 'edit' }, | ||
plural: 'plugins', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we somehow guarantee that if we add a new entity in the future it will also have a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not a required property. Maybe converting the file to TS and adding proper types could help us here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we keep the |
||
key: 'plugin', | ||
}, | ||
{ | ||
|
@@ -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' ) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So the "name" here is actually not the name of the entity but a plural form already. It was not clear to me when reading the diff and may confuse people. Maybe we should rename the argument? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is more like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JSDoc would be helpful yes |
||
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 ); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neat simplification here 👍 |
||
return `${ prefix }${ kindPrefix }${ suffix }`; | ||
}; | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this check necessary? Don't all root entities have There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess Riad answered most of my question here - we intentionally don't want plural support for some of the entities. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I mentioned this in the description, but I probably should've added an inline comment as well. Not every entity will have "plural" selectors. Current cases are |
||
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 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above. |
||
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; | ||
}, {} ); | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this for BC? because this particular entity doesn't support multiple records.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. This is for BC. I mentioned this in the PR description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ooops, sorry :P
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it could use an extra comment that explains why we need it then.