forked from mirror/pixelfed
Update likebutton component
This commit is contained in:
parent
03f4c23024
commit
dbfc401987
1 changed files with 17 additions and 13 deletions
16
resources/assets/js/components/likebutton.js
vendored
16
resources/assets/js/components/likebutton.js
vendored
|
@ -1,10 +1,9 @@
|
|||
$(document).ready(function() {
|
||||
|
||||
if(!ls.get('likes')) {
|
||||
pixelfed.fetchLikes = () => {
|
||||
axios.get('/api/v1/likes')
|
||||
.then(function (res) {
|
||||
ls.set('likes', res.data);
|
||||
console.log(res);
|
||||
})
|
||||
.catch(function (res) {
|
||||
ls.set('likes', []);
|
||||
|
@ -12,7 +11,7 @@ $(document).ready(function() {
|
|||
}
|
||||
|
||||
|
||||
pixelfed.hydrateLikes = function() {
|
||||
pixelfed.hydrateLikes = () => {
|
||||
var likes = ls.get('likes');
|
||||
$('.like-form').each(function(i, el) {
|
||||
var el = $(el);
|
||||
|
@ -20,11 +19,14 @@ $(document).ready(function() {
|
|||
var heart = el.find('.status-heart');
|
||||
|
||||
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();
|
||||
|
||||
$(document).on('submit', '.like-form', function(e) {
|
||||
|
@ -33,6 +35,8 @@ $(document).ready(function() {
|
|||
var id = el.data('id');
|
||||
axios.post('/i/like', {item: id})
|
||||
.then(function (res) {
|
||||
pixelfed.fetchLikes();
|
||||
pixelfed.hydrateLikes();
|
||||
var likes = ls.get('likes');
|
||||
var action = false;
|
||||
var counter = el.parents().eq(1).find('.like-count');
|
||||
|
@ -40,14 +44,14 @@ $(document).ready(function() {
|
|||
var heart = el.find('.status-heart');
|
||||
|
||||
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) {
|
||||
return item !== id
|
||||
});
|
||||
counter.text(count);
|
||||
action = 'unlike';
|
||||
} else {
|
||||
heart.removeClass('far fa-heart').addClass('fas fa-heart');
|
||||
heart.removeClass('text-dark').addClass('text-primary');
|
||||
likes.push(id);
|
||||
counter.text(count);
|
||||
action = 'like';
|
||||
|
|
Loading…
Reference in a new issue