Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions js/cart-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
// =====================
Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,8 @@ function addToCart(id) {
return;
}

cartManager.addItem(item, 1);
const success = cartManager.addItem(item, 1);
if (!success) return;
updateCartCount();
updateFavCount();
renderCart();
Expand Down
27 changes: 24 additions & 3 deletions js/recommendations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}