-
Notifications
You must be signed in to change notification settings - Fork 14
/
cart.js
98 lines (91 loc) · 3.57 KB
/
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
import products from "./products.js";
const cart = () => {
let listCartHTML = document.querySelector('.listCart');
let iconCart = document.querySelector('.icon-cart');
let iconCartSpan = iconCart.querySelector('span');
let body = document.querySelector('body');
let closeCart = document.querySelector('.close');
let cart = [];
// open and close tab
iconCart.addEventListener('click', () => {
body.classList.toggle('activeTabCart');
})
closeCart.addEventListener('click', () => {
body.classList.toggle('activeTabCart');
})
const setProductInCart = (idProduct, value) => {
let positionThisProductInCart = cart.findIndex((value) => value.product_id == idProduct);
if(value <= 0){
cart.splice(positionThisProductInCart, 1);
}else if(positionThisProductInCart < 0){
cart.push({
product_id: idProduct,
quantity: 1
});
}else{
cart[positionThisProductInCart].quantity = value;
}
localStorage.setItem('cart', JSON.stringify(cart));
addCartToHTML();
}
const addCartToHTML = () => {
listCartHTML.innerHTML = '';
let totalQuantity = 0;
if(cart.length > 0){
cart.forEach(item => {
totalQuantity = totalQuantity + item.quantity;
let newItem = document.createElement('div');
newItem.classList.add('item');
newItem.dataset.id = item.product_id;
let positionProduct = products.findIndex((value) => value.id == item.product_id);
let info = products[positionProduct];
listCartHTML.appendChild(newItem);
newItem.innerHTML = `
<div class="image">
<img src="${info.image}">
</div>
<div class="name">
${info.name}
</div>
<div class="totalPrice">$${info.price * item.quantity}</div>
<div class="quantity">
<span class="minus" data-id="${info.id}"><</span>
<span>${item.quantity}</span>
<span class="plus" data-id="${info.id}">></span>
</div>
`;
})
}
iconCartSpan.innerText = totalQuantity;
}
document.addEventListener('click', (event) => {
let buttonClick = event.target;
let idProduct = buttonClick.dataset.id;
let quantity = null;
let positionProductInCart = cart.findIndex((value) => value.product_id == idProduct);
switch (true) {
case (buttonClick.classList.contains('addCart')):
quantity = (positionProductInCart < 0) ? 1 : cart[positionProductInCart].quantity+1;
setProductInCart(idProduct, quantity);
break;
case (buttonClick.classList.contains('minus')):
quantity = cart[positionProductInCart].quantity-1;
setProductInCart(idProduct, quantity);
break;
case (buttonClick.classList.contains('plus')):
quantity = cart[positionProductInCart].quantity+1;
setProductInCart(idProduct, quantity);
break;
default:
break;
}
})
const initApp = () => {
if(localStorage.getItem('cart')){
cart = JSON.parse(localStorage.getItem('cart'));
addCartToHTML();
}
}
initApp();
}
export default cart;