nagasurendra commited on
Commit
32f38e3
·
verified ·
1 Parent(s): 16249c1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -35
app.py CHANGED
@@ -64,6 +64,15 @@ def load_menu_from_salesforce():
64
  except Exception as e:
65
  return []
66
 
 
 
 
 
 
 
 
 
 
67
  # Function to filter menu items
68
  def filter_menu(preference):
69
  menu_data = load_menu_from_salesforce()
@@ -103,7 +112,40 @@ def filter_menu(preference):
103
 
104
  return html_content
105
 
106
- # JavaScript for Modal and Cart
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
107
  def modal_js():
108
  modal_script = """
109
  <script>
@@ -111,23 +153,22 @@ def modal_js():
111
 
112
  function openModal(name, image2, description, price) {
113
  const modal = document.getElementById('modal');
114
- modal.style.display = 'block'; // Make the modal visible
115
  modal.style.position = 'fixed';
116
  modal.style.width = window.innerWidth <= 768 ? '90%' : '30%';
117
- modal.style.top = '50%';
118
  modal.style.left = '50%';
119
  modal.style.transform = 'translate(-50%, -50%)';
120
- document.getElementById('modal-image').src = image2 || ''; // Set image or leave empty
121
  document.getElementById('modal-name').innerText = name;
122
  document.getElementById('modal-description').innerText = description;
123
  document.getElementById('modal-price').innerText = price;
124
- document.getElementById('quantity').value = 1; // Reset quantity
125
- document.getElementById('special-instructions').value = ''; // Reset special instructions
126
  }
127
 
128
  function closeModal() {
129
- const modal = document.getElementById('modal');
130
- modal.style.display = 'none'; // Hide the modal
131
  }
132
 
133
  function addToCart() {
@@ -142,22 +183,16 @@ def modal_js():
142
  const extrasCost = extras.reduce((total, extra) => total + extra.price, 0);
143
  const totalCost = (price + extrasCost) * quantity;
144
  cart.push({ name, price, quantity, extras, instructions, totalCost });
145
- updateCartButton();
146
  closeModal();
147
  }
148
 
149
- function updateCartButton() {
150
- const cartButton = document.getElementById('cart-button');
151
- cartButton.textContent = `View Cart (${cart.length} items)`;
152
- }
153
-
154
- function openCartModal() {
155
- const cartModal = document.getElementById('cart-modal');
156
- const cartItemsContainer = document.getElementById('cart-items');
157
- cartItemsContainer.innerHTML = "";
158
  cart.forEach((item, index) => {
159
- const extrasList = item.extras.map(extra => `${extra.name} (+$${extra.price.toFixed(2)})`).join(', ');
160
- cartItemsContainer.innerHTML += `
161
  <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
162
  <h3>${item.name}</h3>
163
  <p>Quantity: ${item.quantity}</p>
@@ -166,29 +201,19 @@ def modal_js():
166
  <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
167
  <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
168
  </div>
169
- `;
170
  });
171
- cartModal.style.display = 'block'; // Make the cart modal visible
172
- }
173
-
174
- function closeCartModal() {
175
- const cartModal = document.getElementById('cart-modal');
176
- cartModal.style.display = 'none'; // Hide the cart modal
177
  }
178
 
179
  function removeFromCart(index) {
180
  cart.splice(index, 1);
181
- updateCartButton();
182
- openCartModal(); // Refresh the cart modal
183
- }
184
-
185
- function proceedToCheckout() {
186
- alert("Proceeding to checkout...");
187
  }
188
  </script>
189
  """
190
  return modal_script
191
 
 
192
  # Gradio App
193
  with gr.Blocks() as app:
194
  with gr.Row():
@@ -202,12 +227,27 @@ with gr.Blocks() as app:
202
  signup_button = gr.Button("Go to Signup")
203
  login_output = gr.Textbox(label="Status")
204
 
 
 
 
 
 
 
 
 
 
 
205
  with gr.Row(visible=False) as menu_page:
206
  with gr.Column():
207
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
 
 
 
 
208
  menu_output = gr.HTML()
209
- gr.HTML("<div id='cart-modal' style='display: none; position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: white; z-index: 1000; overflow-y: auto;'><div style='padding: 20px;'><div style='text-align: right;'><button onclick='closeCartModal()' style='background: none; border: none; font-size: 24px; cursor: pointer;'>&times;</button></div><h1>Your Cart</h1><div id='cart-items'></div><button style='background: #ff5722; color: white; padding: 10px 20px; border-radius: 5px; border: none; cursor: pointer;' onclick='proceedToCheckout()'>Proceed to Checkout</button></div></div>")
210
- gr.HTML(modal_js())
 
211
 
