/* Создаем объект корзины и сохраняем его в localStorage */
const myCart = new Cart();
if (localStorage.getItem("cart") == null) {
localStorage.setItem("cart", JSON.stringify(myCart));
}
const savedCart = JSON.parse(localStorage.getItem("cart"));
myCart.products = savedCart.products;
cartNum.textContent = myCart.count;
/* Добавляем товар в корзину */
myCart.products = cardAddArr.forEach((cardAdd) => {
cardAdd.addEventListener("click", (e) => {
e.preventDefault();
const card = e.target.closest(".card");
const product = new Product(card);
const savedCart = JSON.parse(localStorage.getItem("cart"));
myCart.products = savedCart.products;
myCart.addProduct(product);
localStorage.setItem("cart", JSON.stringify(myCart));
cartNum.textContent = myCart.count;
});
});
/* Удаление продукта */
const productDelete = document.createElement("button");
productDelete.classList.add("btn-del");
productDelete.innerHTML = "✖";
productDelete.addEventListener("click", () => {
myCart.removeProduct(product);
localStorage.setItem("cart", JSON.stringify(myCart));
});