-
Notifications
You must be signed in to change notification settings - Fork 34
/
shopping-cart.js
103 lines (94 loc) · 3.04 KB
/
shopping-cart.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
let productsInCart = JSON.parse(localStorage.getItem('shoppingCart'));
if(!productsInCart){
productsInCart = [];
}
const parentElement = document.querySelector('#buyItems');
const cartSumPrice = document.querySelector('#sum-prices');
const products = document.querySelectorAll('.product-under');
const countTheSumPrice = function () { // 4
let sum = 0;
productsInCart.forEach(item => {
sum += item.price;
});
return sum;
}
const updateShoppingCartHTML = function () { // 3
localStorage.setItem('shoppingCart', JSON.stringify(productsInCart));
if (productsInCart.length > 0) {
let result = productsInCart.map(product => {
return `
<li class="buyItem">
<img src="${product.image}">
<div>
<h5>${product.name}</h5>
<h6>$${product.price}</h6>
<div>
<button class="button-minus" data-id=${product.id}>-</button>
<span class="countOfProduct">${product.count}</span>
<button class="button-plus" data-id=${product.id}>+</button>
</div>
</div>
</li>`
});
parentElement.innerHTML = result.join('');
document.querySelector('.checkout').classList.remove('hidden');
cartSumPrice.innerHTML = '$' + countTheSumPrice();
}
else {
document.querySelector('.checkout').classList.add('hidden');
parentElement.innerHTML = '<h4 class="empty">Your shopping cart is empty</h4>';
cartSumPrice.innerHTML = '';
}
}
function updateProductsInCart(product) { // 2
for (let i = 0; i < productsInCart.length; i++) {
if (productsInCart[i].id == product.id) {
productsInCart[i].count += 1;
productsInCart[i].price = productsInCart[i].basePrice * productsInCart[i].count;
return;
}
}
productsInCart.push(product);
}
products.forEach(item => { // 1
item.addEventListener('click', (e) => {
if (e.target.classList.contains('addToCart')) {
const productID = e.target.dataset.productId;
const productName = item.querySelector('.productName').innerHTML;
const productPrice = item.querySelector('.priceValue').innerHTML;
const productImage = item.querySelector('img').src;
let product = {
name: productName,
image: productImage,
id: productID,
count: 1,
price: +productPrice,
basePrice: +productPrice,
}
updateProductsInCart(product);
updateShoppingCartHTML();
}
});
});
parentElement.addEventListener('click', (e) => { // Last
const isPlusButton = e.target.classList.contains('button-plus');
const isMinusButton = e.target.classList.contains('button-minus');
if (isPlusButton || isMinusButton) {
for (let i = 0; i < productsInCart.length; i++) {
if (productsInCart[i].id == e.target.dataset.id) {
if (isPlusButton) {
productsInCart[i].count += 1
}
else if (isMinusButton) {
productsInCart[i].count -= 1
}
productsInCart[i].price = productsInCart[i].basePrice * productsInCart[i].count;
}
if (productsInCart[i].count <= 0) {
productsInCart.splice(i, 1);
}
}
updateShoppingCartHTML();
}
});
updateShoppingCartHTML();