More robust reactions

This commit is contained in:
Chi Vinh Le
2017-06-13 22:02:54 +07:00
parent aed265efcc
commit 7d8f2aa876
6 changed files with 98 additions and 22 deletions
+40 -10
View File
@@ -149,6 +149,9 @@ export default (reaction) => (WrappedComponent) => {
client: PropTypes.object.isRequired,
};
// Whether or not a mutation is currently active.
duringMutation = false;
constructor(props, context) {
super(props, context);
@@ -208,6 +211,26 @@ export default (reaction) => (WrappedComponent) => {
}
}
postReaction = () => {
if (this.duringMutation) {
return;
}
this.duringMutation = true;
return this.props.postReaction(this.props.comment)
.then((result) => {this.duringMutation = false; return Promise.resolve(result); })
.catch((err) => {this.duringMutation = false; throw err; });
}
deleteReaction = () => {
if (this.duringMutation) {
return;
}
this.duringMutation = true;
return this.props.deleteReaction(this.props.comment)
.then((result) => {this.duringMutation = false; return Promise.resolve(result); })
.catch((err) => {this.duringMutation = false; throw err; });
}
render() {
const {comment} = this.props;
@@ -223,9 +246,16 @@ export default (reaction) => (WrappedComponent) => {
const alreadyReacted = !!reactionSummary;
const withReactionProps = {reactionSummary, count, alreadyReacted};
return <WrappedComponent {...this.props} {...withReactionProps} />;
return <WrappedComponent
showSignInDialog={this.props.showSignInDialog}
user={this.props.user}
comment={comment}
reactionSummary={reactionSummary}
count={count}
alreadyReacted={alreadyReacted}
postReaction={this.postReaction}
deleteReaction={this.deleteReaction}
/>;
}
}
@@ -240,16 +270,16 @@ export default (reaction) => (WrappedComponent) => {
}
`,
{
props: ({mutate, ownProps}) => ({
deleteReaction: () => {
props: ({mutate}) => ({
deleteReaction: (comment) => {
const reactionSummary = getMyActionSummary(
`${Reaction}ActionSummary`,
ownProps.comment
comment
);
const id = reactionSummary.current_user.id;
const item_id = ownProps.comment.id;
const item_id = comment.id;
const input = {id};
return mutate({
@@ -283,11 +313,11 @@ export default (reaction) => (WrappedComponent) => {
}
`,
{
props: ({mutate, ownProps}) => ({
postReaction: () => {
props: ({mutate}) => ({
postReaction: (comment) => {
const input = {
item_id: ownProps.comment.id,
item_id: comment.id,
};
return mutate({
+14 -1
View File
@@ -1,5 +1,6 @@
const wrapResponse = require('../../../graph/helpers/response');
const {SEARCH_OTHER_USERS} = require('../../../perms/constants');
const errors = require('../../../errors');
function getReactionConfig(reaction) {
reaction = reaction.toLowerCase();
@@ -134,13 +135,24 @@ function getReactionConfig(reaction) {
// The comment is needed to allow better filtering e.g. by asset_id.
pubsub.publish(`${reaction}ActionCreated`, {action, comment});
return Promise.resolve(action);
});
})
.catch((err) => {
if (err instanceof errors.ErrAlreadyExists) {
return Promise.resolve(err.metadata.existing);
}
throw err;
});
});
return wrapResponse(reaction)(response);
},
[`delete${Reaction}Action`]: (_, {input: {id}}, {mutators: {Action}, pubsub, loaders: {Comments}}) => {
const response = Action.delete({id})
.then((action) => {
// Action doesn't exist or was already deleted.
if (!action) {
return Promise.resolve(null);
}
return Comments.get.load(action.item_id).then((comment) => {
// The comment is needed to allow better filtering e.g. by asset_id.
@@ -148,6 +160,7 @@ function getReactionConfig(reaction) {
return Promise.resolve(action);
});
});
return wrapResponse(reaction)(response);
}
},