1
0
Fork 0

Update likebutton component

This commit is contained in:
Daniel Supernault 2018-08-09 21:57:06 -06:00
parent 03f4c23024
commit dbfc401987
No known key found for this signature in database
GPG Key ID: 0DEF1C662C9033F7
1 changed files with 17 additions and 13 deletions

View File

@ -1,18 +1,17 @@
$(document).ready(function() { $(document).ready(function() {
if(!ls.get('likes')) { pixelfed.fetchLikes = () => {
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);
console.log(res); })
}) .catch(function (res) {
.catch(function (res) { ls.set('likes', []);
ls.set('likes', []); })
})
} }
pixelfed.hydrateLikes = function() { pixelfed.hydrateLikes = () => {
var likes = ls.get('likes'); var likes = ls.get('likes');
$('.like-form').each(function(i, el) { $('.like-form').each(function(i, el) {
var el = $(el); var el = $(el);
@ -20,11 +19,14 @@ $(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('far fa-heart').addClass('fas fa-heart'); heart.removeClass('text-dark').addClass('text-primary');
} else {
heart.removeClass('text-primary').addClass('text-dark');
} }
}); });
}; };
pixelfed.fetchLikes();
pixelfed.hydrateLikes(); pixelfed.hydrateLikes();
$(document).on('submit', '.like-form', function(e) { $(document).on('submit', '.like-form', function(e) {
@ -33,6 +35,8 @@ $(document).ready(function() {
var id = el.data('id'); var id = el.data('id');
axios.post('/i/like', {item: id}) axios.post('/i/like', {item: id})
.then(function (res) { .then(function (res) {
pixelfed.fetchLikes();
pixelfed.hydrateLikes();
var likes = ls.get('likes'); var likes = ls.get('likes');
var action = false; var action = false;
var counter = el.parents().eq(1).find('.like-count'); var counter = el.parents().eq(1).find('.like-count');
@ -40,14 +44,14 @@ $(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('fas fa-heart').addClass('far fa-heart'); heart.removeClass('text-primary').addClass('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('far fa-heart').addClass('fas fa-heart'); heart.removeClass('text-dark').addClass('text-primary');
likes.push(id); likes.push(id);
counter.text(count); counter.text(count);
action = 'like'; action = 'like';