window.addEventListener('click', function(event) {
if(event.target.classList.contains('card-btn') || event.target.classList.contains('adaptive-price')) {
const cardHTML = event.target.closest('.card')
const productInfo = {
img: cardHTML.querySelector('.card-img').classList,
name: cardHTML.querySelector('.card-title').innerText,
desc: cardHTML.querySelector('.card-desc').innerText,
price: cardHTML.querySelector('.card-price').innerText,
}
const cartCardHTML = `
<li class="card">
<ul class="card-box">
<li class="${productInfo.img}"></li>
<li class="card-content-box">
<ul>
<li class="delete-card-btn-box"><button class="delete-card-btn"></button></li>
<li class="card-title">${productInfo.name}</li>
<li class="card-desc">
${productInfo.desc}
</li>
<li>
<ul class="price-quantity-box">
<li class="quantity-box">
<button class="quantity-btn minus">-</button>
<p class="quantity">1</p>
<button class="quantity-btn plus">+</button>
</li>
<li class="price">${productInfo.price}</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
`
const alreadyInStorageCart = JSON.parse(localStorage.getItem('productInfo')) || "[]"
const productInfoStorage = localStorage.setItem('productInfo', JSON.stringify([...alreadyInStorageCart, cartCardHTML]))
}
})
const cartCardBox = document.querySelector('.cart-card-box')
if (localStorage.length > 0) {
cartCardBox.insertAdjacentHTML('beforeend', JSON.parse(localStorage.getItem('productInfo')))
}
window.addEventListener('click', function(event) {
if (event.target.classList.contains('delete-card-btn')) {
const Card = event.target.closest('.card')
Card.remove()
}
})
window.addEventListener('click', function(event) {
if (event.target.classList.contains('card-btn') || event.target.classList.contains('adaptive-price')) {
const cardHTML = event.target.closest('.card');
const productInfo = {
img: cardHTML.querySelector('.card-img').className, // Используйте className
name: cardHTML.querySelector('.card-title').innerText,
desc: cardHTML.querySelector('.card-desc').innerText,
price: cardHTML.querySelector('.card-price').innerText,
};
const alreadyInStorageCart = JSON.parse(localStorage.getItem('productInfo')) || [];
localStorage.setItem('productInfo', JSON.stringify([...alreadyInStorageCart, productInfo]));
}
});
const cartCardBox = document.querySelector('.cart-card-box');
if (localStorage.length > 0) {
const products = JSON.parse(localStorage.getItem('productInfo'));
products.forEach((product, index) => {
const cartCardHTML = `
<li class="card" data-index="${index}">
<ul class="card-box">
<li class="${product.img}"></li>
<li class="card-content-box">
<ul>
<li class="delete-card-btn-box"><button class="delete-card-btn">Delete</button></li>
<li class="card-title">${product.name}</li>
<li class="card-desc">${product.desc}</li>
<li class="price">${product.price}</li>
</ul>
</li>
</ul>
</li>
`;
cartCardBox.insertAdjacentHTML('beforeend', cartCardHTML);
});
}
window.addEventListener('click', function(event) {
if (event.target.classList.contains('delete-card-btn')) {
const card = event.target.closest('.card');
const index = parseInt(card.dataset.index, 10); // Получите индекс из атрибута data-index
const products = JSON.parse(localStorage.getItem('productInfo'));
products.splice(index, 1); // Удаляет элемент по индексу
localStorage.setItem('productInfo', JSON.stringify(products)); // Сохраняем измененный массив обратно в localStorage
card.remove(); // Удаление HTML карточки
}
});