212
  login_button.click(
213
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")
 
64
  except Exception as e:
65
  return []
66
 
67
+ # Function to load add-ons data
68
+ def load_add_ons_from_salesforce():
69
+ try:
70
+ query = "SELECT Name, Price__c FROM Add_Ons__c"
71
+ result = sf.query(query)
72
+ return result['records']
73
+ except Exception as e:
74
+ return []
75
+
76
  # Function to filter menu items
77
  def filter_menu(preference):
78
  menu_data = load_menu_from_salesforce()
 
112
 
113
  return html_content
114
 
115
+ # Create Modal Window HTML
116
+ def create_modal_window():
117
+ add_ons = load_add_ons_from_salesforce()
118
+ add_ons_html = ""
119
+ for add_on in add_ons:
120
+ add_ons_html += f"""
121
+ <label>
122
+ <input type="checkbox" name="biryani-extra" value="{add_on['Name']}" data-price="{add_on['Price__c']}" />
123
+ {add_on['Name']} + ${add_on['Price__c']}
124
+ </label>
125
+ """
126
+
127
+ modal_html = f"""
128
+ <div id="modal" style="display: none; position: fixed; background: white; border-radius: 8px; box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2); padding: 20px; z-index: 1000;">
129
+ <div style="text-align: right;">
130
+ <button onclick="closeModal()" style="background: none; border: none; font-size: 18px; cursor: pointer;">&times;</button>
131
+ </div>
132
+ <img id="modal-image" style="width: 100%; height: 300px; border-radius: 8px; margin-bottom: 20px;" />
133
+ <h2 id="modal-name"></h2>
134
+ <p id="modal-description"></p>
135
+ <p id="modal-price"></p>
136
+ <label for="biryani-extras"><strong>Add-ons :</strong></label>
137
+ <div id="biryani-extras-options" style="display: flex; flex-wrap: wrap; gap: 10px; margin: 10px 0;">
138
+ {add_ons_html}
139
+ </div>
140
+ <label for="quantity">Quantity:</label>
141
+ <input type="number" id="quantity" value="1" min="1" style="width: 50px;" />
142
+ <textarea id="special-instructions" placeholder="Add your special instructions here..." style="width: 100%; height: 60px;"></textarea>
143
+ <button style="background-color: #28a745; color: white; border: none; padding: 10px 20px; font-size: 14px; border-radius: 5px; cursor: pointer;" onclick="addToCart()">Add to Cart</button>
144
+ </div>
145
+ """
146
+ return modal_html
147
+
148
+ # JavaScript for Modal Functionality
149
  def modal_js():
150
  modal_script = """
151
  <script>
 
153
 
154
  function openModal(name, image2, description, price) {
155
  const modal = document.getElementById('modal');
156
+ modal.style.display = 'block';
157
  modal.style.position = 'fixed';
158
  modal.style.width = window.innerWidth <= 768 ? '90%' : '30%';
159
+ modal.style.top = ${event.clientY}px;
160
  modal.style.left = '50%';
161
  modal.style.transform = 'translate(-50%, -50%)';
162
+ document.getElementById('modal-image').src = image2;
163
  document.getElementById('modal-name').innerText = name;
164
  document.getElementById('modal-description').innerText = description;
165
  document.getElementById('modal-price').innerText = price;
166
+ document.getElementById('quantity').value = 1;
167
+ document.getElementById('special-instructions').value = '';
168
  }
169
 
170
  function closeModal() {
171
+ document.getElementById('modal').style.display = 'none';
 
172
  }
173
 
174
  function addToCart() {
 
183
  const extrasCost = extras.reduce((total, extra) => total + extra.price, 0);
184
  const totalCost = (price + extrasCost) * quantity;
185
  cart.push({ name, price, quantity, extras, instructions, totalCost });
186
+ updateCartUI();
187
  closeModal();
188
  }
189
 
190
+ function updateCartUI() {
191
+ const cartContainer = document.getElementById('cart-container');
192
+ cartContainer.innerHTML = "";
 
 
 
 
 
 
193
  cart.forEach((item, index) => {
194
+ const extrasList = item.extras.map(extra => ${extra.name} (+$${extra.price.toFixed(2)})).join(', ');
195
+ cartContainer.innerHTML +=
196
  <div style="border: 1px solid #ddd; padding: 10px; margin-bottom: 10px; border-radius: 8px;">
197
  <h3>${item.name}</h3>
198
  <p>Quantity: ${item.quantity}</p>
 
201
  <p>Total Cost: $${item.totalCost.toFixed(2)}</p>
202
  <button onclick="removeFromCart(${index})" style="color: red;">Remove</button>
203
  </div>
204
+ ;
205
  });
 
 
 
 
 
 
206
  }
207
 
208
  function removeFromCart(index) {
209
  cart.splice(index, 1);
210
+ updateCartUI();
 
 
 
 
 
211
  }
212
  </script>
213
  """
214
  return modal_script
215
 
216
+
217
  # Gradio App
218
  with gr.Blocks() as app:
219
  with gr.Row():
 
227
  signup_button = gr.Button("Go to Signup")
228
  login_output = gr.Textbox(label="Status")
229
 
230
+ with gr.Row(visible=False) as signup_page:
231
+ with gr.Column():
232
+ signup_name = gr.Textbox(label="Name")
233
+ signup_email = gr.Textbox(label="Email")
234
+ signup_phone = gr.Textbox(label="Phone")
235
+ signup_password = gr.Textbox(label="Password", type="password")
236
+ submit_signup = gr.Button("Signup")
237
+ login_redirect = gr.Button("Go to Login")
238
+ signup_output = gr.Textbox(label="Status")
239
+
240
  with gr.Row(visible=False) as menu_page:
241
  with gr.Column():
242
  preference = gr.Radio(choices=["All", "Veg", "Non-Veg"], label="Filter Preference", value="All")
243
+
244
+ # Duplicate View Cart at the top
245
+ gr.HTML("<div id='cart-container-top' style='margin-top: 10px; padding: 10px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9;'>Your cart is empty.</div>")
246
+
247
  menu_output = gr.HTML()
248
+ gr.HTML("<div id='cart-container' style='margin-top: 20px; padding: 10px; border: 1px solid #ddd; border-radius: 8px; background-color: #f9f9f9;'>Your cart is empty.</div>")
249
+ gr.HTML(create_modal_window()) # Add modal window HTML
250
+ gr.HTML(modal_js()) # Add modal JavaScript
251
 
252
  login_button.click(
253
  lambda email, password: (gr.update(visible=False), gr.update(visible=True), gr.update(value=filter_menu("All")), "Login successful!")