38 lines
1.4 KiB
JavaScript
38 lines
1.4 KiB
JavaScript
export class LikesView {
|
||
constructor() {
|
||
this.likeButtons = document.querySelectorAll('.like-btn');
|
||
}
|
||
|
||
bindLikeButton(handler) {
|
||
this.likeButtons.forEach(button => {
|
||
button.addEventListener('click', async (e) => {
|
||
const card = e.target.closest('.card');
|
||
const productId = card.dataset.id;
|
||
const isLiked = button.classList.contains('liked');
|
||
|
||
if (isLiked) {
|
||
await handler(productId, 'remove');
|
||
button.classList.remove('liked');
|
||
button.innerHTML = '<i class="bi bi-heart"></i> В избранное';
|
||
} else {
|
||
await handler(productId, 'add');
|
||
button.classList.add('liked');
|
||
button.innerHTML = '<i class="bi bi-heart-fill"></i> В избранном';
|
||
}
|
||
});
|
||
});
|
||
}
|
||
|
||
updateLikeButton(productId, isLiked) {
|
||
const buttons = document.querySelectorAll(`.like-btn[data-id="${productId}"]`);
|
||
buttons.forEach(button => {
|
||
if (isLiked) {
|
||
button.classList.add('liked');
|
||
button.innerHTML = '<i class="bi bi-heart-fill"></i> В избранном';
|
||
} else {
|
||
button.classList.remove('liked');
|
||
button.innerHTML = '<i class="bi bi-heart"></i> В избранное';
|
||
}
|
||
});
|
||
}
|
||
} |