2016-03-21 10:27:43 +09:00
|
|
|
import React, { PropTypes, Component } from 'react';
|
2016-03-23 11:37:15 +09:00
|
|
|
import Actions from "../actions.js";
|
2016-03-21 10:50:22 +09:00
|
|
|
import Core from "meteor/nova:core";
|
2016-05-03 12:44:50 +09:00
|
|
|
import classNames from 'classnames';
|
2016-03-21 10:50:22 +09:00
|
|
|
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;
|
2016-03-23 14:27:08 +09:00
|
|
|
const user = this.context.currentUser;
|
2016-03-21 10:27:43 +09:00
|
|
|
|
|
|
|
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)) {
|
2016-03-23 11:37:15 +09:00
|
|
|
Actions.call('posts.cancelUpvote', post._id, function(){
|
2016-03-21 10:27:43 +09:00
|
|
|
Events.track("post upvote cancelled", {'_id': post._id});
|
|
|
|
});
|
|
|
|
} else {
|
2016-03-23 11:37:15 +09:00
|
|
|
Actions.call('posts.upvote', post._id, function(){
|
2016-03-21 10:27:43 +09:00
|
|
|
Events.track("post upvoted", {'_id': post._id});
|
2016-03-23 11:37:15 +09:00
|
|
|
});
|
2016-03-21 10:27:43 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
({Icon} = Telescope.components);
|
|
|
|
|
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-03-21 10:27:43 +09:00
|
|
|
<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
|
|
|
|
// currentUser: React.PropTypes.object, // the current user
|
2016-03-21 10:27:43 +09:00
|
|
|
}
|
|
|
|
|
2016-03-23 14:27:08 +09:00
|
|
|
Vote.contextTypes = {
|
|
|
|
currentUser: React.PropTypes.object
|
|
|
|
};
|
|
|
|
|
2016-03-21 10:27:43 +09:00
|
|
|
module.exports = Vote;
|
|
|
|
export default Vote;
|