lokesh341 commited on
Commit
6904ddf
·
verified ·
1 Parent(s): 40bbbf4

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +58 -4
static/script.js CHANGED
@@ -73,6 +73,7 @@ function fetchMenuItems(dietaryPreference) {
73
  } else if (data.menu_items.length > 0) {
74
  addMessage('bot', `Here are some ${dietaryPreference} menu items:`);
75
  displayItemsList(data.menu_items, 'menuItemsList');
 
76
  } else {
77
  addMessage('bot', `No ${dietaryPreference} items found. Try searching for something specific!`);
78
  }
@@ -97,6 +98,7 @@ function suggestItems(searchTerm) {
97
  } else if (data.suggestions.length > 0) {
98
  addMessage('bot', `Found these suggestions for "${searchTerm}":`);
99
  displayItemsList(data.suggestions, 'suggestionsList');
 
100
  } else {
101
  addMessage('bot', `No matches for "${searchTerm}". Try "chicken" or "paneer"?`);
102
  }
@@ -117,12 +119,13 @@ function fetchItemDetails(itemName) {
117
  .then(response => response.json())
118
  .then(data => {
119
  if (data.error) {
120
- addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Check the name or try another item!`);
121
  } else {
122
  const details = data.item_details;
123
- selectedItems.push(details);
124
- addMessage('bot', `Added "${itemName}"! Here are the details:`);
125
  displaySelectedItems();
 
126
  console.log(`Details for ${itemName}:`, details);
127
  }
128
  })
@@ -175,7 +178,7 @@ function displaySelectedItems() {
175
  } else {
176
  selectedItems.forEach(item => {
177
  const div = document.createElement('div');
178
- div.innerHTML = `<strong>${item.name}</strong><br>Description: ${item.description}<br>Ingredients: ${item.ingredients}`;
179
  selectedArea.appendChild(div);
180
  });
181
  }
@@ -213,6 +216,57 @@ function displayOptions(options) {
213
  chatMessages.appendChild(optionsDiv);
214
  }
215
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
216
  function resetConversation() {
217
  const userName = conversation.length > 1 ? conversation[1].message : 'Friend';
218
  conversation = [
 
73
  } else if (data.menu_items.length > 0) {
74
  addMessage('bot', `Here are some ${dietaryPreference} menu items:`);
75
  displayItemsList(data.menu_items, 'menuItemsList');
76
+ displaySubmitButton();
77
  } else {
78
  addMessage('bot', `No ${dietaryPreference} items found. Try searching for something specific!`);
79
  }
 
98
  } else if (data.suggestions.length > 0) {
99
  addMessage('bot', `Found these suggestions for "${searchTerm}":`);
100
  displayItemsList(data.suggestions, 'suggestionsList');
101
+ displaySubmitButton();
102
  } else {
103
  addMessage('bot', `No matches for "${searchTerm}". Try "chicken" or "paneer"?`);
104
  }
 
119
  .then(response => response.json())
120
  .then(data => {
121
  if (data.error) {
122
+ addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Check the name!`);
123
  } else {
124
  const details = data.item_details;
125
+ selectedItems.push(details.name); // Store only the name for submission
126
+ addMessage('bot', `Added "${itemName}" to your selection!`);
127
  displaySelectedItems();
128
+ displaySubmitButton();
129
  console.log(`Details for ${itemName}:`, details);
130
  }
131
  })
 
178
  } else {
179
  selectedItems.forEach(item => {
180
  const div = document.createElement('div');
181
+ div.textContent = item; // Display only the name
182
  selectedArea.appendChild(div);
183
  });
184
  }
 
216
  chatMessages.appendChild(optionsDiv);
217
  }
218
 
219
+ function displaySubmitButton() {
220
+ const chatMessages = document.getElementById('chatMessages');
221
+ if (!chatMessages) return;
222
+
223
+ // Remove existing submit button if any
224
+ const existingButton = document.getElementById('submitButton');
225
+ if (existingButton) existingButton.remove();
226
+
227
+ if (selectedItems.length > 0) {
228
+ const submitButton = document.createElement('button');
229
+ submitButton.id = 'submitButton';
230
+ submitButton.textContent = 'Submit';
231
+ submitButton.className = 'option-button green';
232
+ submitButton.style.marginTop = '10px';
233
+ submitButton.onclick = submitIngredients;
234
+ chatMessages.appendChild(submitButton);
235
+ }
236
+ }
237
+
238
+ function submitIngredients() {
239
+ if (selectedItems.length === 0) {
240
+ addMessage('bot', 'No items selected to submit!');
241
+ return;
242
+ }
243
+
244
+ fetch('/submit_ingredients', {
245
+ method: 'POST',
246
+ headers: { 'Content-Type': 'application/json' },
247
+ body: JSON.stringify({ ingredients: selectedItems })
248
+ })
249
+ .then(response => response.json())
250
+ .then(data => {
251
+ if (data.error) {
252
+ addMessage('bot', `Failed to submit: ${data.error}`);
253
+ } else {
254
+ addMessage('bot', 'Ingredients submitted successfully! Ready to start over?');
255
+ selectedItems = [];
256
+ displaySelectedItems();
257
+ displayOptions([
258
+ { text: 'Vegetarian', class: 'green' },
259
+ { text: 'Non-Vegetarian', class: 'red' },
260
+ { text: 'Both', class: 'gray' }
261
+ ]);
262
+ }
263
+ })
264
+ .catch(error => {
265
+ addMessage('bot', `Error submitting ingredients: ${error.message}. Retrying...`);
266
+ setTimeout(submitIngredients, 2000);
267
+ });
268
+ }
269
+
270
  function resetConversation() {
271
  const userName = conversation.length > 1 ? conversation[1].message : 'Friend';
272
  conversation = [