-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
130 lines (113 loc) · 3.59 KB
/
app.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// SELECT ELEMENTS
const productsEl = document.querySelector(".products");
const cartItemsEl = document.querySelector(".cart-items");
const subtotalEl = document.querySelector(".subtotal");
const totalItemsInCartEl = document.querySelector(".total-items-in-cart");
// RENDER PRODUCTS
function renderProdcuts() {
products.forEach((product) => {
productsEl.innerHTML += `
<div class="item">
<div class="item-container">
<div class="item-img">
<img src="${product.imgSrc}" alt="${product.name}">
</div>
<div class="desc">
<h2>${product.name}</h2>
<h2><small>$</small>${product.price}</h2>
<p>
${product.description}
</p>
</div>
<div class="add-to-wishlist">
<img src="./icons/heart.png" alt="add to wish list">
</div>
<div class="add-to-cart" onclick="addToCart(${product.id})">
<img src="./icons/bag-plus.png" alt="add to cart">
</div>
</div>
</div>
`;
});
}
renderProdcuts();
// cart array
let cart = JSON.parse(localStorage.getItem("CART")) || [];
updateCart();
// ADD TO CART
function addToCart(id) {
// check if prodcut already exist in cart
if (cart.some((item) => item.id === id)) {
changeNumberOfUnits("plus", id);
} else {
const item = products.find((product) => product.id === id);
cart.push({
...item,
numberOfUnits: 1,
});
}
updateCart();
}
// update cart
function updateCart() {
renderCartItems();
renderSubtotal();
// save cart to local storage
localStorage.setItem("CART", JSON.stringify(cart));
}
// calculate and render subtotal
function renderSubtotal() {
let totalPrice = 0,
totalItems = 0;
cart.forEach((item) => {
totalPrice += item.price * item.numberOfUnits;
totalItems += item.numberOfUnits;
});
subtotalEl.innerHTML = `Subtotal (${totalItems} items): $${totalPrice.toFixed(2)}`;
totalItemsInCartEl.innerHTML = totalItems;
}
// render cart items
function renderCartItems() {
cartItemsEl.innerHTML = ""; // clear cart element
cart.forEach((item) => {
cartItemsEl.innerHTML += `
<div class="cart-item">
<div class="item-info" onclick="removeItemFromCart(${item.id})">
<img src="${item.imgSrc}" alt="${item.name}">
<h4>${item.name}</h4>
</div>
<div class="unit-price">
<small>$</small>${item.price}
</div>
<div class="units">
<div class="btn minus" onclick="changeNumberOfUnits('minus', ${item.id})">-</div>
<div class="number">${item.numberOfUnits}</div>
<div class="btn plus" onclick="changeNumberOfUnits('plus', ${item.id})">+</div>
</div>
</div>
`;
});
}
// remove item from cart
function removeItemFromCart(id) {
cart = cart.filter((item) => item.id !== id);
updateCart();
}
// change number of units for an item
function changeNumberOfUnits(action, id) {
cart = cart.map((item) => {
let numberOfUnits = item.numberOfUnits;
if (item.id === id) {
if (action === "minus" && numberOfUnits > 1) {
numberOfUnits--;
} else if (action === "plus" && numberOfUnits < item.instock) {
numberOfUnits++;
}
}
return {
...item,
numberOfUnits,
};
});
updateCart();
}