Skip to content

Commit

Permalink
Restore site entity shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Mar 18, 2024
1 parent 837b557 commit ae8e941
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 9 deletions.
7 changes: 6 additions & 1 deletion packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,12 @@ export const rootEntitiesConfig = [
export const additionalEntityConfigLoaders = [
{ kind: 'postType', loadEntities: loadPostTypeEntities },
{ kind: 'taxonomy', loadEntities: loadTaxonomyEntities },
{ kind: 'root', loadEntities: loadSiteEntity },
{
kind: 'root',
name: 'site',
plural: 'sites',
loadEntities: loadSiteEntity,
},
];

/**
Expand Down
16 changes: 12 additions & 4 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,24 @@ import * as privateSelectors from './private-selectors';
import * as actions from './actions';
import * as resolvers from './resolvers';
import createLocksActions from './locks/actions';
import { rootEntitiesConfig, getMethodName } from './entities';
import {
rootEntitiesConfig,
additionalEntityConfigLoaders,
getMethodName,
} from './entities';
import { STORE_NAME } from './name';
import { unlock } from './private-apis';

// The entity selectors/resolvers and actions are shortcuts to their generic equivalents
// (getEntityRecord, getEntityRecords, updateEntityRecord, updateEntityRecords)
// Instead of getEntityRecord, the consumer could use more user-friendly named selector: getPostType, getTaxonomy...
// The "kind" and the "name" of the entity are combined to generate these shortcuts.
const entitiesConfig = [
...rootEntitiesConfig,
...additionalEntityConfigLoaders.filter( ( config ) => !! config.name ),
];

const entitySelectors = rootEntitiesConfig.reduce( ( result, entity ) => {
const entitySelectors = entitiesConfig.reduce( ( result, entity ) => {
const { kind, name, plural } = entity;
result[ getMethodName( kind, name ) ] = ( state, key, query ) =>
selectors.getEntityRecord( state, kind, name, key, query );
Expand All @@ -33,7 +41,7 @@ const entitySelectors = rootEntitiesConfig.reduce( ( result, entity ) => {
return result;
}, {} );

const entityResolvers = rootEntitiesConfig.reduce( ( result, entity ) => {
const entityResolvers = entitiesConfig.reduce( ( result, entity ) => {
const { kind, name, plural } = entity;
result[ getMethodName( kind, name ) ] = ( key, query ) =>
resolvers.getEntityRecord( kind, name, key, query );
Expand All @@ -48,7 +56,7 @@ const entityResolvers = rootEntitiesConfig.reduce( ( result, entity ) => {
return result;
}, {} );

const entityActions = rootEntitiesConfig.reduce( ( result, entity ) => {
const entityActions = entitiesConfig.reduce( ( result, entity ) => {
const { kind, name } = entity;
result[ getMethodName( kind, name, 'save' ) ] = ( record, options ) =>
actions.saveEntityRecord( kind, name, record, options );
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/site-hub/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const SiteHub = memo( ( { isTransparent, className } ) => {
);

const {
getEntityRecord,
getSite,
getUnstableBase, // Site index.
} = select( coreStore );

Expand All @@ -50,7 +50,7 @@ const SiteHub = memo( ( { isTransparent, className } ) => {
dashboardLink:
getSettings().__experimentalDashboardLink || 'index.php',
homeUrl: getUnstableBase()?.home,
siteTitle: getEntityRecord( 'root', 'site' )?.title,
siteTitle: getSite()?.title,
};
},
[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ const postTypesWithoutParentTemplate = [
function useResolveEditedEntityAndContext( { path, postId, postType } ) {
const { hasLoadedAllDependencies, homepageId, url, frontPageTemplateId } =
useSelect( ( select ) => {
const { getEntityRecord, getUnstableBase, getEntityRecords } =
const { getSite, getUnstableBase, getEntityRecords } =
select( coreDataStore );
const siteData = getEntityRecord( 'root', 'site' );
const siteData = getSite();
const base = getUnstableBase();
const templates = getEntityRecords(
'postType',
Expand Down

0 comments on commit ae8e941

Please sign in to comment.