pixelfed/resources/assets/js/components/likebutton.js

59 lines
1.5 KiB
JavaScript
Raw Normal View History

2018-05-20 03:11:58 +00:00
$(document).ready(function() {
2018-06-04 02:20:39 +00:00
2018-05-20 03:11:58 +00:00
if(!ls.get('likes')) {
2018-06-04 02:20:39 +00:00
axios.get('/api/v1/likes')
.then(function (res) {
ls.set('likes', res.data);
console.log(res);
})
.catch(function (res) {
ls.set('likes', []);
})
}
hydrateLikes();
function hydrateLikes() {
var likes = ls.get('likes');
$('.like-form').each(function(i, el) {
var el = $(el);
var id = el.data('id');
var heart = el.find('.status-heart');
if(likes.indexOf(id) != -1) {
heart.addClass('fas fa-heart').removeClass('far fa-heart');
}
});
2018-05-20 03:11:58 +00:00
}
$('.like-form').submit(function(e) {
e.preventDefault();
var el = $(this);
var id = el.data('id');
var res = axios.post('/i/like', {item: id});
var likes = ls.get('likes');
var action = false;
2018-06-04 02:20:39 +00:00
var counter = el.parents().eq(2).find('.like-count');
2018-05-20 03:11:58 +00:00
var count = parseInt(counter.text());
2018-06-04 02:20:39 +00:00
var heart = el.find('.status-heart');
2018-05-20 03:11:58 +00:00
if(likes.indexOf(id) > -1) {
2018-06-04 02:20:39 +00:00
heart.addClass('far fa-heart').removeClass('fas fa-heart');
likes = likes.filter(function(item) {
return item !== id
});
count = count == 0 ? 0 : count--;
2018-05-20 03:11:58 +00:00
counter.text(count);
action = 'unlike';
} else {
2018-06-04 02:20:39 +00:00
heart.addClass('fas fa-heart').removeClass('far fa-heart');
2018-05-20 03:11:58 +00:00
likes.push(id);
count++;
counter.text(count);
action = 'like';
}
2018-06-04 02:20:39 +00:00
2018-05-20 03:11:58 +00:00
ls.set('likes', likes);
console.log(action + ' - ' + $(this).data('id') + ' like event');
});
});