From 9573c9a7afd006ff57296148def24e0d8eabb18d Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Fri, 16 Jun 2017 20:49:17 +0700 Subject: [PATCH] Refactor & docs --- client/coral-admin/src/graphql/utils.js | 14 ++++++- .../Moderation/containers/Moderation.js | 38 +++++++++---------- 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/client/coral-admin/src/graphql/utils.js b/client/coral-admin/src/graphql/utils.js index 8d4418806..331fd876a 100644 --- a/client/coral-admin/src/graphql/utils.js +++ b/client/coral-admin/src/graphql/utils.js @@ -84,7 +84,19 @@ function getCommentQueues(comment) { return queues; } -export function handleCommentStatusChange(root, comment, {sort, notify}) { +/** + * Assimilate comment changes into current store. + * @param {Object} root current state of the store + * @param {Object} comment comment that was changed + * @param {string} sort current sort order of the queues + * @param {Object} [notify] show know notifications if set + * @param {string} notify.activeQueue current active queue + * @param {string} notify.text notification text to show + * @param {bool} notify.anyQueue if true show the notification when the comment is shown + * in the current active queue besides the 'all' queue. + * @return {Object} next state of the store + */ +export function handleCommentChange(root, comment, sort, notify) { let next = root; const nextQueues = getCommentQueues(comment); diff --git a/client/coral-admin/src/routes/Moderation/containers/Moderation.js b/client/coral-admin/src/routes/Moderation/containers/Moderation.js index e1bd18435..1d75b3838 100644 --- a/client/coral-admin/src/routes/Moderation/containers/Moderation.js +++ b/client/coral-admin/src/routes/Moderation/containers/Moderation.js @@ -11,7 +11,7 @@ import update from 'immutability-helper'; import truncate from 'lodash/truncate'; import {withSetUserStatus, withSuspendUser, withSetCommentStatus} from 'coral-framework/graphql/mutations'; -import {handleCommentStatusChange} from '../../../graphql/utils'; +import {handleCommentChange} from '../../../graphql/utils'; import {fetchSettings} from 'actions/settings'; import {updateAssets} from 'actions/assets'; @@ -45,6 +45,7 @@ class ModerationContainer extends Component { }, updateQuery: (prev, {subscriptionData: {data: {commentStatusChanged: comment}}}) => { const user = comment.status_history[comment.status_history.length - 1].assigned_by; + const sort = this.props.moderation.sortOrder; const notify = this.props.auth.user.id === user.id ? {} : { @@ -52,10 +53,7 @@ class ModerationContainer extends Component { text: `${user.username} ${comment.status.toLowerCase()} comment "${truncate(comment.body, {lenght: 50})}"`, anyQueue: false, }; - return handleCommentStatusChange(prev, comment, { - sort: this.props.moderation.sortOrder, - notify, - }); + return handleCommentChange(prev, comment, sort, notify); }, }); @@ -65,14 +63,13 @@ class ModerationContainer extends Component { asset_id: this.props.data.variables.asset_id, }, updateQuery: (prev, {subscriptionData: {data: {commentEdited: comment}}}) => { - return handleCommentStatusChange(prev, comment, { - sort: this.props.moderation.sortOrder, - notify: { - activeQueue: this.activeTab, - text: `${comment.user.username} edited comment to "${truncate(comment.body, {lenght: 50})}"`, - anyQueue: false, - }, - }); + const sort = this.props.moderation.sortOrder; + const notify = { + activeQueue: this.activeTab, + text: `${comment.user.username} edited comment to "${truncate(comment.body, {lenght: 50})}"`, + anyQueue: false, + }; + return handleCommentChange(prev, comment, sort, notify); }, }); @@ -83,14 +80,13 @@ class ModerationContainer extends Component { }, updateQuery: (prev, {subscriptionData: {data: {commentFlagged: comment}}}) => { const user = comment.actions[comment.actions.length - 1].user; - return handleCommentStatusChange(prev, comment, { - sort: this.props.moderation.sortOrder, - notify: { - activeQueue: this.activeTab, - text: `${user.username} flagged comment "${truncate(comment.body, {lenght: 50})}"`, - anyQueue: true, - }, - }); + const sort = this.props.moderation.sortOrder; + const notify = { + activeQueue: this.activeTab, + text: `${user.username} flagged comment "${truncate(comment.body, {lenght: 50})}"`, + anyQueue: true, + }; + return handleCommentChange(prev, comment, sort, notify); }, });