Skip to content

Commit

Permalink
add function to favorite button
Browse files Browse the repository at this point in the history
  • Loading branch information
HectorVilas committed Apr 27, 2023
1 parent ef867a3 commit 0da4063
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 16 additions & 0 deletions modules/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ export function addToCart() {
thisCardMainBtn.setAttribute('disabled', true);
}
}

export function toggleFavorite() {
const favIcon = document.querySelector('.nav-favorites');
const favCount = document.querySelector('.nav-favorites-count');
const addSubstract = this.checked ? 1 : -1;

favCount.textContent = parseInt(favCount.textContent) + addSubstract;

if (parseInt(favCount.textContent) === 0) {
favCount.style.backgroundColor = '#201E1F';
favIcon.style.backgroundImage = 'url(./media/images/icons/favorites.png)';
} else {
favCount.style.backgroundColor = '#1F604A';
favIcon.style.backgroundImage = 'url(./media/images/icons/favorites-active.png)';
};
}
3 changes: 2 additions & 1 deletion modules/shopCard.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addToCart } from "./functions.js";
import { addToCart, toggleFavorite } from "./functions.js";

export default function shopCard(id, imgUrl, category, name, price, amount = 0){
const card = document.createElement('div');
Expand Down Expand Up @@ -45,6 +45,7 @@ export default function shopCard(id, imgUrl, category, name, price, amount = 0){
[btn, btnAdd, btnRemove].forEach((button) => {
button.addEventListener('click', addToCart);
});
favCheckbox.addEventListener('click', toggleFavorite);

btnDiv.append(btn, btnRemove, btnAdd);
img.append(imgHover);
Expand Down

0 comments on commit 0da4063

Please sign in to comment.