Fix error notification bugs

This commit is contained in:
Chi Vinh Le
2017-09-14 22:22:16 +07:00
parent c8cc5e6a34
commit 7600ce8d26
7 changed files with 69 additions and 57 deletions
@@ -6,15 +6,22 @@ import {hideBanUserDialog} from '../actions/banUserDialog';
import {withSetUserStatus, withSetCommentStatus} from 'coral-framework/graphql/mutations';
import {compose} from 'react-apollo';
import t from 'coral-framework/services/i18n';
import {getErrorMessages} from 'coral-framework/utils';
import {notify} from 'coral-framework/actions/notification';
class BanUserDialogContainer extends Component {
banUser = async () => {
const {userId, commentId, commentStatus, setUserStatus, setCommentStatus, hideBanUserDialog} = this.props;
await setUserStatus({userId, status: 'BANNED'});
hideBanUserDialog();
if (commentId && commentStatus && commentStatus !== 'REJECTED') {
await setCommentStatus({commentId, status: 'REJECTED'});
const {userId, commentId, commentStatus, setUserStatus, setCommentStatus, hideBanUserDialog, notify} = this.props;
try {
await setUserStatus({userId, status: 'BANNED'});
hideBanUserDialog();
if (commentId && commentStatus && commentStatus !== 'REJECTED') {
await setCommentStatus({commentId, status: 'REJECTED'});
}
}
catch(err) {
notify('error', getErrorMessages(err));
}
}
@@ -50,6 +57,7 @@ const mapStateToProps = ({banUserDialog: {open, userId, username, commentId, com
const mapDispatchToProps = (dispatch) => ({
...bindActionCreators({
hideBanUserDialog,
notify,
}, dispatch),
});
@@ -17,21 +17,13 @@ class SuspendUserDialogContainer extends Component {
const {userId, username, commentStatus, commentId, hideSuspendUserDialog, setCommentStatus, suspendUser, notify} = this.props;
hideSuspendUserDialog();
try {
const result = await suspendUser({id: userId, message, until});
if (result.data.suspendUser.errors) {
throw result.data.suspendUser.errors;
}
await suspendUser({id: userId, message, until});
notify(
'success',
t('suspenduser.notify_suspend_until', username, timeago(until)),
);
if (commentId && commentStatus && commentStatus !== 'REJECTED') {
return setCommentStatus({commentId, status: 'REJECTED'})
.then((result) => {
if (result.data.setCommentStatus.errors) {
throw result.data.setCommentStatus.errors;
}
});
await setCommentStatus({commentId, status: 'REJECTED'});
}
}
catch(err) {
+37 -8
View File
@@ -7,12 +7,13 @@ import {getDisplayName} from 'coral-framework/helpers/hoc';
import {compose, gql} from 'react-apollo';
import withFragments from 'coral-framework/hocs/withFragments';
import withMutation from 'coral-framework/hocs/withMutation';
import {addNotification} from 'coral-framework/actions/notification';
import {notify} from 'coral-framework/actions/notification';
import {capitalize} from 'coral-framework/helpers/strings';
import {getMyActionSummary, getTotalActionCount} from 'coral-framework/utils';
import {getMyActionSummary, getTotalActionCount, getErrorMessages} from 'coral-framework/utils';
import hoistStatics from 'recompose/hoistStatics';
import * as PropTypes from 'prop-types';
import {getDefinitionName} from '../utils';
import {t, can} from 'plugin-api/beta/client/services';
// TODO: Auth logic needs refactoring.
import {showSignInDialog} from 'coral-embed-stream/src/actions/auth';
@@ -248,9 +249,23 @@ export default (reaction, options = {}) => hoistStatics((WrappedComponent) => {
return;
}
this.duringMutation = true;
// If the current user is suspended, do nothing.
if (!can(this.props.user, 'INTERACT_WITH_COMMUNITY')) {
notify('error', t('error.NOT_AUTHORIZED'));
return;
}
return this.props.postReaction(this.props.comment)
.then((result) => {this.duringMutation = false; return Promise.resolve(result); })
.catch((err) => {this.duringMutation = false; throw err; });
.then((result) => {
this.duringMutation = false;
return result;
})
.catch((err) => {
this.duringMutation = false;
this.props.notify('error', getErrorMessages(err));
throw err;
});
}
deleteReaction = () => {
@@ -258,9 +273,23 @@ export default (reaction, options = {}) => hoistStatics((WrappedComponent) => {
return;
}
this.duringMutation = true;
// If the current user is suspended, do nothing.
if (!can(this.props.user, 'INTERACT_WITH_COMMUNITY')) {
notify('error', t('error.NOT_AUTHORIZED'));
return;
}
return this.props.deleteReaction(this.props.comment)
.then((result) => {this.duringMutation = false; return Promise.resolve(result); })
.catch((err) => {this.duringMutation = false; throw err; });
.then((result) => {
this.duringMutation = false;
return result;
})
.catch((err) => {
this.duringMutation = false;
this.props.notify('error', getErrorMessages(err));
throw err;
});
}
render() {
@@ -283,7 +312,7 @@ export default (reaction, options = {}) => hoistStatics((WrappedComponent) => {
asset={asset}
comment={comment}
showSignInDialog={this.props.showSignInDialog}
addNotification={this.props.addNotification}
notify={this.props.notify}
user={this.props.user}
reactionSummary={reactionSummary}
count={count}
@@ -386,7 +415,7 @@ export default (reaction, options = {}) => hoistStatics((WrappedComponent) => {
});
const mapDispatchToProps = (dispatch) =>
bindActionCreators({showSignInDialog, addNotification}, dispatch);
bindActionCreators({showSignInDialog, notify}, dispatch);
const enhance = compose(
withFragments({
+14 -10
View File
@@ -6,8 +6,8 @@ import {getDisplayName} from 'coral-framework/helpers/hoc';
import {capitalize} from 'coral-framework/helpers/strings';
import {withAddTag, withRemoveTag} from 'coral-framework/graphql/mutations';
import withFragments from 'coral-framework/hocs/withFragments';
import {addNotification} from 'coral-framework/actions/notification';
import {forEachError, isTagged} from 'coral-framework/utils';
import {notify} from 'coral-framework/actions/notification';
import {getErrorMessages, isTagged} from 'coral-framework/utils';
import hoistStatics from 'recompose/hoistStatics';
import {getDefinitionName} from '../utils';
@@ -37,7 +37,7 @@ export default (tag, options = {}) => hoistStatics((WrappedComponent) => {
loading = false;
postTag = () => {
const {comment, asset, addNotification} = this.props;
const {comment, asset, notify} = this.props;
if (this.loading) {
return;
@@ -45,23 +45,25 @@ export default (tag, options = {}) => hoistStatics((WrappedComponent) => {
this.loading = true;
this.props.addTag({
return this.props.addTag({
id: comment.id,
name: TAG,
assetId: asset.id,
itemType: 'COMMENTS',
})
.then(() => {
.then((result) => {
this.loading = false;
return result;
})
.catch((err) => {
this.loading = false;
forEachError(err, ({msg}) => addNotification('error', msg));
notify('error', getErrorMessages(err));
throw err;
});
}
deleteTag = () => {
const {comment, asset, addNotification} = this.props;
const {comment, asset, notify} = this.props;
if (this.loading) {
return;
@@ -73,12 +75,14 @@ export default (tag, options = {}) => hoistStatics((WrappedComponent) => {
assetId: asset.id,
itemType: 'COMMENTS',
})
.then(() => {
.then((result) => {
this.loading = false;
return result;
})
.catch((err) => {
this.loading = false;
forEachError(err, ({msg}) => addNotification('error', msg));
notify('error', getErrorMessages(err));
throw err;
});
}
@@ -105,7 +109,7 @@ export default (tag, options = {}) => hoistStatics((WrappedComponent) => {
});
const mapDispatchToProps = (dispatch) =>
bindActionCreators({addNotification}, dispatch);
bindActionCreators({notify}, dispatch);
const enhance = compose(
withFragments({
@@ -1,7 +1,7 @@
import React from 'react';
import styles from './styles.css';
import {withReaction} from 'plugin-api/beta/client/hocs';
import {t, can} from 'plugin-api/beta/client/services';
import {t} from 'plugin-api/beta/client/services';
import {Icon} from 'plugin-api/beta/client/components/ui';
import cn from 'classnames';
@@ -13,7 +13,6 @@ class LikeButton extends React.Component {
postReaction,
deleteReaction,
showSignInDialog,
addNotification,
alreadyReacted,
user,
} = this.props;
@@ -24,12 +23,6 @@ class LikeButton extends React.Component {
return;
}
// If the current user is suspended, do nothing.
if (!can(user, 'INTERACT_WITH_COMMUNITY')) {
addNotification('error', t('error.NOT_AUTHORIZED'));
return;
}
if (alreadyReacted) {
deleteReaction();
} else {
@@ -1,7 +1,7 @@
import React from 'react';
import styles from './styles.css';
import {withReaction} from 'plugin-api/beta/client/hocs';
import {t, can} from 'plugin-api/beta/client/services';
import {t} from 'plugin-api/beta/client/services';
import {Icon} from 'plugin-api/beta/client/components/ui';
import cn from 'classnames';
@@ -13,7 +13,6 @@ class LoveButton extends React.Component {
postReaction,
deleteReaction,
showSignInDialog,
addNotification,
alreadyReacted,
user,
} = this.props;
@@ -24,12 +23,6 @@ class LoveButton extends React.Component {
return;
}
// If the current user is suspended, do nothing.
if (!can(user, 'INTERACT_WITH_COMMUNITY')) {
addNotification('error', t('error.NOT_AUTHORIZED'));
return;
}
if (alreadyReacted) {
deleteReaction();
} else {
@@ -2,7 +2,7 @@ import React from 'react';
import Icon from './Icon';
import styles from './styles.css';
import {withReaction} from 'plugin-api/beta/client/hocs';
import {t, can} from 'plugin-api/beta/client/services';
import {t} from 'plugin-api/beta/client/services';
import cn from 'classnames';
const plugin = 'talk-plugin-respect';
@@ -14,7 +14,6 @@ class RespectButton extends React.Component {
deleteReaction,
showSignInDialog,
alreadyReacted,
addNotification,
user,
} = this.props;
@@ -24,12 +23,6 @@ class RespectButton extends React.Component {
return;
}
// If the current user is suspended, do nothing.
if (!can(user, 'INTERACT_WITH_COMMUNITY')) {
addNotification('error', t('error.NOT_AUTHORIZED'));
return;
}
if (alreadyReacted) {
deleteReaction();
} else {