diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 9748355fc5caf6..7666c3b07bd45f 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -418,7 +418,29 @@ export function entitiesConfig( state = rootEntitiesConfig, action ) { export const entities = ( state = {}, action ) => { const newConfig = entitiesConfig( state.config, action ); - // Generates a dynamic reducer for the entities. + // Generates a reducer for the entities nested by `kind` and `name`. + // A config array with shape: + // ``` + // [ + // { kind: 'taxonomy', name: 'category' }, + // { kind: 'taxonomy', name: 'post_tag' }, + // { kind: 'postType', name: 'post' }, + // { kind: 'postType', name: 'page' }, + // ] + // ``` + // generates a reducer for state tree with shape: + // ``` + // { + // taxonomy: { + // category, + // post_tag, + // }, + // postType: { + // post, + // page, + // }, + // } + // ``` let entitiesDataReducer = state.reducer; if ( ! entitiesDataReducer || newConfig !== state.config ) { const entitiesByKind = newConfig.reduce( ( acc, record ) => { @@ -431,22 +453,21 @@ export const entities = ( state = {}, action ) => { }, {} ); entitiesDataReducer = combineReducers( - Object.entries( entitiesByKind ).reduce( - ( memo, [ kind, subEntities ] ) => { - const kindReducer = combineReducers( - subEntities.reduce( - ( kindMemo, entityConfig ) => ( { - ...kindMemo, - [ entityConfig.name ]: entity( entityConfig ), - } ), - {} - ) - ); - - memo[ kind ] = kindReducer; - return memo; - }, - {} + Object.fromEntries( + Object.entries( entitiesByKind ).map( + ( [ kind, subEntities ] ) => { + const kindReducer = combineReducers( + Object.fromEntries( + subEntities.map( ( entityConfig ) => [ + entityConfig.name, + entity( entityConfig ), + ] ) + ) + ); + + return [ kind, kindReducer ]; + } + ) ) ); }