1
0
Fork 1
mirror of https://github.com/pixelfed/pixelfed.git synced 2024-12-27 18:26:50 +00:00
pixelfed/resources/assets/js/components/likebutton.js

75 lines
2 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-08-10 03:57:06 +00:00
pixelfed.fetchLikes = () => {
2018-11-11 03:56:19 +00:00
let ts = Date.now();
let offset = ts - 900000;
let updated = ls.get('likesUpdated');
if(updated != null && ls.get('likes').length > 0 || offset < updated) {
return;
}
2018-08-10 03:57:06 +00:00
axios.get('/api/v1/likes')
.then(function (res) {
ls.set('likes', res.data);
2018-11-11 03:56:19 +00:00
ls.set('likesUpdated', ts);
2018-08-10 03:57:06 +00:00
})
.catch(function (res) {
ls.set('likes', []);
2018-11-11 03:56:19 +00:00
ls.set('likesUpdated', ts);
2018-08-10 03:57:06 +00:00
})
2018-06-04 02:20:39 +00:00
}
2018-08-10 03:57:06 +00:00
pixelfed.hydrateLikes = () => {
2018-06-04 02:20:39 +00:00
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) {
2018-11-11 03:56:19 +00:00
heart.removeClass('far text-dark').addClass('fas text-danger');
2018-08-10 03:57:06 +00:00
} else {
2018-11-11 03:56:19 +00:00
heart.removeClass('fas text-danger').addClass('far text-dark');
2018-06-04 02:20:39 +00:00
}
});
};
2018-08-10 03:57:06 +00:00
pixelfed.fetchLikes();
pixelfed.hydrateLikes();
2018-05-20 03:11:58 +00:00
$(document).on('submit', '.like-form', function(e) {
2018-05-20 03:11:58 +00:00
e.preventDefault();
var el = $(this);
var id = el.data('id');
2018-06-04 02:46:55 +00:00
axios.post('/i/like', {item: id})
.then(function (res) {
2018-08-10 03:57:06 +00:00
pixelfed.fetchLikes();
pixelfed.hydrateLikes();
2018-06-04 02:46:55 +00:00
var likes = ls.get('likes');
var action = false;
2018-06-04 05:40:17 +00:00
var counter = el.parents().eq(1).find('.like-count');
2018-06-04 02:46:55 +00:00
var count = res.data.count;
var heart = el.find('.status-heart');
2018-06-04 02:20:39 +00:00
2018-06-04 02:46:55 +00:00
if(likes.indexOf(id) > -1) {
2018-11-11 03:56:19 +00:00
heart.removeClass('fas text-danger').addClass('far text-dark');
2018-06-04 02:46:55 +00:00
likes = likes.filter(function(item) {
return item !== id
});
counter.text(count);
action = 'unlike';
} else {
2018-11-11 03:56:19 +00:00
heart.removeClass('far text-dark').addClass('fas text-danger');
2018-06-04 02:46:55 +00:00
likes.push(id);
counter.text(count);
action = 'like';
}
2018-06-04 02:20:39 +00:00
2018-06-04 02:46:55 +00:00
ls.set('likes', likes);
2018-11-11 03:56:19 +00:00
ls.set('likesUpdated', Date.now());
2018-06-04 05:40:17 +00:00
console.log(action + ' - ' + id + ' like event');
2018-06-04 02:46:55 +00:00
});
2018-05-20 03:11:58 +00:00
});
});