Skip to content

Commit

Permalink
Core Data: Fix errors when the entities list doesn't contain config key
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka committed Jun 5, 2024
1 parent afb94b8 commit 0011ca6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions packages/core-data/src/queried-data/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ export function items( state = {}, action ) {
[ context ]: {
...state[ context ],
...action.items.reduce( ( accumulator, value ) => {
const itemId = value[ key ];
const itemId = value?.[ key ];
if ( ! itemId ) {
return accumulator;
}

accumulator[ itemId ] = conservativeMapItem(
state?.[ context ]?.[ itemId ],
value
Expand Down Expand Up @@ -164,7 +168,10 @@ export function itemIsComplete( state = {}, action ) {
[ context ]: {
...state[ context ],
...action.items.reduce( ( result, item ) => {
const itemId = item[ key ];
const itemId = item?.[ key ];
if ( ! itemId ) {
return result;
}

// Defer to completeness if already assigned. Technically the
// data may be outdated if receiving items for a field subset.
Expand Down Expand Up @@ -232,7 +239,7 @@ const receiveQueries = compose( [
return {
itemIds: getMergedItemIds(
state?.itemIds || [],
action.items.map( ( item ) => item[ key ] ),
action.items.flatMap( ( item ) => item?.[ key ] ?? [] ),
page,
perPage
),
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ function entity( entityConfig ) {
const nextState = { ...state };

for ( const record of action.items ) {
const recordId = record[ action.key ];
const recordId = record?.[ action.key ];
const edits = nextState[ recordId ];
if ( ! edits ) {
continue;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ export const getEntityRecords =
if ( ! query?._fields && ! query.context ) {
const key = entityConfig.key || DEFAULT_ENTITY_KEY;
const resolutionsArgs = records
.filter( ( record ) => record[ key ] )
.filter( ( record ) => record?.[ key ] )
.map( ( record ) => [ kind, name, record[ key ] ] );

dispatch( {
Expand Down

0 comments on commit 0011ca6

Please sign in to comment.