Skip to content

Commit

Permalink
Mwoar clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 28, 2023
1 parent 8086da2 commit 4afe96f
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 72 deletions.
10 changes: 8 additions & 2 deletions packages/core-data/src/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import deprecated from '@wordpress/deprecated';
/**
* Internal dependencies
*/
import { getNestedValue, setNestedValue } from './utils';
import { getNestedValue, setNestedValue, parseEntityName } from './utils';
import { receiveItems, removeItems, receiveQueriedItems } from './queried-data';
import { getOrLoadEntitiesConfig, DEFAULT_ENTITY_KEY } from './entities';
import { createBatch } from './batch';
Expand Down Expand Up @@ -90,9 +90,11 @@ export function receiveEntityRecords(
invalidateCache = false,
edits
) {
const { isRevision } = parseEntityName( name );

// Auto drafts should not have titles, but some plugins rely on them so we can't filter this
// on the server.
if ( kind === 'postType' ) {
if ( ! isRevision && kind === 'postType' ) {
records = ( Array.isArray( records ) ? records : [ records ] ).map(
( record ) =>
record.status === 'auto-draft'
Expand All @@ -107,6 +109,10 @@ export function receiveEntityRecords(
action = receiveItems( records, edits );
}

if ( isRevision ) {
action.type = 'RECEIVE_ITEM_REVISIONS';
}

return {
...action,
kind,
Expand Down
5 changes: 3 additions & 2 deletions packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ifMatchingAction,
replaceAction,
onSubKey,
parseEntityName,
} from '../utils';
import { DEFAULT_ENTITY_KEY } from '../entities';
import getQueryParts from './get-query-parts';
Expand Down Expand Up @@ -406,10 +407,10 @@ const revisionsReducer = combineReducers( {
export const revisionsQueriedDataReducer = ( state = {}, action ) => {
switch ( action.type ) {
case 'RECEIVE_ITEM_REVISIONS':
const parent = action.name.split( ':' )[ 1 ];
const { key: parentId } = parseEntityName( action.name );
return {
...state,
[ parent ]: revisionsReducer( state[ parent ], action ),
[ parentId ]: revisionsReducer( state[ parentId ], action ),
};
default:
return state;
Expand Down
92 changes: 24 additions & 68 deletions packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,33 +148,16 @@ export const getEntityRecord =
};
}

// Disable reason: While true that an early return could leave `path`
// unused, it's important that path is derived using the query prior to
// additional query modifications in the condition below, since those
// modifications are relevant to how the data is tracked in state, and not
// for how the request is made to the REST API.

// @TODO this is a mess.
// @TODO Create predictable URL building rules for names like post:[key]:revisions.
// @TODO Possibly `entityConfig.getRevisionsUrl( { name } )?
let path;
if ( isRevision ) {
path = addQueryArgs(
entityConfig.getRevisionsUrl( parsedKey, key ),
{
...entityConfig.revisionURLParams,
...query,
}
);
} else {
path = addQueryArgs(
entityConfig.baseURL + ( key ? '/' + key : '' ),
{
...entityConfig.baseURLParams,
...query,
}
);
}
const baseUrl = isRevision
? entityConfig.getRevisionsUrl( parsedKey, key )
: entityConfig.baseURL + ( key ? '/' + key : '' );

const path = addQueryArgs( baseUrl, {
...( isRevision
? entityConfig.revisionURLParams
: entityConfig.baseURLParams ),
...query,
} );

if ( query !== undefined ) {
query = { ...query, include: [ key ] };
Expand All @@ -196,18 +179,7 @@ export const getEntityRecord =
}

const record = await apiFetch( { path } );
// @TODO just dispatching here to send the action type.
if ( isRevision ) {
dispatch( {
type: 'RECEIVE_ITEM_REVISIONS',
kind,
name,
items: [ record ],
query,
} );
} else {
dispatch.receiveEntityRecords( kind, name, record, query );
}
dispatch.receiveEntityRecords( kind, name, record, query );
}
} finally {
dispatch.__unstableReleaseStoreLock( lock );
Expand Down Expand Up @@ -241,7 +213,6 @@ export const getEntityRecords =
key: parsedKey,
isRevision,
} = parseEntityName( name );

const entityConfig = configs.find(
( config ) => config.name === parsedName && config.kind === kind
);
Expand Down Expand Up @@ -277,21 +248,16 @@ export const getEntityRecords =
};
}

let path;
if ( isRevision ) {
path = addQueryArgs(
entityConfig.getRevisionsUrl( parsedKey ),
{
...entityConfig.revisionURLParams,
...query,
}
);
} else {
path = addQueryArgs( entityConfig.baseURL, {
...entityConfig.baseURLParams,
...query,
} );
}
const baseUrl = isRevision
? entityConfig.getRevisionsUrl( parsedKey )
: entityConfig.baseURL;

const path = addQueryArgs( baseUrl, {
...( isRevision
? entityConfig.revisionURLParams
: entityConfig.baseURLParams ),
...query,
} );

let records = Object.values( await apiFetch( { path } ) );

Expand All @@ -310,23 +276,12 @@ export const getEntityRecords =
} );
}

// @TODO just dispatching here to send the action type.
if ( isRevision ) {
dispatch( {
type: 'RECEIVE_ITEM_REVISIONS',
kind,
name,
items: records,
query,
} );
} else {
dispatch.receiveEntityRecords( kind, name, records, query );
}
dispatch.receiveEntityRecords( kind, name, records, query );

// When requesting all fields, the list of results can be used to
// resolve the `getEntityRecord` selector in addition to `getEntityRecords`.
// See https://github.com/WordPress/gutenberg/pull/26575
if ( ! query?._fields && ! query.context ) {
if ( ! isRevision && ! query?._fields && ! query.context ) {
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
Expand All @@ -350,6 +305,7 @@ export const getEntityRecords =

getEntityRecords.shouldInvalidate = ( action, kind, name ) => {
// Invalidate cache when a new revision is created.
// @TODO This is not right, we should invalidate the cache via action.invalidateCache before the next call.
if ( action.type === 'SAVE_ENTITY_RECORD_FINISH' ) {
const [ postType, recordId ] = name.split( ':' );
return (
Expand Down
2 changes: 2 additions & 0 deletions packages/core-data/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,13 @@ export const getEntityRecords = ( <
key: parsedKey,
isRevision,
} = parseEntityName( name );

if ( isRevision ) {
const queriedStateRevisions =
state.entities.records?.[ kind ]?.[ parsedName ]?.revisions[
parsedKey
];

if ( ! queriedStateRevisions ) {
return null;
}
Expand Down

0 comments on commit 4afe96f

Please sign in to comment.