мелкие улучшения

This commit is contained in:
2025-11-16 19:42:01 +03:00
parent 61595e31d4
commit 9669459920

View File

@@ -3,14 +3,6 @@
const CATEGORIES = JSON.parse(document.getElementById('categoriesData').textContent); const CATEGORIES = JSON.parse(document.getElementById('categoriesData').textContent);
const ITEMS = JSON.parse(document.getElementById('itemsData').textContent); // Единый массив товаров и комплектов const ITEMS = JSON.parse(document.getElementById('itemsData').textContent); // Единый массив товаров и комплектов
// Отладка: проверить количество загруженных позиций
console.log('Загружено категорий:', CATEGORIES.length);
console.log('Загружено позиций (товары + комплекты):', ITEMS.length);
const productsCount = ITEMS.filter(i => i.type === 'product').length;
const kitsCount = ITEMS.filter(i => i.type === 'kit').length;
console.log(` - Товаров: ${productsCount}, Комплектов: ${kitsCount}`);
console.log('Позиции:', ITEMS);
let currentCategoryId = null; let currentCategoryId = null;
const cart = new Map(); // "type-id" -> {id, name, price, qty, type} const cart = new Map(); // "type-id" -> {id, name, price, qty, type}
@@ -147,9 +139,8 @@ function renderProducts() {
function addToCart(item) { function addToCart(item) {
const cartKey = `${item.type}-${item.id}`; // Уникальный ключ: "product-1" или "kit-1" const cartKey = `${item.type}-${item.id}`; // Уникальный ключ: "product-1" или "kit-1"
const isNew = !cart.has(cartKey);
if (isNew) { if (!cart.has(cartKey)) {
cart.set(cartKey, { id: item.id, name: item.name, price: Number(item.price), qty: 1, type: item.type }); cart.set(cartKey, { id: item.id, name: item.name, price: Number(item.price), qty: 1, type: item.type });
} else { } else {
cart.get(cartKey).qty += 1; cart.get(cartKey).qty += 1;
@@ -160,8 +151,7 @@ function addToCart(item) {
// Автоматический фокус на поле количества // Автоматический фокус на поле количества
setTimeout(() => { setTimeout(() => {
const qtyInputs = document.querySelectorAll('.qty-input'); const qtyInputs = document.querySelectorAll('.qty-input');
const cartItems = Array.from(cart.keys()); const itemIndex = Array.from(cart.keys()).indexOf(cartKey);
const itemIndex = cartItems.indexOf(cartKey);
if (itemIndex !== -1 && qtyInputs[itemIndex]) { if (itemIndex !== -1 && qtyInputs[itemIndex]) {
qtyInputs[itemIndex].focus(); qtyInputs[itemIndex].focus();
@@ -170,14 +160,6 @@ function addToCart(item) {
}, 50); }, 50);
} }
function updateQty(id, delta) {
if (!cart.has(id)) return;
const item = cart.get(id);
item.qty += delta;
if (item.qty <= 0) cart.delete(id);
renderCart();
}
function renderCart() { function renderCart() {
const list = document.getElementById('cartList'); const list = document.getElementById('cartList');
list.innerHTML = ''; list.innerHTML = '';
@@ -212,7 +194,7 @@ function renderCart() {
qtyInput.className = 'qty-input'; qtyInput.className = 'qty-input';
qtyInput.value = item.qty; qtyInput.value = item.qty;
qtyInput.min = 1; qtyInput.min = 1;
qtyInput.onchange = (e) => { qtyInput.oninput = (e) => {
const newQty = parseInt(e.target.value) || 1; const newQty = parseInt(e.target.value) || 1;
if (newQty <= 0) { if (newQty <= 0) {
removeFromCart(cartKey); removeFromCart(cartKey);