2016-12-06 18:06:29 +01:00
|
|
|
import Telescope, { Components, registerComponent } 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-06-23 15:00:58 +09:00
|
|
|
import Users from 'meteor/nova:users';
|
2016-11-11 18:29:49 +09:00
|
|
|
import { bindActionCreators } from 'redux';
|
|
|
|
import { connect } from 'react-redux';
|
2016-11-30 16:58:28 +09:00
|
|
|
import { withCurrentUser } from 'meteor/nova:core';
|
2016-11-29 18:52:13 +09:00
|
|
|
import { withVote, hasUpvoted, hasDownvoted } from 'meteor/nova:voting';
|
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);
|
2016-11-29 12:27:26 +01:00
|
|
|
|
|
|
|
this.hasUpvoted = hasUpvoted;
|
|
|
|
this.hasDownvoted = hasDownvoted;
|
|
|
|
|
2016-11-02 14:50:51 +09:00
|
|
|
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-11-15 18:33:16 +01:00
|
|
|
const user = this.props.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-11-29 12:27:26 +01:00
|
|
|
const voteType = this.hasUpvoted(user, post) ? "cancelUpvote" : "upvote";
|
2016-11-15 18:33:16 +01:00
|
|
|
this.props.vote({post, voteType, currentUser: this.props.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-11-29 12:27:26 +01:00
|
|
|
// uncomment for debug:
|
|
|
|
// console.log('hasUpvoted', hasUpvoted);
|
|
|
|
// console.log('this.hasUpvoted', this.hasUpvoted);
|
|
|
|
|
2016-03-21 10:46:00 +09:00
|
|
|
const post = this.props.post;
|
2016-11-15 18:33:16 +01:00
|
|
|
const user = this.props.currentUser;
|
2016-03-21 10:27:43 +09:00
|
|
|
|
2016-11-29 12:27:26 +01:00
|
|
|
const hasUpvoted = this.hasUpvoted(user, post);
|
|
|
|
const hasDownvoted = this.hasDownvoted(user, post);
|
2016-05-03 12:44:50 +09:00
|
|
|
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-12-06 18:06:29 +01:00
|
|
|
{this.state.loading ? <Components.Icon name="spinner" /> : <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-11-15 18:33:16 +01:00
|
|
|
currentUser: React.PropTypes.object,
|
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
|
|
|
actions: React.PropTypes.object,
|
|
|
|
events: React.PropTypes.object,
|
2016-03-23 14:27:08 +09:00
|
|
|
};
|
|
|
|
|
2016-11-11 18:29:49 +09:00
|
|
|
// redux state + actions for messages
|
|
|
|
const mapStateToProps = state => ({ messages: state.messages });
|
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators(Telescope.actions.messages, dispatch);
|
|
|
|
|
2016-12-06 18:06:29 +01:00
|
|
|
registerComponent('Vote', Vote, withCurrentUser, connect(mapStateToProps, mapDispatchToProps), withVote);
|