Files
PIbd-23_Baryshev_D.A._Inter…/js/likes/view.js

38 lines
1.4 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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> В избранное';
}
});
}
}