From 2cbd59b4b5b2df7b75087760eeb6f1893f47239a Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Fri, 8 Sep 2017 17:05:20 -0300 Subject: [PATCH] Featured comments error in permalink view, handling permaview moderation and handling replies moderation --- .../src/components/Comment.js | 2 +- client/coral-framework/graphql/mutations.js | 21 +++++ .../client/index.js | 76 ++++++++++--------- .../client/containers/ApproveCommentAction.js | 3 +- .../client/containers/RejectCommentAction.js | 3 +- .../client/index.js | 49 +----------- 6 files changed, 68 insertions(+), 86 deletions(-) diff --git a/client/coral-embed-stream/src/components/Comment.js b/client/coral-embed-stream/src/components/Comment.js index 68eaf50cc..c91396eb1 100644 --- a/client/coral-embed-stream/src/components/Comment.js +++ b/client/coral-embed-stream/src/components/Comment.js @@ -341,7 +341,7 @@ export default class Comment extends React.Component { emit, commentClassNames = [] } = this.props; - + if (this.commentIsRejected(comment)) { return ; } diff --git a/client/coral-framework/graphql/mutations.js b/client/coral-framework/graphql/mutations.js index c09ce8751..fb004956b 100644 --- a/client/coral-framework/graphql/mutations.js +++ b/client/coral-framework/graphql/mutations.js @@ -127,6 +127,27 @@ export const withSetCommentStatus = withMutation( commentId, status, }, + optimisticResponse: { + setCommentStatus: { + __typename: 'SetCommentStatusResponse', + errors: null, + } + }, + update: (proxy) => { + + const fragment = gql` + fragment Talk_SetCommentStatus on Comment { + status + }`; + + const fragmentId = `Comment_${commentId}`; + + const data = proxy.readFragment({fragment, id: fragmentId}); + + data.status = status; + + proxy.writeFragment({fragment, id: fragmentId, data}); + } }); } }) diff --git a/plugins/talk-plugin-featured-comments/client/index.js b/plugins/talk-plugin-featured-comments/client/index.js index e451ad830..765951652 100644 --- a/plugins/talk-plugin-featured-comments/client/index.js +++ b/plugins/talk-plugin-featured-comments/client/index.js @@ -49,36 +49,39 @@ export default { AddTag: ({variables}) => ({ updateQueries: { CoralEmbedStream_Embed: (previous) => { + let updated = previous; if (variables.name !== 'FEATURED') { return; } const comment = findCommentInEmbedQuery(previous, variables.id); - - const updated = update(previous, { - asset: { - comments: { - nodes: { - $apply: (nodes) => nodes.map((node) => { - if (node.id === variables.id) { - node.status = 'ACCEPTED'; - } - - return node; - }) - } - }, - featuredComments: { - nodes: { - $apply: (nodes) => prependNewNodes(nodes, [comment]), - } - }, - featuredCommentsCount: { - $apply: (value) => value + 1 + + if (previous.asset.comments) { + updated = update(previous, { + asset: { + comments: { + nodes: { + $apply: (nodes) => nodes.map((node) => { + if (node.id === variables.id) { + node.status = 'ACCEPTED'; + } + + return node; + }) + } + }, + featuredComments: { + nodes: { + $apply: (nodes) => prependNewNodes(nodes, [comment]), + } + }, + featuredCommentsCount: { + $apply: (value) => value + 1 + }, } - } - }); + }); + } return updated; }, @@ -87,24 +90,27 @@ export default { RemoveTag: ({variables}) => ({ updateQueries: { CoralEmbedStream_Embed: (previous) => { - + let updated = previous; + if (variables.name !== 'FEATURED') { return; } - const updated = update(previous, { - asset: { - featuredComments: { - nodes: { - $apply: (nodes) => - nodes.filter((n) => n.id !== variables.id) + if (previous.asset.comments) { + updated = update(previous, { + asset: { + featuredComments: { + nodes: { + $apply: (nodes) => + nodes.filter((n) => n.id !== variables.id) + } + }, + featuredCommentsCount: { + $apply: (value) => value - 1 } - }, - featuredCommentsCount: { - $apply: (value) => value - 1 } - } - }); + }); + } return updated; }, diff --git a/plugins/talk-plugin-moderation-actions/client/containers/ApproveCommentAction.js b/plugins/talk-plugin-moderation-actions/client/containers/ApproveCommentAction.js index bcedb7fc6..686cded9f 100644 --- a/plugins/talk-plugin-moderation-actions/client/containers/ApproveCommentAction.js +++ b/plugins/talk-plugin-moderation-actions/client/containers/ApproveCommentAction.js @@ -3,6 +3,7 @@ import {getErrorMessages} from 'plugin-api/beta/client/utils'; import {withSetCommentStatus} from 'plugin-api/beta/client/hocs'; import {notify} from 'plugin-api/beta/client/actions/notification'; import ApproveCommentAction from '../components/ApproveCommentAction'; +import isNil from 'lodash/isNil'; class ApproveCommentActionContainer extends React.Component { @@ -15,7 +16,7 @@ class ApproveCommentActionContainer extends React.Component { status: 'ACCEPTED' }); - if (result.data.setCommentStatus.errors) { + if (!isNil(result.data.setCommentStatus)) { throw result.data.setCommentStatus.errors; } diff --git a/plugins/talk-plugin-moderation-actions/client/containers/RejectCommentAction.js b/plugins/talk-plugin-moderation-actions/client/containers/RejectCommentAction.js index fa8c12730..82ca9f175 100644 --- a/plugins/talk-plugin-moderation-actions/client/containers/RejectCommentAction.js +++ b/plugins/talk-plugin-moderation-actions/client/containers/RejectCommentAction.js @@ -3,6 +3,7 @@ import {getErrorMessages} from 'plugin-api/beta/client/utils'; import {withSetCommentStatus} from 'plugin-api/beta/client/hocs'; import {notify} from 'plugin-api/beta/client/actions/notification'; import RejectCommentAction from '../components/RejectCommentAction'; +import isNil from 'lodash/isNil'; class RejectCommentActionContainer extends React.Component { @@ -15,7 +16,7 @@ class RejectCommentActionContainer extends React.Component { status: 'REJECTED' }); - if (result.data.setCommentStatus.errors) { + if (!isNil(result.data.setCommentStatus)) { throw result.data.setCommentStatus.errors; } diff --git a/plugins/talk-plugin-moderation-actions/client/index.js b/plugins/talk-plugin-moderation-actions/client/index.js index 445c72271..3343505f3 100644 --- a/plugins/talk-plugin-moderation-actions/client/index.js +++ b/plugins/talk-plugin-moderation-actions/client/index.js @@ -1,56 +1,9 @@ import ModerationActions from './containers/ModerationActions'; import translations from './translations.yml'; -import update from 'immutability-helper'; export default { slots: { commentInfoBar: [ModerationActions], }, - translations, - mutations: { - SetCommentStatus: ({variables: {status, commentId}}) => ({ - updateQueries: { - CoralEmbedStream_Embed: (prev) => { - - if (status !== 'REJECTED' && status !== "ACCEPTED") { - return prev; - } - - // Permalink view will retrieve only one comment. - if (prev.asset.comment) { - const updated = update(prev, { - asset: { - comment: { - status: { - $set: status - } - } - } - }); - - return updated; - } - - // Stream View - const updated = update(prev, { - asset: { - comments: { - nodes: { - $apply: (nodes) => nodes.map((node) => { - if (node.id === commentId) { - node.status = status; - } - - return node; - }) - } - } - } - }); - - return updated; - } - } - }), - }, + translations };