Skip to content

Commit

Permalink
Testing out a name parser
Browse files Browse the repository at this point in the history
Trying to invalidate revisions after saved entity
  • Loading branch information
ramonjd committed Sep 26, 2023
1 parent c2e687d commit 8702134
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 43 deletions.
6 changes: 6 additions & 0 deletions packages/core-data/src/entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,12 @@ async function loadPostTypeEntities() {
},
syncObjectType: 'postType/' + postType.name,
getSyncObjectId: ( id ) => id,
getRevisionsUrl: ( parentId, revisionId ) =>
`/${ namespace }/${
postType.rest_base
}/${ parentId }/revisions${
revisionId ? '/' + revisionId : ''
}`,
};
} );
}
Expand Down
39 changes: 12 additions & 27 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,7 @@ export const getEntityRecord =
if ( isRevisionEntityRecord ) {
const [ , parentKey ] = name.split( ':' );
path = addQueryArgs(
`${ entityConfig.baseURL }/${ parentKey }/revisions${
key ? '/' + key : ''
}`,
entityConfig.getRevisionsUrl( parentKey, key ),
{
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
context: 'view',
Expand Down Expand Up @@ -205,7 +203,6 @@ export const getEntityRecord =
context: 'view',
...query,
},
invalidateCache: false,
} );
} else {
dispatch.receiveEntityRecords( kind, name, record, query );
Expand Down Expand Up @@ -236,7 +233,7 @@ export const getEditedEntityRecord = forwardResolver( 'getEntityRecord' );
*/
export const getEntityRecords =
( kind, name, query = {} ) =>
async ( { dispatch, select } ) => {
async ( { dispatch } ) => {
const configs = await dispatch( getOrLoadEntitiesConfig( kind ) );
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
const splitName = name.split( ':' )[ 0 ];
Expand Down Expand Up @@ -281,7 +278,7 @@ export const getEntityRecords =
if ( isRevisionEntityRecords ) {
const [ , parentKey ] = name.split( ':' );
path = addQueryArgs(
`${ entityConfig.baseURL }/${ parentKey }/revisions`,
entityConfig.getRevisionsUrl( parentKey ),
{
// @TODO Default query params for revisions should be defined in the entity config?
order: 'desc',
Expand Down Expand Up @@ -317,20 +314,6 @@ export const getEntityRecords =

// @TODO just dispatching here to send the action type.
if ( isRevisionEntityRecords ) {
const [ postType, revisionParentKey ] = name.split( ':' );
const existingRecords = select.getEntityRecords(
'postType',
`${ postType }:${ revisionParentKey }:revisions`,
{
// @TODO Default query params for revisions should be defined in the entity config?
order: 'desc',
orderby: 'date',
// @TODO check if this is the default for revisions (should be view?). Is there anything else?
context: 'view',
...query,
}
);
console.log( 'existingRecords, records, shouldInvalidate?', existingRecords, records, existingRecords?.length !== records?.length );
dispatch( {
type: 'RECEIVE_ITEM_REVISIONS',
kind,
Expand All @@ -344,8 +327,8 @@ export const getEntityRecords =
context: 'view',
...query,
},
invalidateCache:
existingRecords?.length !== records?.length,
invalidateCache: true,

} );
} else {
dispatch.receiveEntityRecords( kind, name, records, query );
Expand Down Expand Up @@ -375,14 +358,16 @@ export const getEntityRecords =
dispatch.__unstableReleaseStoreLock( lock );
}
};
// @TODO work out how to invalidate revisions. At the moment, adding a new post revisions doesn't update the state without page refresh.

getEntityRecords.shouldInvalidate = ( action, kind, name ) => {
if ( action.type === 'RECEIVE_ITEM_REVISIONS' && name === action.name ) {
console.log( 'action', action, kind, name );
// Invalidate cache when a new revision is created.
if ( action.type === 'SAVE_ENTITY_RECORD_FINISH' ) {
const [ postType, recordId ] = name.split( ':' );
return (
action.invalidateCache &&
kind === action.kind &&
name === action.name
postType === action.name &&
! action.error &&
Number( recordId ) === action.recordId
);
}

Expand Down
37 changes: 21 additions & 16 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
getNormalizedCommaSeparable,
isRawAttribute,
setNestedValue,
parseEntityName,
} from './utils';
import type * as ET from './entity-types';
import type { UndoManager } from '@wordpress/undo-manager';
Expand Down Expand Up @@ -324,12 +325,15 @@ export const getEntityRecord = createSelector(
// @TODO this is a mess.
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
// @TODO update the resolver to fetch the revision item.
const splitName = name?.split( ':' );
const isRevision = splitName?.[ 2 ] === 'revisions';
const {
name: parsedName,
key: parsedKey,
isRevision,
} = parseEntityName( name );

const queriedState = isRevision
? state.entities.records?.[ kind ]?.[ splitName[ 0 ] ]?.revisions[
splitName[ 1 ]
? state.entities.records?.[ kind ]?.[ parsedName[ 0 ] ]?.revisions[
parsedKey
]
: state.entities.records?.[ kind ]?.[ name ]?.queriedData;
if ( ! queriedState ) {
Expand Down Expand Up @@ -374,9 +378,11 @@ export const getEntityRecord = createSelector(
return item;
} ) as GetEntityRecord,
( state: State, kind, name, recordId, query ) => {
// @TODO this is a mess.
// @TODO Create predictable parsing rules for names like post:[key]:revisions.
const splitName = name?.split( ':' );
const {
name: parsedName,
key: parsedKey,
isRevision,
} = parseEntityName( name );
const isRevision = splitName?.[ 2 ] === 'revisions';
const queryParams = isRevision
? {
Expand All @@ -390,15 +396,15 @@ export const getEntityRecord = createSelector(

return [
isRevision
? state.entities.records?.[ kind ]?.[ splitName[ 0 ] ]
?.revisions[ splitName[ 1 ] ]?.items[ recordId ]
? state.entities.records?.[ kind ]?.[ parsedName ]?.revisions[
parsedKey
]?.items[ recordId ]
: state.entities.records?.[ kind ]?.[ name ]?.queriedData
?.items[ context ]?.[ recordId ],
isRevision
? state.entities.records?.[ kind ]?.[ splitName[ 0 ] ]
?.revisions[ splitName[ 1 ] ]?.itemIsComplete[
context
]?.[ recordId ]
? state.entities.records?.[ kind ]?.[ parsedName ]?.revisions[
parsedKey
]?.itemIsComplete[ context ]?.[ recordId ]
: state.entities.records?.[ kind ]?.[ name ]?.queriedData
?.itemIsComplete[ context ]?.[ recordId ],
];
Expand Down Expand Up @@ -567,8 +573,7 @@ export const getEntityRecords = ( <
if ( ! queriedStateRevisions ) {
return null;
}
console.log( 'state.entities.records?.[ kind ]?.[ splitName[ 0 ] ]?.revisions', state.entities.records?.[ kind ]?.[ splitName[ 0 ] ]?.revisions );
const extraQueryParams = {
const defaultQueryParams = {
// @TODO Default query params for revisions should be defined in the entity config?
order: 'desc',
orderby: 'date',
Expand All @@ -578,7 +583,7 @@ export const getEntityRecords = ( <

return getQueriedItems( queriedStateRevisions, {
...query,
...extraQueryParams,
...defaultQueryParams,
} );
}

Expand Down
1 change: 1 addition & 0 deletions packages/core-data/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { default as withWeakMapCache } from './with-weak-map-cache';
export { default as isRawAttribute } from './is-raw-attribute';
export { default as setNestedValue } from './set-nested-value';
export { default as getNestedValue } from './get-nested-value';
export { default as parseEntityName } from './parse-entity-name';
9 changes: 9 additions & 0 deletions packages/core-data/src/utils/parse-entity-name.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export default function parseEntityName( name = '' ) {
const [ postType, key, revisions ] = name?.split( ':' );

return {
name: postType,
key,
isRevision: revisions === 'revisions',
};
}

0 comments on commit 8702134

Please sign in to comment.