Skip to content

Commit

Permalink
add/remove favs and cart on localStorage
Browse files Browse the repository at this point in the history
  • Loading branch information
HectorVilas committed May 2, 2023
1 parent 8ef4aaa commit 849f3d4
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions modules/functions.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import home from "./pages/home.js";
import shop from "./pages/shop.js";
import { storedData } from "./storedData.js";

export function addToCart() {
const thisCard = this.closest('.shop-card');
const thisCardMainBtn = thisCard.querySelector('.shop-card-buy');
const itemId = thisCard.dataset.itemId;
const itemId = parseInt(thisCard.dataset.itemId);
const cart = document.querySelector('.nav-cart');
const cartCount = document.querySelector('.nav-cart-count');

Expand All @@ -28,12 +29,21 @@ export function addToCart() {
thisCardMainBtn.textContent = `${newAmount} Item${newAmount > 1 ? 's' : ''} added`;
thisCardMainBtn.setAttribute('disabled', true);
}

if (newAmount > amount) {
storedData.addToCart(itemId);
} else {
storedData.removeFromCart(itemId);
}
}

export function toggleFavorite() {
const favIcon = document.querySelector('.nav-favorites');
const favCount = document.querySelector('.nav-favorites-count');
const addSubstract = this.checked ? 1 : -1;
const thisCard = this.closest('.shop-card');
const itemId = parseInt(thisCard.dataset.itemId);
const isChecked = this.checked;
const addSubstract = isChecked ? 1 : -1;

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

Expand All @@ -43,6 +53,12 @@ export function toggleFavorite() {
} else {
favIcon.classList.add('has-content');
};

if (isChecked) {
storedData.addFav(itemId);
} else {
storedData.removeFav(itemId);
}
}

export function goToPage(page) {
Expand Down

0 comments on commit 849f3d4

Please sign in to comment.