-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
182 lines (161 loc) · 5.61 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Select elements
const productEl = document.getElementById("products");
const subTotalAmount = document.getElementById("sub-total");
const cartItemsEl = document.getElementById("cart-info");
const quantity = document.getElementById("quantity");
const totalItemsInCart = document.getElementById("total-items-in-cart");
const cartBtn = document.getElementById("cart-btn");
const itemPrice = document.getElementById("item-price");
const cartContainer = document.getElementById("cart-details");
const lipaNaMpesaBtn = document.getElementById("lipa-na-mpesa-btn");
// Render products function
function renderProducts() {
products.forEach((product) => {
productEl.innerHTML += `
<div class="col mb-5">
<div class="h-100">
<a class="underline" href="./product-detail.html?productId=${product.id}">
<img
class="card-img-top"
src="${product.imgThumbnail}"
>
</a>
<hr />
<h4 class="text-black text-start fw-bold">
<a class="underline text-black" href="./product-detail.html?productId=${product.id}">
${product.color} ${product.type}
</a>
</h4>
<h6 class="text-start text-black fw-bold">${product.title} Edition</h6>
<h5 class="my-2 text-start">$${product.price}</h5>
<button class="add-to-cart w-100 text-white" onclick="addToCart(${product.id})">
Add To <i class="fa-solid fa-cart-shopping fa-lg"></i>
</button>
</div>
</div>
`;
});
}
renderProducts();
// Cart array to save all my items in the cart
let cart = JSON.parse(localStorage.getItem("CART")) || [];
updateCart();
// Add to cart function
function addToCart(id) {
if (cart.some((item) => item.id === id)) {
changeNumberOfUnits("plus", id);
} else {
const item = products.find((product) => product.id === id);
cart.push({
...item,
numberOfUnits: 1,
});
// Update the button text to "Added in the Bag" after adding to cart
const addButton = document.querySelector(`[onclick="addToCart(${id})"]`);
addButton.innerText = `In the Bag`;
addButton.disabled = true; // Disable the button after adding to cart
}
updateCart();
}
// update cart
function updateCart() {
renderCartItems();
renderSubtotal();
// save cart to local storage
localStorage.setItem("CART", JSON.stringify(cart));
// Check if the cart is empty and display "No items in cart" if it is
const cartMessage = document.getElementById("cart-message");
const paypalButtonContainer = document.getElementById(
"paypal-button-container"
);
if (cart.length === 0) {
cartMessage.innerHTML = "No items in the bag!";
paypalButtonContainer.classList.add("hidden");
lipaNaMpesaBtn.classList.add("hidden");
} else {
paypalButtonContainer.classList.remove("hidden");
lipaNaMpesaBtn.classList.remove("hidden");
cartMessage.innerHTML = "";
}
}
// calculate and render subtotal
function renderSubtotal() {
let totalPrice = 0;
let totalItems = 0;
cart.forEach((item) => {
totalPrice += item.price * item.numberOfUnits;
totalItems += item.numberOfUnits;
});
subTotalAmount.innerHTML = `$${totalPrice}`;
totalItemsInCart.innerHTML = `${totalItems}`;
cartBtn.innerHTML = `<img src="./images/cart.png" width="30px" height="30px">${totalItems}`;
}
// Render cart items
function renderCartItems() {
let totalPrice = 0;
cartItemsEl.innerHTML = "";
cart.forEach((item) => {
totalPrice = item.price * item.numberOfUnits;
cartItemsEl.innerHTML += `
<tr>
<th scope="row"><img style="height: 70px; width:auto; object-fit: cover" src="${item.imgThumbnail}"></th>
<td>
${item.color} ${item.type}<br/>
<div class="trash-btn" onclick="removeItemFromCart(${item.id})">
<i class="fa-solid fa-trash"></i>
</div>
</td>
<td>
<div class="row text-center">
<button class="cart-quantity-btn" onclick="changeNumberOfUnits('plus', ${item.id})">+</button>
<h5>${item.numberOfUnits}</h5>
<button class="cart-quantity-btn" onclick="changeNumberOfUnits('minus', ${item.id})">-</button>
</div>
</td>
<td>$${totalPrice}</td>
</tr>
`;
});
}
// Remove items from the cart
function removeItemFromCart(id) {
cart = cart.filter((item) => item.id !== id);
updateCart();
}
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();
}
function sortProducts() {
const sortingOptions = document.getElementById("sortingOptions");
const selectedOption = sortingOptions.value;
switch (selectedOption) {
case "popular":
products.sort((a, b) => b.instock - a.instock); // Sort by popularity (descending order of instock)
break;
case "latest":
products.sort((a, b) => b.id - a.id); // Sort by the latest (descending order of id)
break;
case "default":
default:
// If "Default Sorting" or invalid option selected, reset the array to its original order
products.sort((a, b) => a.id - b.id);
break;
}
// After sorting, update the rendered products in the HTML
productEl.innerHTML = ""; // Clear existing product elements
renderProducts();
}