Vulcan/packages/vulcan-voting/lib/components/VoteB.jsx

90 lines
2.4 KiB
React
Raw Normal View History

2017-09-27 17:15:49 +02:00
/*
This variant of the Vote.jsx component implements a loading spinner instead of
optimistic response
*/
2017-09-04 18:37:21 +09:00
import { Components, registerComponent, withMessages } from 'meteor/vulcan:core';
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
2017-09-27 17:15:49 +02:00
import { withVote } from '../containers/withVote.js';
2017-09-04 18:37:21 +09:00
class Vote extends PureComponent {
constructor() {
super();
this.upvote = this.upvote.bind(this);
2017-09-27 17:15:49 +02:00
this.hasVoted = this.hasVoted.bind(this);
2017-09-04 18:37:21 +09:00
this.getActionClass = this.getActionClass.bind(this);
2017-09-27 17:15:49 +02:00
this.startLoading = this.startLoading.bind(this);
this.stopLoading = this.stopLoading.bind(this);
2017-09-04 18:37:21 +09:00
this.state = {
loading: false
}
}
2017-09-27 17:15:49 +02:00
startLoading() {
this.setState({ loading: true });
}
2017-09-04 18:37:21 +09:00
2017-09-27 17:15:49 +02:00
stopLoading() {
this.setState({ loading: false });
}
2017-09-04 18:37:21 +09:00
2017-09-27 17:15:49 +02:00
hasVoted() {
return this.props.document.currentUserVotes && this.props.document.currentUserVotes.length;
}
2017-09-04 18:37:21 +09:00
upvote(e) {
e.preventDefault();
2017-09-27 17:15:49 +02:00
this.startLoading();
2017-09-04 18:37:21 +09:00
const document = this.props.document;
const collection = this.props.collection;
const user = this.props.currentUser;
if(!user){
this.props.flash(this.context.intl.formatMessage({id: 'users.please_log_in'}));
2017-09-27 17:15:49 +02:00
this.stopLoading();
2017-09-04 18:37:21 +09:00
} else {
2017-09-27 17:15:49 +02:00
const operationType = this.hasVoted() ? 'cancelVote' : 'upvote';
2017-09-25 22:09:09 +02:00
this.props.vote({document, operationType, collection, currentUser: this.props.currentUser}).then(result => {
2017-09-27 17:15:49 +02:00
this.stopLoading();
2017-09-04 18:37:21 +09:00
});
}
}
getActionClass() {
const actionsClass = classNames(
'vote',
2017-09-27 17:15:49 +02:00
{voted: this.hasVoted()},
2017-09-04 18:37:21 +09:00
);
return actionsClass;
}
render() {
return (
<div className={this.getActionClass()}>
<a className="upvote-button" onClick={this.upvote}>
{this.state.loading ? <Components.Icon name="spinner" /> : <Components.Icon name="upvote" /> }
<div className="sr-only">Upvote</div>
<div className="vote-count">{this.props.document.baseScore || 0}</div>
</a>
</div>
)
}
}
Vote.propTypes = {
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
};
2017-09-27 17:15:49 +02:00
registerComponent('Upvote', Vote, withMessages, withVote);