Skip to content

Commit

Permalink
Moving revisionKey to the entity config and use it in the action an…
Browse files Browse the repository at this point in the history
…d resolvers
  • Loading branch information
ramonjd committed Nov 22, 2023
1 parent 8ceae2f commit 1eb6e18
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 47 deletions.
6 changes: 1 addition & 5 deletions docs/reference-guides/data/data-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ _Returns_

### receiveRevisions

Returns an action object used in signalling that revisions have been received.
Action triggered to receive revision items.

_Parameters_

Expand All @@ -751,10 +751,6 @@ _Parameters_
- _invalidateCache_ `?boolean`: Should invalidate query caches.
- _meta_ `?Object`: Meta information about pagination.

_Returns_

- `Object`: Action object.

### receiveThemeSupports

> **Deprecated** since WP 5.9, this is not useful anymore, use the selector direclty.
Expand Down
6 changes: 1 addition & 5 deletions packages/core-data/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ _Returns_

### receiveRevisions

Returns an action object used in signalling that revisions have been received.
Action triggered to receive revision items.

_Parameters_

Expand All @@ -262,10 +262,6 @@ _Parameters_
- _invalidateCache_ `?boolean`: Should invalidate query caches.
- _meta_ `?Object`: Meta information about pagination.

_Returns_

- `Object`: Action object.

### receiveThemeSupports

> **Deprecated** since WP 5.9, this is not useful anymore, use the selector direclty.
Expand Down
48 changes: 25 additions & 23 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ export function receiveDefaultTemplateId( query, templateId ) {
}

/**
* Returns an action object used in signalling that revisions have been received.
* Action triggered to receive revision items.
*
* @param {string} kind Kind of the received entity record revisions.
* @param {string} name Name of the received entity record revisions.
Expand All @@ -935,27 +935,29 @@ export function receiveDefaultTemplateId( query, templateId ) {
* @param {?Object} query Query Object.
* @param {?boolean} invalidateCache Should invalidate query caches.
* @param {?Object} meta Meta information about pagination.
* @return {Object} Action object.
*/
export function receiveRevisions(
kind,
name,
recordKey,
records,
query,
invalidateCache = false,
meta
) {
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes( name );
return {
type: 'RECEIVE_ITEM_REVISIONS',
key: isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY,
items: Array.isArray( records ) ? records : [ records ],
recordKey,
meta,
query,
kind,
name,
invalidateCache,

export const receiveRevisions =
( kind, name, recordKey, records, query, invalidateCache = false, meta ) =>
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
const entityConfig = configs.find(
( config ) => config.kind === kind && config.name === name
);
const key =
entityConfig && entityConfig?.revisionKey
? entityConfig.revisionKey
: DEFAULT_ENTITY_KEY;

dispatch( {
type: 'RECEIVE_ITEM_REVISIONS',
key,
items: Array.isArray( records ) ? records : [ records ],
recordKey,
meta,
query,
kind,
name,
invalidateCache,
} );
};
}
1 change: 1 addition & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ async function loadPostTypeEntities() {
}/${ parentId }/revisions${
revisionId ? '/' + revisionId : ''
}`,
revisionKey: isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY,
};
} );
}
Expand Down
6 changes: 0 additions & 6 deletions packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,12 +377,6 @@ function entity( entityConfig ) {
}

if ( action.type === 'REMOVE_ITEMS' ) {
/*
For templates;
itemIds: ['twentytwentyfour//wp-custom-template-nag']
But the state key is with one slash: twentytwentyfour/wp-custom-template-nag
So deleteEntityRecord needs send extra data to removeItems?
*/
return Object.fromEntries(
Object.entries( state ).filter(
( [ id ] ) =>
Expand Down
12 changes: 4 additions & 8 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,7 @@ export const getRevisions =
) {
return;
}
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
name
);

if ( query._fields ) {
// If requesting specific fields, items and query association to said
// records are stored by ID reference. Thus, fields must always include
Expand All @@ -757,7 +755,7 @@ export const getRevisions =
...new Set( [
...( getNormalizedCommaSeparable( query._fields ) ||
[] ),
isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY,
entityConfig.revisionKey || DEFAULT_ENTITY_KEY,
] ),
].join(),
};
Expand Down Expand Up @@ -839,9 +837,7 @@ export const getRevision =
) {
return;
}
const isTemplate = [ 'wp_template', 'wp_template_part' ].includes(
name
);

if ( query !== undefined && query._fields ) {
// If requesting specific fields, items and query association to said
// records are stored by ID reference. Thus, fields must always include
Expand All @@ -852,7 +848,7 @@ export const getRevision =
...new Set( [
...( getNormalizedCommaSeparable( query._fields ) ||
[] ),
isTemplate ? 'wp_id' : DEFAULT_ENTITY_KEY,
entityConfig.revisionKey || DEFAULT_ENTITY_KEY,
] ),
].join(),
};
Expand Down

0 comments on commit 1eb6e18

Please sign in to comment.