From e3b9799d4a2125377679652723eeaf8b3679fe50 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Thu, 23 Feb 2017 09:58:31 -0700 Subject: [PATCH] nested ternaries hurt me --- .../coral-framework/graphql/queries/index.js | 49 +++++++++++-------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/client/coral-framework/graphql/queries/index.js b/client/coral-framework/graphql/queries/index.js index 9b8a55134..c993dfb6a 100644 --- a/client/coral-framework/graphql/queries/index.js +++ b/client/coral-framework/graphql/queries/index.js @@ -49,29 +49,38 @@ export const loadMore = (data) => ({limit, cursor, parent_id, asset_id, sort}, n asset_id, sort }, - updateQuery: (oldData, {fetchMoreResult:{data:{new_top_level_comments}}}) => + updateQuery: (oldData, {fetchMoreResult:{data:{new_top_level_comments}}}) => { - // If loading more replies - parent_id ? { - ...oldData, - asset: { - ...oldData.asset, - comments: oldData.asset.comments.map((comment) => - comment.id === parent_id - ? {...comment, replies: [...comment.replies, ...new_top_level_comments]} - : comment) - } + let updatedAsset; + + if (parent_id) { + + // If loading more replies + updatedAsset = { + ...oldData, + asset: { + ...oldData.asset, + comments: oldData.asset.comments.map((comment) => + comment.id === parent_id + ? {...comment, replies: [...comment.replies, ...new_top_level_comments]} + : comment) + } + }; + } else { + + // If loading more top-level comments + updatedAsset = { + ...oldData, + asset: { + ...oldData.asset, + comments: newComments ? [...new_top_level_comments.reverse(), ...oldData.asset.comments] + : [...oldData.asset.comments, ...new_top_level_comments] + } + }; } - // If loading more top-level comments - : { - ...oldData, - asset: { - ...oldData.asset, - comments: newComments ? [...new_top_level_comments.reverse(), ...oldData.asset.comments] - : [...oldData.asset.comments, ...new_top_level_comments] - } - } + return updatedAsset; + } }); };