From 109f4b2c0ff75faf0ff7c94e1ffe8bee7ea5f989 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Wed, 31 May 2017 00:44:16 +0700 Subject: [PATCH] Handle mutation errors --- .../Moderation/components/Moderation.js | 31 +------------------ .../Moderation/containers/Moderation.js | 30 ++++++++++++++++++ .../coral-admin/src/services/notification.js | 19 ++++++------ 3 files changed, 41 insertions(+), 39 deletions(-) diff --git a/client/coral-admin/src/routes/Moderation/components/Moderation.js b/client/coral-admin/src/routes/Moderation/components/Moderation.js index 4f5441e4f..956575ce4 100644 --- a/client/coral-admin/src/routes/Moderation/components/Moderation.js +++ b/client/coral-admin/src/routes/Moderation/components/Moderation.js @@ -1,8 +1,6 @@ import React, {Component} from 'react'; -import * as notification from 'coral-admin/src/services/notification'; import key from 'keymaster'; import styles from './styles.css'; -import t, {timeago} from 'coral-framework/services/i18n'; import BanUserDialog from './BanUserDialog'; import SuspendUserDialog from './SuspendUserDialog'; @@ -72,33 +70,6 @@ export default class Moderation extends Component { } } - suspendUser = async (args) => { - this.props.hideSuspendUserDialog(); - try { - const result = await this.props.suspendUser(args); - if (result.data.suspendUser.errors) { - throw result.data.suspendUser.errors; - } - notification.success( - t('suspenduser.notify_suspend_until', - this.props.moderation.suspendUserDialog.username, - timeago(args.until)), - ); - const {commentStatus, commentId} = this.props.moderation.suspendUserDialog; - if (commentStatus !== 'REJECTED') { - return this.props.rejectComment({commentId}) - .then((result) => { - if (result.data.setCommentStatus.errors) { - throw result.data.setCommentStatus.errors; - } - }); - } - } - catch(err) { - notification.showMutationErrors(err); - } - }; - componentWillUnmount() { key.unbind('s'); key.unbind('shift+/'); @@ -206,7 +177,7 @@ export default class Moderation extends Component { userId={moderation.suspendUserDialog.userId} organizationName={root.settings.organizationName} onCancel={props.hideSuspendUserDialog} - onPerform={this.suspendUser} + onPerform={this.props.suspendUser} /> { + this.props.hideSuspendUserDialog(); + try { + const result = await this.props.suspendUser(args); + if (result.data.suspendUser.errors) { + throw result.data.suspendUser.errors; + } + notification.success( + t('suspenduser.notify_suspend_until', + this.props.moderation.suspendUserDialog.username, + timeago(args.until)), + ); + const {commentStatus, commentId} = this.props.moderation.suspendUserDialog; + if (commentStatus !== 'REJECTED') { + return this.props.rejectComment({commentId}) + .then((result) => { + if (result.data.setCommentStatus.errors) { + throw result.data.setCommentStatus.errors; + } + }); + } + } + catch(err) { + notification.showMutationErrors(err); + } + }; + banUser = ({userId}) => { return this.props.setUserStatus({userId, status: 'BANNED'}); } @@ -108,6 +137,7 @@ class ModerationContainer extends Component { banUser={this.banUser} acceptComment={this.acceptComment} rejectComment={this.rejectComment} + suspendUser={this.suspendUser} />; } } diff --git a/client/coral-admin/src/services/notification.js b/client/coral-admin/src/services/notification.js index cc25b0756..5e8673060 100644 --- a/client/coral-admin/src/services/notification.js +++ b/client/coral-admin/src/services/notification.js @@ -13,13 +13,14 @@ export function info(msg) { return toast(msg, {type: 'info'}); } -export function showMutationErrors(err) { - const errors = Array.isArray(err) ? err : [err]; - errors.forEach((err) => { - console.error(err); - toast( - err.translation_key ? t(`error.${err.translation_key}`) : err, - {type: 'error'} - ); - }); +export function showMutationErrors(error) { + console.error(error); + if (error.errors) { + error.errors.forEach((err) => { + toast( + err.translation_key ? t(`error.${err.translation_key}`) : err, + {type: 'error'} + ); + }); + } }