From 84596751e9c8dd1c9ce148cdc66541687f0c4d97 Mon Sep 17 00:00:00 2001 From: Adam Meikle Date: Fri, 27 Jan 2023 07:05:00 -0500 Subject: [PATCH] added check for undefined for contentful --- src/shared/reducers/contentful/content.js | 70 ++++++++++++----------- 1 file changed, 36 insertions(+), 34 deletions(-) diff --git a/src/shared/reducers/contentful/content.js b/src/shared/reducers/contentful/content.js index a137a584d5..c12f446b07 100644 --- a/src/shared/reducers/contentful/content.js +++ b/src/shared/reducers/contentful/content.js @@ -120,43 +120,45 @@ function onQueryContentDone(state, action) { /* Adds matched items to items collection. */ const d = _.omit(data, 'includes'); - let a = collectionActions.addItems(_.keyBy(data.items, i => i.sys.id)); - res.items = reduceCollection(res.items, a); - - /* Adds query itself to the quries collection. */ - d.items = d.items.map(i => i.sys.id); - a = collectionActions.loadItemDone(operationId, queryId, d); - res.queries = reduceCollection(res.queries, a); - - /* In case the resulting query match is different from the one stored in the - * old state, we should correctly update reference counters of the newly - * matched items, and of the items not matched anymore. */ - - const q = state.queries[queryId]; - - let added = []; - const gone = []; - if (q.item) { - const neu = new Set(d.items); - const old = new Set(q.item.items); - d.items.forEach((id) => { - if (!old.has(id)) added.push(id); - }); - q.item.items.forEach((id) => { - if (!neu.has(id)) gone.push(id); - }); - } else added = d.items; - - if (added.length) { - a = collectionActions.bookItems(added, q.numRefs); + /* sometimes contentful returns no data, so just return current state if that happens */ + if (data && data.items) { + let a = collectionActions.addItems(_.keyBy(data.items, i => i.sys.id)); res.items = reduceCollection(res.items, a); - } - if (gone.length) { - a = collectionActions.freeItems(gone, q.numRefs); - res.items = reduceCollection(res.items, a); + /* Adds query itself to the quries collection. */ + d.items = d.items.map(i => i.sys.id); + a = collectionActions.loadItemDone(operationId, queryId, d); + res.queries = reduceCollection(res.queries, a); + + /* In case the resulting query match is different from the one stored in the + * old state, we should correctly update reference counters of the newly + * matched items, and of the items not matched anymore. */ + + const q = state.queries[queryId]; + + let added = []; + const gone = []; + if (q.item) { + const neu = new Set(d.items); + const old = new Set(q.item.items); + d.items.forEach((id) => { + if (!old.has(id)) added.push(id); + }); + q.item.items.forEach((id) => { + if (!neu.has(id)) gone.push(id); + }); + } else added = d.items; + + if (added.length) { + a = collectionActions.bookItems(added, q.numRefs); + res.items = reduceCollection(res.items, a); + } + + if (gone.length) { + a = collectionActions.freeItems(gone, q.numRefs); + res.items = reduceCollection(res.items, a); + } } - return res; }