flpolprojects commited on
Commit
9eff9e1
·
verified ·
1 Parent(s): e0103ae

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -6
app.py CHANGED
@@ -418,11 +418,13 @@ def catalog():
418
  let selectedProductIndex = null;
419
 
420
  function openModal(index) {
 
421
  loadProductDetails(index);
422
  document.getElementById('productModal').style.display = "block";
423
  }
424
 
425
  function closeModal(modalId) {
 
426
  document.getElementById(modalId).style.display = "none";
427
  }
428
 
@@ -432,7 +434,8 @@ def catalog():
432
  .then(data => {
433
  document.getElementById('modalContent').innerHTML = data;
434
  initializeSwiper();
435
- });
 
436
  }
437
 
438
  function initializeSwiper() {
@@ -453,41 +456,56 @@ def catalog():
453
  }
454
 
455
  function openQuantityModal(index) {
 
456
  selectedProductIndex = index;
457
  document.getElementById('quantityModal').style.display = 'block';
458
  document.getElementById('quantityInput').value = 1;
459
  }
460
 
461
  function confirmAddToCart() {
462
- if (selectedProductIndex === null) {
 
463
  console.error("Товар не выбран!");
 
464
  return;
465
  }
466
 
467
- const quantityInput = document.getElementById('quantityInput').value;
468
- const quantity = parseInt(quantityInput) || 0;
469
 
470
  if (quantity <= 0) {
 
471
  alert("Пожалуйста, укажите количество больше 0");
472
  return;
473
  }
474
 
475
  let cart = JSON.parse(localStorage.getItem('cart') || '[]');
476
  const product = products[selectedProductIndex];
 
 
 
 
 
 
 
477
  const existingItem = cart.find(item => item.name === product.name);
478
 
479
  if (existingItem) {
480
  existingItem.quantity += quantity;
 
481
  } else {
482
- cart.push({
483
  name: product.name,
484
  price: product.price,
485
  photo: product.photos && product.photos.length > 0 ? product.photos[0] : '',
486
  quantity: quantity
487
- });
 
 
488
  }
489
 
490
  localStorage.setItem('cart', JSON.stringify(cart));
 
491
  closeModal('quantityModal');
492
  updateCartButton();
493
  }
@@ -496,9 +514,11 @@ def catalog():
496
  const cart = JSON.parse(localStorage.getItem('cart') || '[]');
497
  const cartButton = document.getElementById('cart-button');
498
  cartButton.style.display = cart.length > 0 ? 'block' : 'none';
 
499
  }
500
 
501
  function openCartModal() {
 
502
  const cart = JSON.parse(localStorage.getItem('cart') || '[]');
503
  const cartContent = document.getElementById('cartContent');
504
  let total = 0;
@@ -532,6 +552,7 @@ def catalog():
532
  }
533
 
534
  function clearCart() {
 
535
  localStorage.removeItem('cart');
536
  closeModal('cartModal');
537
  updateCartButton();
@@ -539,10 +560,13 @@ def catalog():
539
 
540
  window.onclick = function(event) {
541
  if (event.target.className === 'modal') {
 
542
  event.target.style.display = "none";
543
  }
544
  }
545
 
 
 
546
  updateCartButton();
547
  </script>
548
  </body>
 
418
  let selectedProductIndex = null;
419
 
420
  function openModal(index) {
421
+ console.log("Открытие модального окна для товара:", index);
422
  loadProductDetails(index);
423
  document.getElementById('productModal').style.display = "block";
424
  }
425
 
426
  function closeModal(modalId) {
427
+ console.log("Закрытие модального окна:", modalId);
428
  document.getElementById(modalId).style.display = "none";
429
  }
430
 
 
434
  .then(data => {
435
  document.getElementById('modalContent').innerHTML = data;
436
  initializeSwiper();
437
+ })
438
+ .catch(error => console.error('Ошибка загрузки деталей:', error));
439
  }
440
 
441
  function initializeSwiper() {
 
456
  }
457
 
458
  function openQuantityModal(index) {
459
+ console.log("Открытие окна количества для товара:", index);
460
  selectedProductIndex = index;
461
  document.getElementById('quantityModal').style.display = 'block';
462
  document.getElementById('quantityInput').value = 1;
463
  }
464
 
465
  function confirmAddToCart() {
466
+ console.log("Подтверждение добавления в корзину, индекс:", selectedProductIndex);
467
+ if (selectedProductIndex === null || selectedProductIndex === undefined) {
468
  console.error("Товар не выбран!");
469
+ alert("Ошибка: Товар не выбран");
470
  return;
471
  }
472
 
473
+ const quantityInput = document.getElementById('quantityInput');
474
+ const quantity = parseInt(quantityInput.value) || 1; // По умолчанию 1, если ввод некорректен
475
 
476
  if (quantity <= 0) {
477
+ console.warn("Некорректное количество:", quantity);
478
  alert("Пожалуйста, укажите количество больше 0");
479
  return;
480
  }
481
 
482
  let cart = JSON.parse(localStorage.getItem('cart') || '[]');
483
  const product = products[selectedProductIndex];
484
+
485
+ if (!product) {
486
+ console.error("Продукт не найден по индексу:", selectedProductIndex);
487
+ alert("Ошибка: Товар не найден");
488
+ return;
489
+ }
490
+
491
  const existingItem = cart.find(item => item.name === product.name);
492
 
493
  if (existingItem) {
494
  existingItem.quantity += quantity;
495
+ console.log("Увеличено количество существующего товара:", product.name, "на", quantity);
496
  } else {
497
+ const newItem = {
498
  name: product.name,
499
  price: product.price,
500
  photo: product.photos && product.photos.length > 0 ? product.photos[0] : '',
501
  quantity: quantity
502
+ };
503
+ cart.push(newItem);
504
+ console.log("Добавлен новый товар в корзину:", newItem);
505
  }
506
 
507
  localStorage.setItem('cart', JSON.stringify(cart));
508
+ console.log("Корзина обновлена:", cart);
509
  closeModal('quantityModal');
510
  updateCartButton();
511
  }
 
514
  const cart = JSON.parse(localStorage.getItem('cart') || '[]');
515
  const cartButton = document.getElementById('cart-button');
516
  cartButton.style.display = cart.length > 0 ? 'block' : 'none';
517
+ console.log("Обновление кнопки корзины, элементов в корзине:", cart.length);
518
  }
519
 
520
  function openCartModal() {
521
+ console.log("Открытие корзины");
522
  const cart = JSON.parse(localStorage.getItem('cart') || '[]');
523
  const cartContent = document.getElementById('cartContent');
524
  let total = 0;
 
552
  }
553
 
554
  function clearCart() {
555
+ console.log("Очистка корзины");
556
  localStorage.removeItem('cart');
557
  closeModal('cartModal');
558
  updateCartButton();
 
560
 
561
  window.onclick = function(event) {
562
  if (event.target.className === 'modal') {
563
+ console.log("Закрытие модального окна по клику вне области");
564
  event.target.style.display = "none";
565
  }
566
  }
567
 
568
+ // Инициализация
569
+ console.log("Инициализация страницы, товары:", products);
570
  updateCartButton();
571
  </script>
572
  </body>