Handle mutation errors

This commit is contained in:
Chi Vinh Le
2017-05-31 00:44:16 +07:00
parent 4f8ef88bd0
commit 109f4b2c0f
3 changed files with 41 additions and 39 deletions
@@ -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}
/>
<ModerationKeysModal
hideShortcutsNote={props.hideShortcutsNote}
@@ -5,6 +5,8 @@ import {compose, gql} from 'react-apollo';
import isEqual from 'lodash/isEqual';
import withQuery from 'coral-framework/hocs/withQuery';
import {getDefinitionName} from 'coral-framework/utils';
import * as notification from 'coral-admin/src/services/notification';
import t, {timeago} from 'coral-framework/services/i18n';
import {withSetUserStatus, withSuspendUser, withSetCommentStatus} from 'coral-framework/graphql/mutations';
@@ -39,6 +41,33 @@ class ModerationContainer 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);
}
};
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}
/>;
}
}
@@ -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'}
);
});
}
}