diff --git a/docs/reference-guides/data/data-core.md b/docs/reference-guides/data/data-core.md index eda4499f9e78b..199c29cd67dd2 100644 --- a/docs/reference-guides/data/data-core.md +++ b/docs/reference-guides/data/data-core.md @@ -279,21 +279,6 @@ _Returns_ - `ET.Updatable< EntityRecord > | false`: The entity record, merged with its edits. -### getEditedEntityRecords - -Returns a list of entity records, merged with their edits. - -_Parameters_ - -- _state_ `State`: State tree. -- _kind_ `string`: Entity kind. -- _name_ `string`: Entity name. -- _recordIds_ `EntityRecordKey[]`: Record IDs. - -_Returns_ - -- `Array< ET.Updatable< EntityRecord > | false >`: The list of entity records, merged with their edits. - ### getEmbedPreview Returns the embed preview for the given URL. diff --git a/packages/core-data/README.md b/packages/core-data/README.md index bf66993d16b78..9549e6742d8cd 100644 --- a/packages/core-data/README.md +++ b/packages/core-data/README.md @@ -501,21 +501,6 @@ _Returns_ - `ET.Updatable< EntityRecord > | false`: The entity record, merged with its edits. -### getEditedEntityRecords - -Returns a list of entity records, merged with their edits. - -_Parameters_ - -- _state_ `State`: State tree. -- _kind_ `string`: Entity kind. -- _name_ `string`: Entity name. -- _recordIds_ `EntityRecordKey[]`: Record IDs. - -_Returns_ - -- `Array< ET.Updatable< EntityRecord > | false >`: The list of entity records, merged with their edits. - ### getEmbedPreview Returns the embed preview for the given URL. diff --git a/packages/core-data/src/private-selectors.ts b/packages/core-data/src/private-selectors.ts index 0d4a28ad174a1..31c06f4f2073d 100644 --- a/packages/core-data/src/private-selectors.ts +++ b/packages/core-data/src/private-selectors.ts @@ -6,9 +6,16 @@ import { createSelector, createRegistrySelector } from '@wordpress/data'; /** * Internal dependencies */ -import { getDefaultTemplateId, getEntityRecord, type State } from './selectors'; +import { + getDefaultTemplateId, + getEditedEntityRecord, + getEntityRecord, + type State, + type GetRecordsHttpQuery, +} from './selectors'; import { STORE_NAME } from './name'; import { unlock } from './lock-unlock'; +import type * as ET from './entity-types'; type EntityRecordKey = string | number; @@ -257,3 +264,54 @@ export const getTemplateId = createRegistrySelector( } ); } ); + +/** + * Returns a list of entity records, merged with their edits. + * + * @param state State tree. + * @param kind Entity kind. + * @param name Entity name. + * @param recordIds Record IDs. + * + * @return The list of entity records, merged with their edits. + */ +export const getEditedEntityRecords = createSelector( + < EntityRecord extends ET.EntityRecord< any > >( + state: State, + kind: string, + name: string, + recordIds: EntityRecordKey[] + ): Array< ET.Updatable< EntityRecord > | false > => { + return recordIds.map( ( recordId ) => + getEditedEntityRecord( state, kind, name, recordId ) + ); + }, + ( + state: State, + kind: string, + name: string, + recordIds: EntityRecordKey[], + query?: GetRecordsHttpQuery + ) => { + const context = query?.context ?? 'default'; + return [ + state.entities.config, + ...recordIds.map( + ( recordId ) => + state.entities.records?.[ kind ]?.[ name ]?.queriedData + .items[ context ]?.[ recordId ] + ), + ...recordIds.map( + ( recordId ) => + state.entities.records?.[ kind ]?.[ name ]?.queriedData + .itemIsComplete[ context ]?.[ recordId ] + ), + ...recordIds.map( + ( recordId ) => + state.entities.records?.[ kind ]?.[ name ]?.edits?.[ + recordId + ] + ), + ]; + } +); diff --git a/packages/core-data/src/selectors.ts b/packages/core-data/src/selectors.ts index d4ed44d6e0988..c31ebc0425464 100644 --- a/packages/core-data/src/selectors.ts +++ b/packages/core-data/src/selectors.ts @@ -113,7 +113,7 @@ type Optional< T > = T | undefined; /** * HTTP Query parameters sent with the API request to fetch the entity records. */ -type GetRecordsHttpQuery = Record< string, any >; +export type GetRecordsHttpQuery = Record< string, any >; /** * Arguments for EntityRecord selectors. @@ -891,57 +891,6 @@ export const getEditedEntityRecord = createSelector( } ); -/** - * Returns a list of entity records, merged with their edits. - * - * @param state State tree. - * @param kind Entity kind. - * @param name Entity name. - * @param recordIds Record IDs. - * - * @return The list of entity records, merged with their edits. - */ -export const getEditedEntityRecords = createSelector( - < EntityRecord extends ET.EntityRecord< any > >( - state: State, - kind: string, - name: string, - recordIds: EntityRecordKey[] - ): Array< ET.Updatable< EntityRecord > | false > => { - return recordIds.map( ( recordId ) => - getEditedEntityRecord( state, kind, name, recordId ) - ); - }, - ( - state: State, - kind: string, - name: string, - recordIds: EntityRecordKey[], - query?: GetRecordsHttpQuery - ) => { - const context = query?.context ?? 'default'; - return [ - state.entities.config, - ...recordIds.map( - ( recordId ) => - state.entities.records?.[ kind ]?.[ name ]?.queriedData - .items[ context ]?.[ recordId ] - ), - ...recordIds.map( - ( recordId ) => - state.entities.records?.[ kind ]?.[ name ]?.queriedData - .itemIsComplete[ context ]?.[ recordId ] - ), - ...recordIds.map( - ( recordId ) => - state.entities.records?.[ kind ]?.[ name ]?.edits?.[ - recordId - ] - ), - ]; - } -); - /** * Returns true if the specified entity record is autosaving, and false otherwise. *