DSatishchandra commited on
Commit
8d3c309
·
verified ·
1 Parent(s): eeaf733

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -30
app.py CHANGED
@@ -77,39 +77,67 @@ modal_and_cart_js = """
77
  function closeModal() {
78
  document.getElementById('modal').style.display = 'none';
79
  }
80
- function addToCart() {
81
- if (finalized) {
82
- alert("You cannot add more items after finalizing your order.");
83
- return;
84
- }
85
- const name = document.getElementById('modal-name').innerText;
86
- const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
87
- const quantity = parseInt(document.getElementById('quantity').value) || 1;
88
- const instructions = document.getElementById('special-instructions').value;
89
- const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
90
- const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
91
- const itemTotal = (price + extrasCost) * quantity;
92
- const cartItem = { name, price, quantity, instructions, extras, itemTotal };
93
- cart.push(cartItem);
94
- alert(`${name} added to cart!`);
95
- updateCartDisplay();
96
- closeModal();
97
  }
98
- function updateCartDisplay() {
99
- let totalBill = 0;
100
- let cartHTML = "<h3>Your Cart:</h3><ul>";
101
- cart.forEach((item, index) => {
102
- totalBill += item.itemTotal;
103
- const extras = item.extras.join(', ');
104
- cartHTML += `<li><input type='checkbox' id='cart-item-${index}' value='${index}' checked onclick='removeItem(${index})'> ${item.name} (x${item.quantity}, Extras: ${extras}, Instructions: ${item.instructions}) - $${item.itemTotal.toFixed(2)}</li>`;
105
- });
106
- cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
107
- document.getElementById('floating-cart').innerHTML = cartHTML;
 
108
  }
109
- function removeItem(index) {
110
- cart.splice(index, 1);
111
- updateCartDisplay();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
112
  }
 
 
 
 
 
 
 
 
113
  function finalizeOrder() {
114
  if (cart.length === 0) {
115
  alert("Your cart is empty. Please add items before finalizing.");
 
77
  function closeModal() {
78
  document.getElementById('modal').style.display = 'none';
79
  }
80
+ function addToCart() {
81
+ if (finalized) {
82
+ alert("You cannot add more items after finalizing your order.");
83
+ return;
 
 
 
 
 
 
 
 
 
 
 
 
 
84
  }
85
+ const name = document.getElementById('modal-name').innerText;
86
+ const price = parseFloat(document.getElementById('modal-price').innerText.replace('$', ''));
87
+ const quantity = parseInt(document.getElementById('quantity').value) || 1;
88
+ const instructions = document.getElementById('special-instructions').value.trim();
89
+ const extras = Array.from(document.querySelectorAll('input[name="biryani-extra"]:checked')).map(extra => extra.value);
90
+ const extrasCost = extras.reduce((sum, extra) => sum + (extrasPrices[extra] || 0), 0);
91
+ const itemTotal = (price + extrasCost) * quantity;
92
+
93
+ if (quantity <= 0) {
94
+ alert("Quantity must be at least 1.");
95
+ return;
96
  }
97
+
98
+ // Add item to the cart
99
+ const cartItem = { name, price, quantity, instructions, extras, itemTotal };
100
+ cart.push(cartItem);
101
+
102
+ // Confirm addition
103
+ alert(`${name} added to cart!`);
104
+
105
+ // Update cart display
106
+ updateCartDisplay();
107
+ closeModal();
108
+ }
109
+
110
+ function updateCartDisplay() {
111
+ let totalBill = 0;
112
+ let cartHTML = "<h3>Your Cart:</h3><ul>";
113
+
114
+ // Render cart items
115
+ cart.forEach((item, index) => {
116
+ totalBill += item.itemTotal;
117
+ const extras = item.extras.length > 0 ? ` (Extras: ${item.extras.join(', ')})` : '';
118
+ cartHTML += `
119
+ <li>
120
+ <input type="checkbox" id="cart-item-${index}" value="${index}" checked onclick="removeItem(${index})">
121
+ ${item.name} (x${item.quantity})${extras} - $${item.itemTotal.toFixed(2)}
122
+ <button onclick="removeItem(${index})" style="margin-left: 10px; background: none; border: none; color: red; cursor: pointer;">Remove</button>
123
+ </li>`;
124
+ });
125
+
126
+ cartHTML += `</ul><p><strong>Total Bill: $${totalBill.toFixed(2)}</strong></p>`;
127
+
128
+ // Update cart display
129
+ const cartElement = document.getElementById('floating-cart');
130
+ if (cartElement) {
131
+ cartElement.innerHTML = cartHTML;
132
  }
133
+ }
134
+
135
+ function removeItem(index) {
136
+ // Remove item from cart
137
+ cart.splice(index, 1);
138
+ updateCartDisplay();
139
+ }
140
+
141
  function finalizeOrder() {
142
  if (cart.length === 0) {
143
  alert("Your cart is empty. Please add items before finalizing.");