2016-08-08 11:18:21 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
2016-03-21 10:27:43 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-05-03 12:44:50 +09:00
|
|
|
import classNames from 'classnames';
|
2016-10-31 17:13:14 +01:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2016-10-31 18:21:28 +01:00
|
|
|
import { graphql } from 'react-apollo';
|
|
|
|
import gql from 'graphql-tag';
|
2016-06-23 15:00:58 +09:00
|
|
|
import Users from 'meteor/nova:users';
|
2016-03-21 10:50:22 +09:00
|
|
|
|
2016-03-21 10:27:43 +09:00
|
|
|
class Vote extends Component {
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.upvote = this.upvote.bind(this);
|
2016-11-02 14:50:51 +09:00
|
|
|
this.startLoading = this.startLoading.bind(this);
|
|
|
|
this.stopLoading = this.stopLoading.bind(this);
|
|
|
|
this.state = {
|
|
|
|
loading: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
startLoading() {
|
|
|
|
this.setState({ loading: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
stopLoading() {
|
|
|
|
this.setState({ loading: false });
|
2016-03-21 10:27:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
upvote(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2016-11-02 14:50:51 +09:00
|
|
|
this.startLoading();
|
|
|
|
|
2016-03-21 10:27:43 +09:00
|
|
|
const post = this.props.post;
|
2016-03-23 14:27:08 +09:00
|
|
|
const user = this.context.currentUser;
|
2016-03-21 10:27:43 +09:00
|
|
|
|
|
|
|
if(!user){
|
2016-10-31 17:13:14 +01:00
|
|
|
this.props.flash("Please log in first");
|
2016-03-21 10:27:43 +09:00
|
|
|
} else {
|
2016-10-31 18:21:28 +01:00
|
|
|
const voteType = Users.hasUpvoted(user, post) ? "cancelUpvote" : "upvote";
|
2016-11-02 16:31:15 +09:00
|
|
|
this.props.vote({post, voteType, currentUser: this.context.currentUser}).then(result => {
|
2016-11-02 14:50:51 +09:00
|
|
|
this.stopLoading();
|
|
|
|
});
|
2016-10-31 18:21:28 +01:00
|
|
|
}
|
2016-03-21 10:27:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
2016-03-21 10:46:00 +09:00
|
|
|
const post = this.props.post;
|
2016-03-23 14:27:08 +09:00
|
|
|
const user = this.context.currentUser;
|
2016-03-21 10:27:43 +09:00
|
|
|
|
2016-05-03 12:44:50 +09:00
|
|
|
const hasUpvoted = Users.hasUpvoted(user, post);
|
|
|
|
const hasDownvoted = Users.hasDownvoted(user, post);
|
|
|
|
const actionsClass = classNames(
|
|
|
|
"vote",
|
|
|
|
{voted: hasUpvoted || hasDownvoted},
|
|
|
|
{upvoted: hasUpvoted},
|
|
|
|
{downvoted: hasDownvoted}
|
|
|
|
);
|
2016-03-21 10:27:43 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={actionsClass}>
|
2016-03-28 11:41:22 +09:00
|
|
|
<a className="upvote-button" onClick={this.upvote}>
|
2016-11-02 14:50:51 +09:00
|
|
|
{this.state.loading ? <Telescope.components.Icon name="spinner" /> : <Telescope.components.Icon name="upvote" /> }
|
2016-03-24 16:03:30 +09:00
|
|
|
<div className="sr-only">Upvote</div>
|
2016-03-25 10:45:28 +09:00
|
|
|
<div className="vote-count">{post.baseScore || 0}</div>
|
2016-03-21 10:27:43 +09:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Vote.propTypes = {
|
2016-03-23 14:27:08 +09:00
|
|
|
post: React.PropTypes.object.isRequired, // the current post
|
2016-10-31 18:21:28 +01:00
|
|
|
vote: React.PropTypes.func, // mutate function with callback inside
|
2016-10-14 08:47:18 +02:00
|
|
|
};
|
2016-03-21 10:27:43 +09:00
|
|
|
|
2016-03-23 14:27:08 +09:00
|
|
|
Vote.contextTypes = {
|
2016-06-14 17:03:35 +09:00
|
|
|
currentUser: React.PropTypes.object,
|
|
|
|
actions: React.PropTypes.object,
|
|
|
|
events: React.PropTypes.object,
|
2016-03-23 14:27:08 +09:00
|
|
|
};
|
|
|
|
|
2016-10-31 18:21:28 +01:00
|
|
|
// graphql
|
|
|
|
const VoteWithMutation = graphql(gql`
|
|
|
|
mutation postVote($postId: String, $voteType: String) {
|
2016-11-02 09:53:16 +01:00
|
|
|
postVote(postId: $postId, voteType: $voteType) {
|
|
|
|
_id
|
|
|
|
baseScore
|
|
|
|
}
|
2016-10-31 18:21:28 +01:00
|
|
|
}
|
|
|
|
`, {
|
|
|
|
props: ({ownProps, mutate}) => ({
|
2016-11-02 16:31:15 +09:00
|
|
|
vote: ({post, voteType, currentUser}) => {
|
2016-11-02 09:53:16 +01:00
|
|
|
const { baseScore, _id } = Telescope.operateOnItem(Posts, post, currentUser, voteType, true);
|
2016-11-02 16:31:15 +09:00
|
|
|
|
2016-11-02 09:53:16 +01:00
|
|
|
// console.log("// votedItem")
|
|
|
|
// console.log(votedItem)
|
2016-11-02 16:31:15 +09:00
|
|
|
return mutate({
|
|
|
|
variables: {postId: post._id, voteType},
|
|
|
|
optimisticResponse: {
|
|
|
|
__typename: 'Mutation',
|
2016-11-02 09:53:16 +01:00
|
|
|
postVote: {
|
|
|
|
__typename: 'Post',
|
|
|
|
_id,
|
|
|
|
baseScore,
|
|
|
|
},
|
2016-11-02 16:31:15 +09:00
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
2016-10-31 18:21:28 +01:00
|
|
|
}),
|
|
|
|
})(Vote);
|
|
|
|
|
|
|
|
// redux state + actions for messages
|
2016-10-31 17:13:14 +01:00
|
|
|
const mapStateToProps = state => ({ messages: state.messages });
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
|
|
|
|
|
2016-10-31 18:21:28 +01:00
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(VoteWithMutation);
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(VoteWithMutation);
|