diff --git a/js/cart-manager.js b/js/cart-manager.js index b91949c..46212d9 100644 --- a/js/cart-manager.js +++ b/js/cart-manager.js @@ -6,12 +6,36 @@ const CART_STORAGE_KEY = 'chaatCart'; const CART_SYNC_EVENT = 'cartStateChanged'; class CartManager { - constructor() { + constructor(maxUniqueItems = 50) { + this.maxUniqueItems = maxUniqueItems; this.items = this.loadFromStorage(); this.listeners = []; this.setupStorageSync(); } + getMaxUniqueItems() { + return this.maxUniqueItems; + } + + setMaxUniqueItems(limit) { + if (typeof limit === 'number' && limit > 0) { + this.maxUniqueItems = limit; + return true; + } + return false; + } + + showCartFullMessage() { + const message = "Your cart is full. Please remove some items before adding more."; + if (typeof showToast === 'function') { + showToast(message); + } else if (typeof window.showToast === 'function') { + window.showToast(message); + } else { + alert(message); + } + } + // ===================== // LOAD // ===================== @@ -117,10 +141,14 @@ class CartManager { return false; } - const existingItem = this.getItem(item.id); + const existingItem = this.getItem(item.id); if (existingItem) { existingItem.quantity += quantity; } else { + if (this.getSize() >= this.maxUniqueItems) { + this.showCartFullMessage(); + return false; + } this.items.push({ item: { ...item }, quantity: quantity diff --git a/js/main.js b/js/main.js index 3f7292d..fa4b93f 100644 --- a/js/main.js +++ b/js/main.js @@ -941,7 +941,8 @@ function addToCart(id) { return; } - cartManager.addItem(item, 1); + const success = cartManager.addItem(item, 1); + if (!success) return; updateCartCount(); updateFavCount(); renderCart(); diff --git a/js/recommendations.js b/js/recommendations.js index ad32cfe..ad24508 100644 --- a/js/recommendations.js +++ b/js/recommendations.js @@ -106,17 +106,38 @@ const foodPairings = { // ===== ADD TO CART FUNCTION ===== function addToCart(itemName, itemPrice) { + if (typeof cartManager !== 'undefined') { + const success = cartManager.addItem({ + id: itemName + "-" + itemPrice, + name: itemName, + price: itemPrice + }, 1); + + if (success) { + cart = cartManager.getItems(); + updateCartCount(); + renderRecommendations(); + showToast(`${itemName} added to cart 🛒`); + } + return; + } const existingItem = cart.find( cartItem => cartItem.item.name === itemName ); if (existingItem) { - existingItem.quantity += 1; - } else { - + if (cart.length >= 50) { + const message = "Your cart is full. Please remove some items before adding more."; + if (typeof showToast === 'function') { + showToast(message); + } else { + alert(message); + } + return; + } cart.push({ item: { name: itemName, diff --git a/package.json b/package.json new file mode 100644 index 0000000..3c8002e --- /dev/null +++ b/package.json @@ -0,0 +1,13 @@ +{ + "name": "chaatbazaar", + "version": "1.0.0", + "description": "ChaatBazaar - Authentic Indian Street Food Platform", + "scripts": { + "dev": "vite", + "build": "vite build", + "preview": "vite preview" + }, + "devDependencies": { + "vite": "^5.2.0" + } +}