WillemVH commited on
Commit
14bd9ee
·
verified ·
1 Parent(s): 56262e9

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +70 -29
templates/index.html CHANGED
@@ -348,11 +348,11 @@
348
  // Sample product data
349
  const products = [
350
  { id: 1, name: "Wireless Headphones", price: 99.99, image: "https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=400&h=300&fit=crop", category: "Electronics" },
351
- { id: 2, name: "Smart Watch", price: 199.99, image: "https://images.unsplash.com/photo-1523275335684-37898b6baf30?w-400&h=300&fit=crop", category: "Electronics" },
352
- { id: 3, name: "Running Shoes", price: 89.99, image: "https://images.unsplash.com/photo-1542291026-7eec264c27ff?w-400&h=300&fit=crop", category: "Fashion" },
353
- { id: 4, name: "Coffee Maker", price: 49.99, image: "https://images.unsplash.com/photo-1495474472287-4d71bcdd2085?w-400&h=300&fit=crop", category: "Home" },
354
- { id: 5, name: "Backpack", price: 39.99, image: "https://images.unsplash.com/photo-1553062407-98eeb64c6a62?w-400&h=300&fit=crop", category: "Fashion" },
355
- { id: 6, name: "Desk Lamp", price: 29.99, image: "https://images.unsplash.com/photo-1507473885765-e6ed057f782c?w-400&h=300&fit=crop", category: "Home" }
356
  ];
357
 
358
  // Cart state
@@ -362,7 +362,7 @@
362
  // Initialize the page
363
  document.addEventListener('DOMContentLoaded', function() {
364
  loadProducts();
365
- updateCartDisplay();
366
  updateNav();
367
  });
368
 
@@ -383,25 +383,25 @@
383
  if (sectionId === 'products') {
384
  loadProducts();
385
  } else if (sectionId === 'cart') {
386
- updateCartDisplay();
387
  } else if (sectionId === 'checkout') {
388
  resetCheckoutSteps();
389
- updateCheckoutDisplay();
390
  }
391
  }
392
 
393
  function updateNav() {
394
- const sections = document.querySelectorAll('.section');
395
  const navLinks = document.querySelectorAll('.nav-link');
396
-
397
  navLinks.forEach(link => link.classList.remove('active'));
398
 
399
- sections.forEach((section, index) => {
400
- if (section.classList.contains('active-section')) {
401
- const sectionId = section.id;
402
- document.querySelector(`[onclick="showSection('${sectionId}')"]`).classList.add('active');
 
 
403
  }
404
- });
405
  }
406
 
407
  // Product functions
@@ -447,13 +447,19 @@
447
  }
448
 
449
  saveCart();
450
- updateCartDisplay();
451
  showToast(`${product.name} added to cart!`);
 
 
 
 
 
452
  }
453
 
454
  function removeFromCart(productId) {
455
  cart = cart.filter(item => item.id !== productId);
456
  saveCart();
 
457
  updateCartDisplay();
458
  }
459
 
@@ -465,6 +471,7 @@
465
  removeFromCart(productId);
466
  } else {
467
  saveCart();
 
468
  updateCartDisplay();
469
  }
470
  }
@@ -473,7 +480,18 @@
473
  function clearCart() {
474
  cart = [];
475
  saveCart();
476
- updateCartDisplay();
 
 
 
 
 
 
 
 
 
 
 
477
  showToast('Cart cleared!');
478
  }
479
 
@@ -485,20 +503,26 @@
485
  function updateCartBadge() {
486
  const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
487
  document.getElementById('cartBadge').textContent = totalItems;
 
 
 
 
 
 
488
  }
489
 
490
  function updateCartDisplay() {
491
  const cartItemsDiv = document.getElementById('cartItems');
492
  const emptyCartMessage = document.getElementById('emptyCartMessage');
493
- const checkoutBtn = document.getElementById('checkoutBtn');
 
 
494
 
495
  if (cart.length === 0) {
496
  cartItemsDiv.innerHTML = '<p class="text-muted">Your cart is empty. <a href="#" onclick="showSection(\'products\')">Browse products</a></p>';
497
  emptyCartMessage.style.display = 'block';
498
- checkoutBtn.disabled = true;
499
  } else {
500
  emptyCartMessage.style.display = 'none';
501
- checkoutBtn.disabled = false;
502
 
503
  let cartHTML = '';
504
  let subtotal = 0;
@@ -541,12 +565,15 @@
541
  const tax = subtotal * 0.08; // 8% tax
542
  const total = subtotal + shipping + tax;
543
 
544
- document.getElementById('subtotal').textContent = subtotal.toFixed(2);
545
- document.getElementById('tax').textContent = tax.toFixed(2);
546
- document.getElementById('total').textContent = total.toFixed(2);
 
 
 
 
 
547
  }
548
-
549
- updateCartBadge();
550
  }
551
 
552
  // Checkout functions
@@ -593,6 +620,8 @@
593
 
