-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
118 lines (107 loc) · 3.29 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
// Select Elements
const productsElement = document.querySelector('.products')
const cartsElement = document.querySelector('.cart-items')
const subtotalElement = document.querySelector('.subtotal')
const totalItemsInCart = document.querySelector('.total-items-in-cart')
const renderProducts = () => {
products.forEach((product) => {
productsElement.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">
<img src="./icons/bag-plus.png" onClick="addToCart(${product.id})" alt="add to cart" />
</div>
</div>
</div>
`
})
}
renderProducts()
// cart array
let cart = []
const addToCart = (id) => {
// Check if the product is 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
})
console.log(cart)
}
updateCart()
}
const updateCart = () => {
renderCartItems()
renderSubtotal()
}
// calculate and render subtotal
const renderSubtotal = () => {
let totalPrice = 0,
totalItems = 0
cart.forEach((item) => {
totalPrice += item.price * item.numberOfUnits
totalItems += item.numberOfUnits
})
subtotalElement.innerHTML = `Subtotal (${totalItems} items): $${totalPrice.toFixed(
2
)}`
totalItemsInCart.textContent = totalItems
}
const renderCartItems = () => {
cartsElement.innerHTML = '' // clear the cart element before adding the new one
cart.forEach((item) => {
cartsElement.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 the cart
const removeItemFromCart = (id) => {
cart = cart.filter((item) => item.id !== id)
updateCart()
}
// Change number of units for an item
const 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()
}