2017-03-23 16:27:59 +09:00
|
|
|
import { Components, registerComponent, withMessages } from 'meteor/vulcan:core';
|
2017-05-19 14:42:43 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2016-05-03 12:44:50 +09:00
|
|
|
import classNames from 'classnames';
|
2017-03-23 16:27:59 +09:00
|
|
|
import { withVote, hasUpvoted, hasDownvoted } from 'meteor/vulcan:voting';
|
2017-05-19 14:42:43 -06:00
|
|
|
import { /*FormattedMessage,*/ intlShape } from 'react-intl';
|
2016-03-21 10:50:22 +09:00
|
|
|
|
2017-05-19 14:42:43 -06:00
|
|
|
class Vote extends PureComponent {
|
2016-03-21 10:27:43 +09:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.upvote = this.upvote.bind(this);
|
2017-01-14 18:03:35 +09:00
|
|
|
this.getActionClass = this.getActionClass.bind(this);
|
2017-01-09 15:42:24 +09:00
|
|
|
// this.startLoading = this.startLoading.bind(this);
|
|
|
|
// this.stopLoading = this.stopLoading.bind(this);
|
2016-11-02 14:50:51 +09:00
|
|
|
this.state = {
|
|
|
|
loading: false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 15:42:24 +09:00
|
|
|
/*
|
2016-11-02 14:50:51 +09:00
|
|
|
|
2017-01-09 15:42:24 +09:00
|
|
|
note: with optimisitc UI, loading functions are not needed
|
|
|
|
also, setState triggers issues when the component is unmounted
|
|
|
|
before the vote mutation returns.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
// startLoading() {
|
|
|
|
// this.setState({ loading: true });
|
|
|
|
// }
|
|
|
|
|
|
|
|
// stopLoading() {
|
|
|
|
// this.setState({ loading: false });
|
|
|
|
// }
|
2016-03-21 10:27:43 +09:00
|
|
|
|
|
|
|
upvote(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
2017-01-09 15:42:24 +09:00
|
|
|
// this.startLoading();
|
2016-11-02 14:50:51 +09:00
|
|
|
|
2017-01-09 15:42:24 +09:00
|
|
|
const document = this.props.document;
|
|
|
|
const collection = this.props.collection;
|
2016-11-15 18:33:16 +01:00
|
|
|
const user = this.props.currentUser;
|
2016-03-21 10:27:43 +09:00
|
|
|
|
|
|
|
if(!user){
|
2017-02-06 10:43:55 +09:00
|
|
|
this.props.flash(this.context.intl.formatMessage({id: 'users.please_log_in'}));
|
2017-01-09 15:42:24 +09:00
|
|
|
// this.stopLoading();
|
2016-03-21 10:27:43 +09:00
|
|
|
} else {
|
2017-01-14 18:03:35 +09:00
|
|
|
const voteType = hasUpvoted(user, document) ? "cancelUpvote" : "upvote";
|
2017-01-09 15:42:24 +09:00
|
|
|
this.props.vote({document, voteType, collection, currentUser: this.props.currentUser}).then(result => {
|
|
|
|
// this.stopLoading();
|
2016-11-02 14:50:51 +09:00
|
|
|
});
|
2016-10-31 18:21:28 +01:00
|
|
|
}
|
2016-03-21 10:27:43 +09:00
|
|
|
}
|
|
|
|
|
2017-01-14 18:03:35 +09:00
|
|
|
getActionClass() {
|
2017-01-09 15:42:24 +09:00
|
|
|
const document = this.props.document;
|
2016-11-15 18:33:16 +01:00
|
|
|
const user = this.props.currentUser;
|
2016-03-21 10:27:43 +09:00
|
|
|
|
2017-01-14 18:03:35 +09:00
|
|
|
const isUpvoted = hasUpvoted(user, document);
|
|
|
|
const isDownvoted = hasDownvoted(user, document);
|
2016-05-03 12:44:50 +09:00
|
|
|
const actionsClass = classNames(
|
2017-01-14 18:03:35 +09:00
|
|
|
'vote',
|
|
|
|
{voted: isUpvoted || isDownvoted},
|
|
|
|
{upvoted: isUpvoted},
|
|
|
|
{downvoted: isDownvoted}
|
2016-05-03 12:44:50 +09:00
|
|
|
);
|
2016-03-21 10:27:43 +09:00
|
|
|
|
2017-01-14 18:03:35 +09:00
|
|
|
return actionsClass;
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2016-03-21 10:27:43 +09:00
|
|
|
return (
|
2017-01-14 18:03:35 +09:00
|
|
|
<div className={this.getActionClass()}>
|
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>
|
2017-01-14 18:03:35 +09:00
|
|
|
<div className="vote-count">{this.props.document.baseScore || 0}</div>
|
2016-03-21 10:27:43 +09:00
|
|
|
</a>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
Vote.propTypes = {
|
2017-05-19 14:42:43 -06:00
|
|
|
document: PropTypes.object.isRequired, // the document to upvote
|
|
|
|
collection: PropTypes.object.isRequired, // the collection containing the document
|
|
|
|
vote: PropTypes.func.isRequired, // mutate function with callback inside
|
|
|
|
currentUser: PropTypes.object, // user might not be logged in, so don't make it required
|
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 = {
|
2017-02-06 10:43:55 +09:00
|
|
|
intl: intlShape
|
2016-03-23 14:27:08 +09:00
|
|
|
};
|
|
|
|
|
2017-01-22 10:12:05 +09:00
|
|
|
registerComponent('Vote', Vote, withMessages, withVote);
|