Update likebutton.js

This commit is contained in:
Daniel Supernault 2018-11-10 20:56:19 -07:00
parent 7e559f9504
commit 329956b60b
No known key found for this signature in database
GPG key ID: 0DEF1C662C9033F7

View file

@ -1,12 +1,22 @@
$(document).ready(function() { $(document).ready(function() {
pixelfed.fetchLikes = () => { pixelfed.fetchLikes = () => {
let ts = Date.now();
let offset = ts - 900000;
let updated = ls.get('likesUpdated');
if(updated != null && ls.get('likes').length > 0 || offset < updated) {
return;
}
axios.get('/api/v1/likes') axios.get('/api/v1/likes')
.then(function (res) { .then(function (res) {
ls.set('likes', res.data); ls.set('likes', res.data);
ls.set('likesUpdated', ts);
}) })
.catch(function (res) { .catch(function (res) {
ls.set('likes', []); ls.set('likes', []);
ls.set('likesUpdated', ts);
}) })
} }
@ -19,9 +29,9 @@ $(document).ready(function() {
var heart = el.find('.status-heart'); var heart = el.find('.status-heart');
if(likes.indexOf(id) != -1) { if(likes.indexOf(id) != -1) {
heart.removeClass('text-dark').addClass('text-primary'); heart.removeClass('far text-dark').addClass('fas text-danger');
} else { } else {
heart.removeClass('text-primary').addClass('text-dark'); heart.removeClass('fas text-danger').addClass('far text-dark');
} }
}); });
}; };
@ -44,20 +54,21 @@ $(document).ready(function() {
var heart = el.find('.status-heart'); var heart = el.find('.status-heart');
if(likes.indexOf(id) > -1) { if(likes.indexOf(id) > -1) {
heart.removeClass('text-primary').addClass('text-dark'); heart.removeClass('fas text-danger').addClass('far text-dark');
likes = likes.filter(function(item) { likes = likes.filter(function(item) {
return item !== id return item !== id
}); });
counter.text(count); counter.text(count);
action = 'unlike'; action = 'unlike';
} else { } else {
heart.removeClass('text-dark').addClass('text-primary'); heart.removeClass('far text-dark').addClass('fas text-danger');
likes.push(id); likes.push(id);
counter.text(count); counter.text(count);
action = 'like'; action = 'like';
} }
ls.set('likes', likes); ls.set('likes', likes);
ls.set('likesUpdated', Date.now());
console.log(action + ' - ' + id + ' like event'); console.log(action + ' - ' + id + ' like event');
}); });
}); });