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-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);
|
|
|
|
}
|
|
|
|
|
|
|
|
upvote(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
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-10-19 17:38:21 +02:00
|
|
|
} else if (Users.hasUpvoted(user, post)) {
|
2016-06-27 17:35:53 +02:00
|
|
|
this.context.actions.call('posts.cancelUpvote', post._id, () => {
|
2016-10-25 15:48:54 +02:00
|
|
|
this.props.refetchQuery();
|
2016-06-14 17:03:35 +09:00
|
|
|
this.context.events.track("post upvote cancelled", {'_id': post._id});
|
2016-03-21 10:27:43 +09:00
|
|
|
});
|
|
|
|
} else {
|
2016-06-27 17:35:53 +02:00
|
|
|
this.context.actions.call('posts.upvote', post._id, () => {
|
2016-10-25 15:48:54 +02:00
|
|
|
this.props.refetchQuery();
|
2016-06-14 17:03:35 +09:00
|
|
|
this.context.events.track("post upvoted", {'_id': post._id});
|
2016-03-23 11:37:15 +09: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-05-22 16:42:24 +09:00
|
|
|
<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-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-10-31 17:13:14 +01:00
|
|
|
//messages: React.PropTypes.object
|
2016-03-23 14:27:08 +09:00
|
|
|
};
|
|
|
|
|
2016-10-31 17:13:14 +01:00
|
|
|
const mapStateToProps = state => ({ messages: state.messages });
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
|
|
|
|
|
|
|
|
module.exports = connect(mapStateToProps, mapDispatchToProps)(Vote);
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(Vote);
|