594
  function updateCheckoutDisplay() {
595
  const checkoutCartItems = document.getElementById('checkoutCartItems');
 
 
596
  let itemsHTML = '';
597
  let subtotal = 0;
598
 
@@ -610,12 +639,13 @@
610
  `;
611
  });
612
 
613
- checkoutCartItems.innerHTML = itemsHTML;
614
 
615
  const shipping = 5.00;
616
  const tax = subtotal * 0.08;
617
  const total = subtotal + shipping + tax;
618
 
 
619
  document.getElementById('checkoutSubtotal').textContent = subtotal.toFixed(2);
620
  document.getElementById('checkoutTax').textContent = tax.toFixed(2);
621
  document.getElementById('checkoutTotal').textContent = total.toFixed(2);
@@ -624,6 +654,8 @@
624
 
625
  function updateReviewSection() {
626
  const reviewOrderItems = document.getElementById('reviewOrderItems');
 
 
627
  let itemsHTML = '<h5>Items:</h5>';
628
 
629
  cart.forEach(item => {
@@ -640,23 +672,32 @@
640
  reviewOrderItems.innerHTML = itemsHTML;
641
 
642
  // Get shipping address from form (simplified)
643
- const address = document.querySelector('#shippingForm input[type="text"]').value;
 
644
  document.getElementById('reviewAddress').textContent = address || "Not provided";
645
  }
646
 
647
  function placeOrder() {
 
 
 
 
 
648
  // Generate random order number
649
  const orderNumber = Math.floor(1000 + Math.random() * 9000);
650
  document.getElementById('orderNumber').textContent = orderNumber;
651
 
652
  // Clear cart
653
- clearCart();
 
 
654
 
655
  // Show confirmation
656
  nextStep(4);
657
 
658
  // In a real app, you would send order to server here
659
- console.log('Order placed:', { orderNumber, cart: JSON.parse(localStorage.getItem('cart') || '[]') });
 
660
  }
661
 
662
  // Utility function
 
348
  // Sample product data
349
  const products = [
350
  { id: 1, name: "Wireless Headphones", price: 99.99, image: "https://images.unsplash.com/photo-1505740420928-5e560c06d30e?w=400&h=300&fit=crop", category: "Electronics" },
351
+ { id: 2, name: "Smart Watch", price: 199.99, image: "https://images.unsplash.com/photo-1523275335684-37898b6baf30?w=400&h=300&fit=crop", category: "Electronics" },
352
+ { id: 3, name: "Running Shoes", price: 89.99, image: "https://images.unsplash.com/photo-1542291026-7eec264c27ff?w=400&h=300&fit=crop", category: "Fashion" },
353
+ { id: 4, name: "Coffee Maker", price: 49.99, image: "https://images.unsplash.com/photo-1495474472287-4d71bcdd2085?w=400&h=300&fit=crop", category: "Home" },
354
+ { id: 5, name: "Backpack", price: 39.99, image: "https://images.unsplash.com/photo-1553062407-98eeb64c6a62?w=400&h=300&fit=crop", category: "Fashion" },
355
+ { id: 6, name: "Desk Lamp", price: 29.99, image: "https://images.unsplash.com/photo-1507473885765-e6ed057f782c?w=400&h=300&fit=crop", category: "Home" }
356
  ];
357
 
358
  // Cart state
 
362
  // Initialize the page
363
  document.addEventListener('DOMContentLoaded', function() {
364
  loadProducts();
365
+ updateCartBadge();
366
  updateNav();
367
  });
368
 
 
383
  if (sectionId === 'products') {
384
  loadProducts();
385
  } else if (sectionId === 'cart') {
386
+ updateCartDisplay(); // FIXED: Only update cart display when on cart section
387
  } else if (sectionId === 'checkout') {
388
  resetCheckoutSteps();
389
+ updateCheckoutDisplay(); // FIXED: Use checkout-specific update function
390
  }
391
  }
392
 
393
  function updateNav() {
 
394
  const navLinks = document.querySelectorAll('.nav-link');
 
395
  navLinks.forEach(link => link.classList.remove('active'));
396
 
397
+ const activeSection = document.querySelector('.section.active-section');
398
+ if (activeSection) {
399
+ const sectionId = activeSection.id;
400
+ const activeLink = document.querySelector(`.nav-link[onclick="showSection('${sectionId}')"]`);
401
+ if (activeLink) {
402
+ activeLink.classList.add('active');
403
  }
404
+ }
405
  }
406
 
407
  // Product functions
 
447
  }
448
 
449
  saveCart();
450
+ updateCartBadge();
451
  showToast(`${product.name} added to cart!`);
452
+
453
+ // Update cart display only if we're on the cart page
454
+ if (document.getElementById('cart').classList.contains('active-section')) {
455
+ updateCartDisplay();
456
+ }
457
  }
458
 
459
  function removeFromCart(productId) {
460
  cart = cart.filter(item => item.id !== productId);
461
  saveCart();
462
+ updateCartBadge();
463
  updateCartDisplay();
464
  }
465
 
 
471
  removeFromCart(productId);
472
  } else {
473
  saveCart();
474
+ updateCartBadge();
475
  updateCartDisplay();
476
  }
477
  }
 
480
  function clearCart() {
481
  cart = [];
482
  saveCart();
483
+ updateCartBadge();
484
+
485
+ // Update cart display if on cart page
486
+ if (document.getElementById('cart').classList.contains('active-section')) {
487
+ updateCartDisplay();
488
+ }
489
+
490
+ // Update checkout display if on checkout page
491
+ if (document.getElementById('checkout').classList.contains('active-section')) {
492
+ updateCheckoutDisplay();
493
+ }
494
+
495
  showToast('Cart cleared!');
496
  }
497
 
 
503
  function updateCartBadge() {
504
  const totalItems = cart.reduce((sum, item) => sum + item.quantity, 0);
505
  document.getElementById('cartBadge').textContent = totalItems;
506
+
507
+ // Update checkout button state
508
+ const checkoutBtn = document.getElementById('checkoutBtn');
509
+ if (checkoutBtn) {
510
+ checkoutBtn.disabled = totalItems === 0;
511
+ }
512
  }
513
 
514
  function updateCartDisplay() {
515
  const cartItemsDiv = document.getElementById('cartItems');
516
  const emptyCartMessage = document.getElementById('emptyCartMessage');
517
+
518
+ // FIXED: Check if elements exist before using them
519
+ if (!cartItemsDiv || !emptyCartMessage) return;
520
 
521
  if (cart.length === 0) {
522
  cartItemsDiv.innerHTML = '<p class="text-muted">Your cart is empty. <a href="#" onclick="showSection(\'products\')">Browse products</a></p>';
523
  emptyCartMessage.style.display = 'block';
 
524
  } else {
525
  emptyCartMessage.style.display = 'none';
 
526
 
527
  let cartHTML = '';
528
  let subtotal = 0;
 
565
  const tax = subtotal * 0.08; // 8% tax
566
  const total = subtotal + shipping + tax;
567
 
568
+ // FIXED: Check if elements exist before updating
569
+ const subtotalEl = document.getElementById('subtotal');
570
+ const taxEl = document.getElementById('tax');
571
+ const totalEl = document.getElementById('total');
572
+
573
+ if (subtotalEl) subtotalEl.textContent = subtotal.toFixed(2);
574
+ if (taxEl) taxEl.textContent = tax.toFixed(2);
575
+ if (totalEl) totalEl.textContent = total.toFixed(2);
576
  }
 
 
577
  }
578
 
579
  // Checkout functions
 
620
 
621
  function updateCheckoutDisplay() {
622
  const checkoutCartItems = document.getElementById('checkoutCartItems');
623
+ if (!checkoutCartItems) return;
624
+
625
  let itemsHTML = '';
626
  let subtotal = 0;
627
 
 
639
  `;
640
  });
