From a9104b749c1f9c3e97cefaa35cbab5d176249444 Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 15 Feb 2017 12:19:36 -0500 Subject: [PATCH 1/7] Updating comments and replies using updatequeries w/ optimistic updating. --- client/coral-embed-stream/src/Comment.js | 1 - client/coral-embed-stream/src/Embed.js | 1 - .../graphql/mutations/index.js | 53 +++++++++++++++++-- .../graphql/mutations/postComment.graphql | 3 ++ 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/client/coral-embed-stream/src/Comment.js b/client/coral-embed-stream/src/Comment.js index 3bf787a9c..48dd52d09 100644 --- a/client/coral-embed-stream/src/Comment.js +++ b/client/coral-embed-stream/src/Comment.js @@ -140,7 +140,6 @@ class Comment extends React.Component { ? { setActiveReplyBox(''); - refetch(); }} setActiveReplyBox={setActiveReplyBox} parentId={parentId || comment.id} diff --git a/client/coral-embed-stream/src/Embed.js b/client/coral-embed-stream/src/Embed.js index d12dbd0d8..6240c293b 100644 --- a/client/coral-embed-stream/src/Embed.js +++ b/client/coral-embed-stream/src/Embed.js @@ -122,7 +122,6 @@ class Embed extends Component { { user ? ({ fragments: commentView }), - props: ({mutate}) => ({ - postItem: ({asset_id, body, parent_id} /* , type */ ) => { - return mutate({ + props: ({ownProps, mutate}) => ({ + postItem: ({asset_id, body, parent_id}) => + mutate({ variables: { asset_id, body, parent_id + }, + optimisticResponse: { + createComment: { + comment: { + user: { + id: ownProps.auth.user.id, + name: ownProps.auth.user.username + }, + created_at: new Date(), + body, + parent_id, + asset_id, + action_summaries: [], + tags: [], + status: null, + id: `${Date.now()}_temp_id` + } + } + }, + updateQueries: { + AssetQuery: (oldData, {mutationResult:{data:{createComment:{comment}}}}) => { + + // If posting a reply + return parent_id ? { + ...oldData, + asset: { + ...oldData.asset, + comments: oldData.asset.comments.map((comment) => + comment.id === parent_id + ? {...comment, replies: [...comment.replies, comment]} + : comment) + } + } + + // If posting a top-level comment + : { + ...oldData, + asset: { + ...oldData.asset, + comments: [comment, ...oldData.asset.comments] + } + }; + } } - }); - }}), + }) + }), }); export const postLike = graphql(POST_LIKE, { diff --git a/client/coral-framework/graphql/mutations/postComment.graphql b/client/coral-framework/graphql/mutations/postComment.graphql index 6ce46eaa8..3840d66c2 100644 --- a/client/coral-framework/graphql/mutations/postComment.graphql +++ b/client/coral-framework/graphql/mutations/postComment.graphql @@ -4,6 +4,9 @@ mutation CreateComment ($asset_id: ID!, $parent_id: ID, $body: String!) { createComment(asset_id:$asset_id, parent_id:$parent_id, body:$body) { comment { ...commentView + replies { + ...commentView + } } errors { translation_key From 904bda86dbf98ef28708361d26d8a4520cf41385 Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 15 Feb 2017 12:36:19 -0500 Subject: [PATCH 2/7] Fixing error with replies. --- client/coral-framework/graphql/mutations/index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/client/coral-framework/graphql/mutations/index.js b/client/coral-framework/graphql/mutations/index.js index 6345e797c..fd08e1751 100644 --- a/client/coral-framework/graphql/mutations/index.js +++ b/client/coral-framework/graphql/mutations/index.js @@ -37,17 +37,17 @@ export const postComment = graphql(POST_COMMENT, { } }, updateQueries: { - AssetQuery: (oldData, {mutationResult:{data:{createComment:{comment}}}}) => { - + AssetQuery: (oldData, {mutationResult:{data:{createComment:{comment}}}}) => + // If posting a reply - return parent_id ? { + parent_id ? { ...oldData, asset: { ...oldData.asset, - comments: oldData.asset.comments.map((comment) => - comment.id === parent_id - ? {...comment, replies: [...comment.replies, comment]} - : comment) + comments: oldData.asset.comments.map((oldComment) => + oldComment.id === parent_id + ? {...oldComment, replies: [...oldComment.replies, comment]} + : oldComment) } } @@ -58,7 +58,6 @@ export const postComment = graphql(POST_COMMENT, { ...oldData.asset, comments: [comment, ...oldData.asset.comments] } - }; } } }) From 79dbe7dcdabf13850cf9a4d9b857bdb03bc0666e Mon Sep 17 00:00:00 2001 From: David Jay Date: Wed, 15 Feb 2017 13:05:31 -0500 Subject: [PATCH 3/7] Updating commentCount on comment post. --- client/coral-framework/graphql/mutations/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/client/coral-framework/graphql/mutations/index.js b/client/coral-framework/graphql/mutations/index.js index fd08e1751..b67343521 100644 --- a/client/coral-framework/graphql/mutations/index.js +++ b/client/coral-framework/graphql/mutations/index.js @@ -38,7 +38,7 @@ export const postComment = graphql(POST_COMMENT, { }, updateQueries: { AssetQuery: (oldData, {mutationResult:{data:{createComment:{comment}}}}) => - + // If posting a reply parent_id ? { ...oldData, @@ -56,6 +56,7 @@ export const postComment = graphql(POST_COMMENT, { ...oldData, asset: { ...oldData.asset, + commentCount: oldData.asset.commentCount + 1, comments: [comment, ...oldData.asset.comments] } } From e3e85c5f3b6473bd816c651370e9909fddd9b9f2 Mon Sep 17 00:00:00 2001 From: David Jay Date: Thu, 16 Feb 2017 17:18:36 -0500 Subject: [PATCH 4/7] Fixing error when posting comment. --- client/coral-framework/graphql/mutations/postComment.graphql | 1 + 1 file changed, 1 insertion(+) diff --git a/client/coral-framework/graphql/mutations/postComment.graphql b/client/coral-framework/graphql/mutations/postComment.graphql index 3840d66c2..110ab4b4e 100644 --- a/client/coral-framework/graphql/mutations/postComment.graphql +++ b/client/coral-framework/graphql/mutations/postComment.graphql @@ -4,6 +4,7 @@ mutation CreateComment ($asset_id: ID!, $parent_id: ID, $body: String!) { createComment(asset_id:$asset_id, parent_id:$parent_id, body:$body) { comment { ...commentView + replyCount replies { ...commentView } From a35af36ee1eaf06aee7ca8a121e8d82952cf7c1d Mon Sep 17 00:00:00 2001 From: David Jay Date: Fri, 17 Feb 2017 11:26:19 -0500 Subject: [PATCH 5/7] Handling premod in optimistic comment updates. --- .../coral-framework/graphql/mutations/index.js | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/client/coral-framework/graphql/mutations/index.js b/client/coral-framework/graphql/mutations/index.js index b67343521..cdc57e73e 100644 --- a/client/coral-framework/graphql/mutations/index.js +++ b/client/coral-framework/graphql/mutations/index.js @@ -25,7 +25,7 @@ export const postComment = graphql(POST_COMMENT, { id: ownProps.auth.user.id, name: ownProps.auth.user.username }, - created_at: new Date(), + created_at: new Date().toString(), body, parent_id, asset_id, @@ -37,17 +37,22 @@ export const postComment = graphql(POST_COMMENT, { } }, updateQueries: { - AssetQuery: (oldData, {mutationResult:{data:{createComment:{comment}}}}) => + AssetQuery: (oldData, {mutationResult:{data:{createComment:{comment}}}}) => { + + if (oldData.asset.moderation === 'PRE') { + return oldData; + } // If posting a reply - parent_id ? { + return parent_id ? { ...oldData, asset: { ...oldData.asset, - comments: oldData.asset.comments.map((oldComment) => - oldComment.id === parent_id + comments: oldData.asset.comments.map((oldComment) => { + return oldComment.id === parent_id ? {...oldComment, replies: [...oldComment.replies, comment]} - : oldComment) + : oldComment; + }) } } @@ -59,6 +64,7 @@ export const postComment = graphql(POST_COMMENT, { commentCount: oldData.asset.commentCount + 1, comments: [comment, ...oldData.asset.comments] } + }; } } }) From 6eb4725dd9bfe273dbfc4edb056951692bf6c2e1 Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Fri, 17 Feb 2017 14:03:37 -0700 Subject: [PATCH 6/7] remove ternary from return statement --- .../graphql/mutations/index.js | 45 +++++++++++-------- 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/client/coral-framework/graphql/mutations/index.js b/client/coral-framework/graphql/mutations/index.js index cdc57e73e..ef5359eb1 100644 --- a/client/coral-framework/graphql/mutations/index.js +++ b/client/coral-framework/graphql/mutations/index.js @@ -43,28 +43,35 @@ export const postComment = graphql(POST_COMMENT, { return oldData; } + let updatedAsset; + // If posting a reply - return parent_id ? { - ...oldData, - asset: { - ...oldData.asset, - comments: oldData.asset.comments.map((oldComment) => { - return oldComment.id === parent_id - ? {...oldComment, replies: [...oldComment.replies, comment]} - : oldComment; - }) - } + if (parent_id) { + updatedAsset = { + ...oldData, + asset: { + ...oldData.asset, + comments: oldData.asset.comments.map((oldComment) => { + return oldComment.id === parent_id + ? {...oldComment, replies: [...oldComment.replies, comment]} + : oldComment; + }) + } + }; + } else { + + // If posting a top-level comment + updatedAsset = { + ...oldData, + asset: { + ...oldData.asset, + commentCount: oldData.asset.commentCount + 1, + comments: [comment, ...oldData.asset.comments] + } + }; } - // If posting a top-level comment - : { - ...oldData, - asset: { - ...oldData.asset, - commentCount: oldData.asset.commentCount + 1, - comments: [comment, ...oldData.asset.comments] - } - }; + return updatedAsset; } } }) From 2506442453877f041e401d68dc9d3a54ccbfe5ba Mon Sep 17 00:00:00 2001 From: Riley Davis Date: Fri, 17 Feb 2017 14:10:21 -0700 Subject: [PATCH 7/7] toISOString --- client/coral-framework/graphql/mutations/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/coral-framework/graphql/mutations/index.js b/client/coral-framework/graphql/mutations/index.js index ef5359eb1..a9d08da85 100644 --- a/client/coral-framework/graphql/mutations/index.js +++ b/client/coral-framework/graphql/mutations/index.js @@ -25,7 +25,7 @@ export const postComment = graphql(POST_COMMENT, { id: ownProps.auth.user.id, name: ownProps.auth.user.username }, - created_at: new Date().toString(), + created_at: new Date().toISOString(), body, parent_id, asset_id,