-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
265 lines (207 loc) · 8.32 KB
/
Copy pathscript.js
File metadata and controls
265 lines (207 loc) · 8.32 KB
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
const productContainer = document.querySelector(".product-list");
const isProductDetailsPage = document.querySelector(".product-detail")
const isCartPage = document.querySelector(".cart");
// noOfItemInCart = (JSON.parse(sessionStorage.getItem("cart")) || []).length;
// document.querySelector(".cart-item-count").textContent = noOfItemInCart;
// document.querySelector(".cart-item-count").style.display = "block"
function displayProducts(){
products.forEach(product => {
const productCard = document.createElement('div');
productCard.classList.add('product-card');
productCard.innerHTML =
`
<div class="img-box">
<img src="${product.colors[0].mainImage}">
</div>
<h2 class="title"> ${product.title}</h2>
<span class="price">${product.price}</span>
`;
productContainer.appendChild(productCard);
const imgBox = productCard.querySelector(".img-box");
imgBox.addEventListener(
'click',
() => {
sessionStorage.setItem("selectedProduct", JSON.stringify(product));
window.location.href = "product-detail.html"
}
)
});
}
const displayProductDetail = () => {
const productData = JSON.parse(sessionStorage.getItem("selectedProduct"));
const titleE1 = document.querySelector(".title");
const priceE1 = document.querySelector(".price");
const descriptionE1 = document.querySelector(".description");
const mainImageContainer = document.querySelector(".main-img");
const thumbnailContainer = document.querySelector(".thumbnail-list");
const colorContainer = document.querySelector(".color-options");
const sizeContainer = document.querySelector(".size-options");
const addToCartBtn = document.querySelector("#add-cart-btn");
let selectedColor = productData.colors[0]
let selectedSize = selectedColor.sizes[0]
function updateProductDisplay(colorData){
if(!colorData.sizes.includes(selectedSize)){
selectedSize = colorData.sizes[0];
}
mainImageContainer.innerHTML = `<img src="${colorData.mainImage}"/>`;
thumbnailContainer.innerHTML = '';
const allThumbnails = [colorData.mainImage].concat(colorData.thumbnails.slice(0, 3));
allThumbnails.forEach(thumb => {
const img = document.createElement("img");
img.src = thumb;
thumbnailContainer.appendChild(img);
img.addEventListener('click', () => {
mainImageContainer.innerHTML = `<img src="${thumb}"/>`;
})
})
colorContainer.innerHTML = '';
productData.colors.forEach(
color => {
const img = document.createElement('img');
img.src = color.mainImage;
if(color.name === colorData.name) img.classList.add('selected');
colorContainer.appendChild(img);
img.addEventListener(
'click', () => {
selectedColor = color;
updateProductDisplay(color);
}
)
}
)
sizeContainer.innerHTML = ''
colorData.sizes.forEach(
size => {
const btn = document.createElement("button");
btn.textContent = size;
if (size === selectedSize) btn.classList.add("selected");
sizeContainer.appendChild(btn);
btn.addEventListener('click', () => {
document.querySelectorAll(".size-options button").forEach (el => el.classList.remove("selected"))
btn.classList.add("selected");
selectedSize = size
})
}
)
}
titleE1.textContent = productData.title;
priceE1.textContent = productData.price;
descriptionE1.textContent = productData.description;
updateProductDisplay(selectedColor);
addToCartBtn.addEventListener('click', () => {
//noOfItemInCart ++;
addToCart(productData, selectedColor, selectedSize);
})
}
if(productContainer){
displayProducts();
}
else if(isProductDetailsPage){
displayProductDetail();
}
else if(isCartPage){
displayCart();
}
function addToCart(product, color, size){
let cart = JSON.parse(sessionStorage.getItem("cart")) || [];
const existingItem = cart.find(item => item.id === product.id && item.color === color.name && item.size === size);
if(existingItem){
existingItem.quantity += 1;
}
else{
cart.push(
{
id: product.id,
title: product.title,
price: product.price,
image: color.mainImage,
color: color.name,
size: size,
quantity: 1
}
)
}
sessionStorage.setItem("cart", JSON.stringify(cart));
updateCartBadge()
}
function displayCart(){
const cart = JSON.parse(sessionStorage.getItem("cart")) || [];
const cartItemsContainer = document.querySelector(".cart-items");
const subTotalE1 = document.querySelector(".subtotal");
const grandTotalE1 = document.querySelector(".grand-total");
cartItemsContainer.innerHTML = "";
if(cart.length === 0){
cartItemsContainer.innerHTML = `<p> Your Cart Is Empty. </p>`;
subTotalE1.textContent = "$0";
grandTotalE1.textContent = "$0";
return;
}
let subtotal = 0;
cart.forEach (
(item, index) => {
const itemTotal = (parseFloat(item.price.replace("$", "")) * item.quantity);
subtotal += itemTotal;
const cartItem = document.createElement("div");
cartItem.classList.add("cart-item");
cartItem.innerHTML =
`
<div class="product">
<img src="${item.image}" alt="">
<div class="item-detail">
<p>${item.title}</p>
<div class="size-color-box">
<span class="size">${item.size}</span>
<span class="color">${item.color}</span>
</div>
</div>
</div>
<span class="price">${item.price}</span>
<div class="quantity"><input type="number" name="" id="" value="${item.quantity}" min="1" max='999' data-index="${index}"></div>
<span class="total-price">$${itemTotal}</span>
<button class="remove" data-index="${index}" ><i class="ri-close-line"></i></button>
`
cartItemsContainer.append(cartItem)
}
)
subTotalE1.textContent = `$ ${Number(subtotal.toFixed(2)).toLocaleString()}`;
grandTotalE1.textContent = `$${Number(subtotal.toFixed(2)).toLocaleString()}`;
removeCartItem();
updateCartQuantity();
}
function removeCartItem() {
document.querySelectorAll(".remove").forEach(button => {
button.addEventListener("click", function () {
let cart = JSON.parse(sessionStorage.getItem("cart")) || [];
const index = this.getAttribute("data-index");
cart.splice(index, 1);
sessionStorage.setItem("cart", JSON.stringify(cart));
displayCart();
updateCartBadge();
})
})
}
function updateCartQuantity() {
document.querySelectorAll(".quantity input").forEach(input => {
input.addEventListener("change", function () {
let cart = JSON.parse(sessionStorage.getItem("cart")) || [];
const index = this.getAttribute("data-index");
cart[index].quantity = parseInt(this.value);
sessionStorage.setItem("cart", JSON.stringify(cart));
displayCart();
updateCartBadge();
})
})
}
function updateCartBadge(){
const cart = JSON.parse(sessionStorage.getItem("cart")) || [];
const cartCount = cart.reduce((total, item) => total + item.quantity, 0 );
const badge = document.querySelector(".cart-item-count");
if(badge){
badge.textContent = cartCount;
badge.style.display = "block"
}
else{
badge.style.display = "none"
}
}
updateCartBadge();