commentVote.js 1.17 KB
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);
        }
    });
}