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
+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({