Files
pillar/src/scripts/js/es6/common/vuecomponents/comments/Rating.js
Tobias Johansson 1101b8e716 Fix Regression: Heart filled icon was shown on all voted comments
Heart filled icon should be an indication that the current user has
voted. Thanks to Pablo Vazques for pointing it out
2019-02-04 10:16:50 +01:00

53 lines
1.8 KiB
JavaScript

import { EventBus, Events } from './EventBus'
import { UnitOfWorkTracker } from '../mixins/UnitOfWorkTracker'
import { thenVoteComment } from '../../api/comments'
const TEMPLATE = `
<div class="comment-rating"
:class="{rated: currentUserHasRated, positive: currentUserRatedPositive }"
>
<div class="comment-rating-value" title="Number of likes">{{ rating }}</div>
<div class="comment-action-rating up" title="Like comment"
v-if="canVote"
@click="upVote"
/>
</div>
`;
Vue.component('comment-rating', {
template: TEMPLATE,
mixins: [UnitOfWorkTracker],
props: {comment: Object},
computed: {
positiveRating() {
return this.comment.properties.rating_positive || 0;
},
negativeRating() {
return this.comment.properties.rating_negative || 0;
},
rating() {
return this.positiveRating - this.negativeRating;
},
currentUserRatedPositive() {
return this.comment.current_user_rating === true;
},
currentUserHasRated() {
return typeof this.comment.current_user_rating === "boolean" ;
},
canVote() {
return this.comment.user.id !== pillar.utils.getCurrentUser().user_id;
}
},
methods: {
upVote() {
let vote = this.comment.current_user_rating === true ? 0 : 1; // revoke if set
this.unitOfWork(
thenVoteComment(this.comment.parent, this.comment.id, vote)
.then((updatedComment) => {
EventBus.$emit(Events.UPDATED_COMMENT, updatedComment);
})
.fail((err) => {toastr.error(pillar.utils.messageFromError(err), 'Faied to vote on comment')})
);
}
}
});