From 794add14d405833a4199776e2f0cace601228b4c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 5 Jun 2024 22:42:39 +0400 Subject: [PATCH 1/3] Core Data: Fix errors when the entities list doesn't contain config key --- packages/core-data/src/queried-data/reducer.js | 13 ++++++++++--- packages/core-data/src/reducer.js | 2 +- packages/core-data/src/resolvers.js | 2 +- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/packages/core-data/src/queried-data/reducer.js b/packages/core-data/src/queried-data/reducer.js index 1f9bb64c7889d..7fc37f6b02851 100644 --- a/packages/core-data/src/queried-data/reducer.js +++ b/packages/core-data/src/queried-data/reducer.js @@ -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 @@ -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. @@ -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 ), diff --git a/packages/core-data/src/reducer.js b/packages/core-data/src/reducer.js index 8e6be42524468..3d6473d761036 100644 --- a/packages/core-data/src/reducer.js +++ b/packages/core-data/src/reducer.js @@ -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; diff --git a/packages/core-data/src/resolvers.js b/packages/core-data/src/resolvers.js index 3e5373eda6d6a..167b8ac052fe4 100644 --- a/packages/core-data/src/resolvers.js +++ b/packages/core-data/src/resolvers.js @@ -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( { From 3748d71a5a979bd172876294b35aa732cc3bed83 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 6 Jun 2024 07:52:26 +0400 Subject: [PATCH 2/3] Fix regression --- packages/core-data/src/queried-data/reducer.js | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/core-data/src/queried-data/reducer.js b/packages/core-data/src/queried-data/reducer.js index 7fc37f6b02851..f6349c5b25a11 100644 --- a/packages/core-data/src/queried-data/reducer.js +++ b/packages/core-data/src/queried-data/reducer.js @@ -111,9 +111,6 @@ export function items( state = {}, action ) { ...state[ context ], ...action.items.reduce( ( accumulator, value ) => { const itemId = value?.[ key ]; - if ( ! itemId ) { - return accumulator; - } accumulator[ itemId ] = conservativeMapItem( state?.[ context ]?.[ itemId ], @@ -169,9 +166,6 @@ export function itemIsComplete( state = {}, action ) { ...state[ context ], ...action.items.reduce( ( result, item ) => { 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. From 10a0ac28f6710fc09502923f663bc8a11caf2605 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 6 Jun 2024 14:39:31 +0400 Subject: [PATCH 3/3] Use map...filter --- packages/core-data/src/queried-data/reducer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/core-data/src/queried-data/reducer.js b/packages/core-data/src/queried-data/reducer.js index f6349c5b25a11..3a0c8869175c4 100644 --- a/packages/core-data/src/queried-data/reducer.js +++ b/packages/core-data/src/queried-data/reducer.js @@ -233,7 +233,7 @@ const receiveQueries = compose( [ return { itemIds: getMergedItemIds( state?.itemIds || [], - action.items.flatMap( ( item ) => item?.[ key ] ?? [] ), + action.items.map( ( item ) => item?.[ key ] ).filter( Boolean ), page, perPage ),