641
 
642
+ checkoutCartItems.innerHTML = itemsHTML || '<p class="text-muted">No items in cart</p>';
643
 
644
  const shipping = 5.00;
645
  const tax = subtotal * 0.08;
646
  const total = subtotal + shipping + tax;
647
 
648
+ // FIXED: Update checkout summary
649
  document.getElementById('checkoutSubtotal').textContent = subtotal.toFixed(2);
650
  document.getElementById('checkoutTax').textContent = tax.toFixed(2);
651
  document.getElementById('checkoutTotal').textContent = total.toFixed(2);
 
654
 
655
  function updateReviewSection() {
656
  const reviewOrderItems = document.getElementById('reviewOrderItems');
657
+ if (!reviewOrderItems) return;
658
+
659
  let itemsHTML = '<h5>Items:</h5>';
660
 
661
  cart.forEach(item => {
 
672
  reviewOrderItems.innerHTML = itemsHTML;
673
 
674
  // Get shipping address from form (simplified)
675
+ const addressInput = document.querySelector('#shippingForm input[type="text"]');
676
+ const address = addressInput ? addressInput.value : '';
677
  document.getElementById('reviewAddress').textContent = address || "Not provided";
678
  }
679
 
680
  function placeOrder() {
681
+ if (cart.length === 0) {
682
+ showToast('Your cart is empty!');
683
+ return;
684
+ }
685
+
686
  // Generate random order number
687
  const orderNumber = Math.floor(1000 + Math.random() * 9000);
688
  document.getElementById('orderNumber').textContent = orderNumber;
689
 
690
  // Clear cart
691
+ cart = [];
692
+ saveCart();
693
+ updateCartBadge();
694
 
695
  // Show confirmation
696
  nextStep(4);
697
 
698
  // In a real app, you would send order to server here
699
+ console.log('Order placed:', orderNumber);
700
+ showToast('Order placed successfully!');
701
  }
702
 
703
  // Utility function