Skip to content
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: harmonize getRevision selector and resolver function signatures #56416

Merged
merged 4 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ export const rootEntitiesConfig = [
kind: 'root',
baseURL: '/wp/v2/global-styles',
baseURLParams: { context: 'edit' },
plural: 'globalStylesVariations', // Should be different than name.
plural: 'globalStylesVariations', // Should be different from name.
getTitle: ( record ) => record?.title?.rendered || record?.title,
getRevisionsUrl: ( parentId ) =>
`/wp/v2/global-styles/${ parentId }/revisions`,
getRevisionsUrl: ( parentId, revisionId ) =>
`/wp/v2/global-styles/${ parentId }/revisions${
revisionId ? '/' + revisionId : ''
}`,
supports: {
revisions: true,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export function getMergedItemIds( itemIds, nextItemIds, page, perPage ) {
for ( let i = 0; i < size; i++ ) {
// Preserve existing item ID except for subset of range of next items.
// We need to check against the possible maximum upper boundary because
// a page could recieve less items than what was previously stored.
// a page could receive fewer than what was previously stored.
const isInNextItemsRange =
i >= nextItemIdsStartIndex && i < nextItemIdsStartIndex + perPage;
mergedItemIds[ i ] = isInNextItemsRange
Expand Down
22 changes: 21 additions & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,26 @@ export const getRevisions =
false,
meta
);

// When requesting all fields, the list of results can be used to
// resolve the `getRevision` selector in addition to `getRevisions`.
if ( ! query?._fields && ! query.context ) {
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
.map( ( record ) => [ kind, name, recordKey, record[ key ] ] );

dispatch( {
type: 'START_RESOLUTIONS',
selectorName: 'getRevision',
args: resolutionsArgs,
} );
dispatch( {
type: 'FINISH_RESOLUTIONS',
selectorName: 'getRevision',
args: resolutionsArgs,
} );
}
};

// Invalidate cache when a new revision is created.
Expand All @@ -823,7 +843,7 @@ getRevisions.shouldInvalidate = ( action, kind, name, recordKey ) =>
* fields, fields must always include the ID.
*/
export const getRevision =
( kind, name, recordKey, revisionKey, query = {} ) =>
( kind, name, recordKey, revisionKey, query ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
const entityConfig = configs.find(
Expand Down
Loading