2016-03-21 10:27:43 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
|
|
|
|
2016-03-21 10:50:22 +09:00
|
|
|
import Core from "meteor/nova:core";
|
|
|
|
const Messages = Core.Messages;
|
|
|
|
|
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;
|
|
|
|
const user = this.props.currentUser;
|
|
|
|
|
|
|
|
if(!user){
|
2016-03-21 10:50:22 +09:00
|
|
|
Messages.flash("Please log in first");
|
2016-03-21 10:27:43 +09:00
|
|
|
} else if (user.hasUpvoted(post)) {
|
|
|
|
Meteor.call('posts.cancelUpvote', post._id, function(){
|
|
|
|
Events.track("post upvote cancelled", {'_id': post._id});
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
Meteor.call('posts.upvote', post._id, function(){
|
|
|
|
Events.track("post upvoted", {'_id': post._id});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
({Icon} = Telescope.components);
|
|
|
|
|
2016-03-21 10:46:00 +09:00
|
|
|
const post = this.props.post;
|
2016-03-21 10:27:43 +09:00
|
|
|
const user = this.props.currentUser;
|
|
|
|
|
|
|
|
let actionsClass = "vote";
|
2016-03-21 10:50:22 +09:00
|
|
|
if (Users.hasUpvoted(user, post)) actionsClass += " voted upvoted";
|
|
|
|
if (Users.hasDownvoted(user, post)) actionsClass += " voted downvoted";
|
2016-03-21 10:27:43 +09:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className={actionsClass}>
|
2016-03-21 10:46:00 +09:00
|
|
|
<a href="#" className="button button--secondary upvote" onClick={this.upvote}>
|
2016-03-21 10:27:43 +09:00
|
|
|
<Icon name="upvote" />
|
|
|
|
<span className="sr-only">Upvote</span>
|
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Vote.propTypes = {
|
|
|
|
post: React.PropTypes.object.isRequired, // the current comment
|
|
|
|
currentUser: React.PropTypes.object, // the current user
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Vote;
|
|
|
|
export default Vote;
|