commentVote.js
1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import showErrors from '../common/showErrors';
/** Click handler for voting on comments.
*
* @param int commentID
* @param int vote either -1 or +1
* @param int postID
*/
export default function (commentID, vote, postID) {
console.log(commentID, vote, postID);
postID = (typeof postID === 'undefined') ? '0' : postID;
$.ajax({
url: '/comments/' + commentID + '/vote',
type: 'POST',
data: {
vote: vote,
postID: postID,
},
})
.done(function () {
var currentVotes = parseInt($('#comment' + commentID + ' .comment-score').html(), 10);
var newVotes = currentVotes + vote;
$('#comment' + commentID + ' .comment-score').html(newVotes + ' ');
// show or hide votes depending wether it's 0 or not
if (newVotes !== 0) {
$('#comment' + commentID + ' .comment-score').show();
} else {
$('#comment' + commentID + ' .comment-score').hide();
}
})
.fail(function (jqXHR) {
if (jqXHR.responseJSON.errors) {
showErrors(jqXHR.responseJSON.errors, '.comment-action-errors', '#comment' + commentID);
}
});
}