-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathlike.js
37 lines (32 loc) · 1.1 KB
/
like.js
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
/* eslint-disable no-empty-label */
/* Like button functionality of nodes */
function clickliked() {
var node_id = $(this).attr('node-id');
changeLikeStatus(node_id, "/delete");
}
function clicknotliked() {
var node_id = $(this).attr('node-id');
changeLikeStatus(node_id, "/create");
}
function changeLikeStatus(node_id, method) {
$('#like-button-' + node_id).off();
$.getJSON("/likes/node/" + node_id + method)
.then(function(resp) {
updateLikeCount(parseInt(resp), node_id);
renderLikeStar(parseInt(resp), node_id);
})
.then(function(resp) {
let method1 = method === "/delete" ? clicknotliked : clickliked
$('#like-button-' + node_id).on('click', method1);
});
}
function updateLikeCount(value, node_id) {
var count = $('#like-count-' + node_id).html();
count = parseInt(count) + value;
$('#like-count-' + node_id).html(count);
}
// where fa fa-star-o is a clear star (indicating you are not currently liking)
function renderLikeStar(value, node_id) {
let name = value === -1 ? "fa fa-star-o" : "fa fa-star"
$('#like-star-' + node_id)[0].className = name;
}