-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
538 lines (459 loc) · 19.3 KB
/
index.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
let wishlist = [];
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('search').addEventListener('input', searchProducts);
document.getElementById('cartButton').addEventListener('click', showCart);
document.querySelector('.logo').addEventListener('click', (event) => {
event.preventDefault();
loadHomePage();
setInterval(() => moveSlide(1), 3000);
});
document.getElementById('wishlistButton').addEventListener('click', showWishlist);
loadHomePage();
});
function loadHomePage() {
const content = document.getElementById('content');
content.innerHTML = `
<section class="trending">
<h1>Check out the season's biggest trends</h1>
<div class="trend-categories"></div>
</section>
<section class="carousel">
<div class="carousel-slide">
<img src="Store Sale Ad 1.png" alt="Ad 1">
<img src="Store Sale Ad2.png" alt="Ad 2">
<img src="Store Sale Ad3.png" alt="Ad 3">
</div>
<button class="carousel-prev" onclick="moveSlide(-1)">❮</button>
<button class="carousel-next" onclick="moveSlide(1)">❯</button>
</section>
<section class="recommended-products">
<h1>Recommended Products</h1>
<div id="recommended-products-container"></div>
</section>
`;
loadTrendingCategories();
loadRecommendedProducts();
showSlides(slideIndex);
}
function loadRecommendedProducts() {
const categories = [
'mens-shirts', 'mens-shoes', 'mens-watches',
'womens-bags', 'womens-dresses', 'womens-jewellery',
'womens-shoes', 'womens-watches', 'tops',
'sunglasses', 'beauty', 'skin-care',
'fragrances', 'laptops', 'smartphones',
'tablets', 'mobile-accessories', 'furniture',
'home-decoration', 'kitchen-accessories', 'sports-accessories',
'motorcycle', 'vehicle', 'groceries'
];
categories.forEach(category => {
loadRecommendedProductsByCategory(category);
});
}
function loadRecommendedProductsByCategory(category) {
fetch(`https://dummyjson.com/products/category/${encodeURIComponent(category)}?limit=3`)
.then(response => response.json())
.then(data => {
displayRecommendedProductsByCategory(category, data.products);
})
.catch(error => console.error(`Error fetching recommended products for category ${category}:`, error));
}
function displayRecommendedProductsByCategory(category, products) {
const container = document.getElementById('recommended-products-container');
const categorySection = document.createElement('div');
categorySection.className = 'category-section';
categorySection.innerHTML = `<h2>${category}</h2>`;
products.forEach(product => {
const div = document.createElement('div');
div.className = 'product';
const img = document.createElement('img');
img.src = product.thumbnail;
img.alt = product.title;
img.addEventListener('click', () => showProductDetails(product));
div.appendChild(img);
const infoDiv = document.createElement('div');
infoDiv.classList.add('product-info');
const title = document.createElement('h2');
title.textContent = product.title;
title.addEventListener('click', () => showProductDetails(product));
infoDiv.appendChild(title);
const price = document.createElement('p');
price.textContent = `${product.price} USD`;
price.addEventListener('click', () => showProductDetails(product));
infoDiv.appendChild(price);
const addButton = document.createElement('button');
addButton.classList.add('add-to-cart-button');
addButton.textContent = 'Add to Cart';
addButton.addEventListener('click', (event) => {
event.stopPropagation();
addToCart(product);
});
infoDiv.appendChild(addButton);
const heartButton = document.createElement('button');
heartButton.classList.add('heart-button');
if (wishlist.some(item => item.id === product.id)) {
heartButton.classList.add('filled');
}
heartButton.addEventListener('click', (event) => {
event.stopPropagation();
toggleWishlist(product, heartButton);
});
infoDiv.appendChild(heartButton);
div.appendChild(infoDiv);
categorySection.appendChild(div);
});
container.appendChild(categorySection);
}
function loadTrendingCategories() {
const trendCategories = [
{ name: "Fashion", image:"fashion.jpg", subCategories: ["mens-shirts", "mens-shoes", "mens-watches", "womens-bags", "womens-dresses", "womens-jewellery", "womens-shoes", "womens-watches"] },
{ name: "Beauty & Care", image: "skin-care.jpg", subCategories: ["beauty", "skin-care", "fragrances"] },
{ name: "Technology & Electronics", image: "technology.jpg", subCategories: ["laptops", "smartphones", "tablets", "mobile-accessories"] },
{ name: "Home & Garden", image: "home.jpg", subCategories: ["furniture", "home-decoration", "kitchen-accessories"] },
{ name: "Leisure & Sports", image: "sports.jpg", subCategories: ["sports-accessories", "motorcycle", "vehicle"] },
{ name: "Grocery & Food", image: "Grocery.jpg", subCategories: ["groceries"] }
];
const trendCategoriesContainer = document.querySelector('.trend-categories');
trendCategories.forEach(category => {
const categoryDiv = document.createElement('div');
categoryDiv.className = 'trend-category';
const img = document.createElement('img');
img.src = category.image;
img.alt = category.name;
img.addEventListener('click', () => loadProductsBySubCategories(category.subCategories));
categoryDiv.appendChild(img);
const name = document.createElement('p');
name.textContent = category.name;
categoryDiv.appendChild(name);
trendCategoriesContainer.appendChild(categoryDiv);
});
}
function loadProductsBySubCategories(subCategories) {
const promises = subCategories.map(subCategory =>
fetch(`https://dummyjson.com/products/category/${encodeURIComponent(subCategory)}`)
.then(response => response.json())
.then(data => data.products)
);
Promise.all(promises)
.then(results => {
const allProducts = results.flat();
displayProducts(allProducts);
})
.catch(error => console.error('Error fetching products by sub-categories:', error));
}
function toggleWishlist(product, heartButton) {
const index = wishlist.findIndex(item => item.id === product.id);
if (index > -1) {
wishlist.splice(index, 1);
heartButton.classList.remove('filled');
} else {
wishlist.push(product);
heartButton.classList.add('filled');
}
}
function showWishlist() {
const content = document.getElementById('content');
content.innerHTML = '<h1>Wishlist</h1>';
if (wishlist.length === 0) {
content.innerHTML += '<p>Your wishlist is empty.</p>';
return;
}
wishlist.forEach(product => {
const div = document.createElement('div');
div.className = 'product';
const img = document.createElement('img');
img.src = product.thumbnail;
img.alt = product.title;
div.appendChild(img);
const infoDiv = document.createElement('div');
infoDiv.classList.add('product-info');
const title = document.createElement('h2');
title.textContent = product.title;
infoDiv.appendChild(title);
const price = document.createElement('p');
price.textContent = `${product.price} USD`;
infoDiv.appendChild(price);
const addButton = document.createElement('button');
addButton.textContent = 'Add to Cart';
addButton.addEventListener('click', (event) => {
event.stopPropagation();
addToCart(product);
});
infoDiv.appendChild(addButton);
const heartButton = document.createElement('button');
heartButton.classList.add('heart-button');
heartButton.classList.add('filled');
heartButton.addEventListener('click', (event) => {
event.stopPropagation();
toggleWishlist(product, heartButton);
});
infoDiv.appendChild(heartButton);
div.appendChild(infoDiv);
content.appendChild(div);
});
}
function displayProducts(products) {
const content = document.getElementById('content');
content.innerHTML = '<h1>Products</h1>';
products.forEach(product => {
const div = document.createElement('div');
div.className = 'product';
const img = document.createElement('img');
img.src = product.thumbnail;
img.alt = product.title;
img.addEventListener('click', () => showProductDetails(product));
div.appendChild(img);
const infoDiv = document.createElement('div');
infoDiv.classList.add('product-info');
const title = document.createElement('h2');
title.textContent = product.title;
title.addEventListener('click', () => showProductDetails(product));
infoDiv.appendChild(title);
const price = document.createElement('p');
price.textContent = `${product.price} USD`;
price.addEventListener('click', () => showProductDetails(product));
infoDiv.appendChild(price);
const addButton = document.createElement('button');
addButton.textContent = 'Add to Cart';
addButton.addEventListener('click', (event) => {
event.stopPropagation();
addToCart(product);
});
infoDiv.appendChild(addButton);
const heartButton = document.createElement('button');
heartButton.classList.add('heart-button');
if (wishlist.some(item => item.id === product.id)) {
heartButton.classList.add('filled');
}
heartButton.addEventListener('click', (event) => {
event.stopPropagation();
toggleWishlist(product, heartButton);
});
infoDiv.appendChild(heartButton);
div.appendChild(infoDiv);
content.appendChild(div);
});
}
function showProductDetails(product) {
const content = document.getElementById('content');
content.innerHTML = `
<div class="product-details">
<h1>${product.title}</h1>
<div class="product-images">
<img src="${product.images[0]}" alt="${product.title}" class="main-image" id="mainImage">
<div class="thumbnail-container">
${product.images.map(img => `<img src="${img}" alt="${product.title}" class="thumbnail" onclick="changeMainImage('${img}')">`).join('')}
</div>
</div>
<p class="description">${product.description}</p>
<p class="price">${product.price} USD</p>
<button onclick="addToCart(${JSON.stringify(product).replace(/"/g, '"')})">Add to Cart</button>
<button class="heart-button" onclick="toggleWishlistFromDetails(${JSON.stringify(product).replace(/"/g, '"')}, this)"></button>
<div class="product-reviews">
<h2>Customer Reviews</h2>
${product.reviews.map(review => `
<div class="review">
<p><strong>${review.reviewerName}</strong> rated it ${review.rating}/5</p>
<p>${review.comment}</p>
</div>
`).join('')}
</div>
<div class="additional-info">
<h2>Additional Information</h2>
<p>Brand: ${product.brand}</p>
<p>Category: ${product.category}</p>
<p>Stock: ${product.stock}</p>
<p>SKU: ${product.sku}</p>
<p>Weight: ${product.weight}kg</p>
<p>Dimensions: ${product.dimensions.width} x ${product.dimensions.height} x ${product.dimensions.depth} cm</p>
<p>Warranty: ${product.warrantyInformation}</p>
<p>Shipping: ${product.shippingInformation}</p>
<p>Return Policy: ${product.returnPolicy}</p>
<p>Availability: ${product.availabilityStatus}</p>
</div>
</div>
`;
const heartButton = content.querySelector('.heart-button');
if (wishlist.some(item => item.id === product.id)) {
heartButton.classList.add('filled');
}
}
function toggleWishlistFromDetails(product, heartButton) {
toggleWishlist(product, heartButton);
}
function toggleCategories() {
const categoryList = document.getElementById('category-list');
if (categoryList.style.display === 'flex' || categoryList.style.display === 'block') {
categoryList.style.display = 'none';
} else {
categoryList.style.display = 'flex';
categoryList.style.flexDirection = 'column';
}
}
function searchProducts(event) {
const query = event.target.value.trim();
if (query === '') {
loadRecommendedProducts();
return;
}
fetch(`https://dummyjson.com/products/search?q=${encodeURIComponent(query)}`)
.then(res => res.json())
.then(data => {
console.log(data);
if (data.products.length === 0) {
const content = document.getElementById('content');
content.innerHTML = '<p>No products found</p>';
} else {
displayProducts(data.products);
}
})
.catch(error => console.error('Error searching products:', error));
}
function loadProductsByCategory(category) {
fetch(`https://dummyjson.com/products/category/${encodeURIComponent(category)}`)
.then(response => response.json())
.then(data => {
displayProducts(data.products);
})
.catch(error => console.error('Error fetching products by category:', error));
}
function loadProductsBySubCategory(subCategory) {
fetch(`https://dummyjson.com/products/category/${encodeURIComponent(subCategory)}`)
.then(response => response.json())
.then(data => {
displayProducts(data.products);
})
.catch(error => console.error(`Error fetching products for sub-category ${subCategory}:`, error));
}
let cart = [];
function addToCart(product) {
const existingProduct = cart.find(item => item.id === product.id);
if (existingProduct) {
existingProduct.quantity += 1;
} else {
cart.push({
id: product.id,
title: product.title,
thumbnail: product.thumbnail,
price: product.price,
quantity: 1
});
}
alert(`${product.title} has been added to your cart.`);
updateCartTotal();
}
function showCart() {
const content = document.getElementById('content');
content.innerHTML = '<h1>Shopping Cart</h1>';
if (cart.length === 0) {
content.innerHTML += '<p>Your cart is empty.</p>';
return;
}
cart.forEach(item => {
const div = document.createElement('div');
div.className = 'cart-item';
div.innerHTML = `
<img src="${item.thumbnail}" alt="${item.title}">
<h2>${item.title}</h2>
<p>${item.price} USD</p>
<p>Quantity: <input type="number" value="${item.quantity}" min="1" data-id="${item.id}" class="quantity-input"></p>
<button onclick="removeFromCart(${item.id})">Remove</button>
`;
content.appendChild(div);
});
const totalDiv = document.createElement('div');
totalDiv.className = 'cart-total';
totalDiv.innerHTML = `<h2>Total: $${calculateCartTotal()} USD</h2>`;
content.appendChild(totalDiv);
content.innerHTML += `<button onclick="checkout()">Checkout</button>`;
document.querySelectorAll('.quantity-input').forEach(input => {
input.addEventListener('change', updateQuantity);
});
}
function updateQuantity(event) {
const input = event.target;
const id = parseInt(input.dataset.id, 10);
const newQuantity = parseInt(input.value, 10);
const product = cart.find(item => item.id === id);
if (product) {
product.quantity = newQuantity;
}
updateCartTotal();
}
function removeFromCart(productId) {
cart = cart.filter(item => item.id !== productId);
showCart();
}
function calculateCartTotal() {
return cart.reduce((total, item) => total + item.price * item.quantity, 0).toFixed(2);
}
function updateCartTotal() {
const totalDiv = document.querySelector('.cart-total h2');
if (totalDiv) {
totalDiv.textContent = `Total: $${calculateCartTotal()} USD`;
}
}
function checkout() {
const content = document.getElementById('content');
content.innerHTML = `
<h1>Checkout</h1>
<form id="checkout-form">
<label for="name">Full Name:</label>
<input type="text" id="name" name="name" required>
<label for="address">Shipping Address:</label>
<input type="text" id="address" name="address" required>
<label for="id-number">ID Number:</label>
<input type="text" id="id-number" name="id-number" required pattern="\\d{9}" title="Please enter a valid 9-digit ID number">
<label for="credit-card">Credit Card Number:</label>
<input type="text" id="credit-card" name="credit-card" required pattern="\\d{16}" title="Please enter a valid 16-digit credit card number">
<label for="expiry-date">Expiry Date (MM/YY):</label>
<input type="text" id="expiry-date" name="expiry-date" required pattern="\\d{2}/\\d{2}" title="Please enter a valid expiry date in MM/YY format">
<label for="cvv">CVV:</label>
<input type="text" id="cvv" name="cvv" required pattern="\\d{3}" title="Please enter a valid 3-digit CVV number">
<button type="submit">Purchase</button>
</form>
`;
document.getElementById('checkout-form').addEventListener('submit', processPurchase);
}
function changeMainImage(src) {
document.getElementById('mainImage').src = src;
}
function processPurchase(event) {
event.preventDefault();
const address = document.getElementById('address').value;
const creditCard = document.getElementById('credit-card').value;
alert('Thank you for your purchase! Your items will be shipped to ' + address);
cart = [];
showCart();
}
let slideIndex = 0;
function showSlides(n) {
let slides = document.querySelectorAll('.carousel-slide img');
if (slides.length === 0) return;
if (n >= slides.length) {
slideIndex = 0;
}
if (n < 0) {
slideIndex = slides.length - 1;
}
slides.forEach((slide, index) => {
slide.style.display = 'none';
});
slides[slideIndex].style.display = 'block';
}
function loadFashionSubCategory(subCategory) {
let subCategories = {
'men': ['mens-shirts', 'mens-shoes', 'mens-watches'],
'women': ['womens-bags', 'womens-dresses', 'womens-jewellery', 'womens-shoes', 'womens-watches', 'tops'],
'shared': ['sunglasses']
};
if (subCategories[subCategory]) {
loadProductsBySubCategories(subCategories[subCategory]);
} else {
console.error('Sub-category not found');
}
}
function moveSlide(n) {
showSlides(slideIndex += n);
}
setInterval(() => moveSlide(1), 3000);