Yaswanth56 commited on
Commit
fb5c95b
·
verified ·
1 Parent(s): ef69d89

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +47 -54
static/script.js CHANGED
@@ -15,34 +15,6 @@ function addMessage(role, message) {
15
  messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';
16
  messageDiv.textContent = message;
17
  chatMessages.appendChild(messageDiv);
18
-
19
- // Add "Go Back" button after bot messages, except for the initial greeting
20
- if (role === 'bot' && conversation.length > 1) {
21
- const backButton = document.createElement('button');
22
- backButton.textContent = 'Go Back';
23
- backButton.className = 'option-button';
24
- backButton.onclick = () => {
25
- // Remove the last user message and bot response
26
- if (conversation.length > 1) {
27
- conversation.pop(); // Remove bot response
28
- if (conversation.length > 1) {
29
- conversation.pop(); // Remove user input
30
- }
31
- selectedIngredients = []; // Clear selected ingredients
32
- selectedMenuItem = null; // Clear selected menu item
33
- chatMessages.innerHTML = ''; // Clear chat
34
- // Re-render conversation
35
- conversation.forEach(msg => addMessage(msg.role, msg.message));
36
- // Reprocess the last user input, if any
37
- if (conversation.length > 1) {
38
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
39
- }
40
- }
41
- };
42
- chatMessages.appendChild(backButton);
43
- chatMessages.appendChild(document.createElement('br'));
44
- }
45
-
46
  chatMessages.scrollTop = chatMessages.scrollHeight;
47
  console.log(`Added ${role} message: ${message}`);
48
  }
@@ -95,7 +67,7 @@ function handleResponse(userInput) {
95
  return;
96
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
97
  botResponse = 'Here are some ingredients to customize your dish:';
98
- fetchIngredients('both'); // Fetch both veg and non-veg ingredients for customization
99
  return;
100
  } else if (lastMessage.includes('no') && selectedMenuItem) {
101
  addToCart(selectedMenuItem);
@@ -137,13 +109,13 @@ function fetchIngredients(foodPreference) {
137
  });
138
  }
139
 
140
- function fetchMenuItems(ingredientNames) {
141
  fetch('/get_menu_items', {
142
  method: 'POST',
143
  headers: {
144
  'Content-Type': 'application/json',
145
  },
146
- body: JSON.stringify({ ingredient_names: ingredientNames })
147
  })
148
  .then(response => response.json())
149
  .then(data => {
@@ -151,7 +123,7 @@ function fetchMenuItems(ingredientNames) {
151
  addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
152
  } else {
153
  const menuItems = data.menu_items || [];
154
- addMessage('bot', 'Here are some dishes based on your selected ingredients:');
155
  displayMenuItems(menuItems);
156
  }
157
  })
@@ -167,10 +139,6 @@ function displayIngredientsList(ingredients) {
167
  return;
168
  }
169
 
170
- // Clear and re-render conversation
171
- chatMessages.innerHTML = '';
172
- conversation.forEach(msg => addMessage(msg.role, msg.message));
173
-
174
  let ingredientsList = document.querySelector('.ingredients-list');
175
  if (!ingredientsList) {
176
  ingredientsList = document.createElement('div');
@@ -213,10 +181,6 @@ function displayMenuItems(menuItems) {
213
  return;
214
  }
215
 
216
- // Clear and re-render conversation
217
- chatMessages.innerHTML = '';
218
- conversation.forEach(msg => addMessage(msg.role, msg.message));
219
-
220
  let menuItemsList = document.querySelector('.menu-items-list');
221
  if (!menuItemsList) {
222
  menuItemsList = document.createElement('div');
@@ -262,10 +226,6 @@ function displaySelectedIngredients() {
262
  return;
263
  }
264
 
265
- // Clear and re-render conversation
266
- chatMessages.innerHTML = '';
267
- conversation.forEach(msg => addMessage(msg.role, msg.message));
268
-
269
  let selectedArea = document.querySelector('.selected-ingredients');
270
  if (!selectedArea) {
271
  selectedArea = document.createElement('div');
@@ -315,6 +275,29 @@ function submitIngredients() {
315
  });
316
  }
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  function displayCustomizationInput() {
319
  const chatMessages = document.getElementById('chatMessages');
320
  if (!chatMessages) {
@@ -322,11 +305,7 @@ function displayCustomizationInput() {
322
  return;
323
  }
324
 
325
- // Clear and re-render conversation
326
- chatMessages.innerHTML = '';
327
- conversation.forEach(msg => addMessage(msg.role, msg.message));
328
-
329
- let customizationArea = document.querySelector('.customization-input');
330
  if (!customizationArea) {
331
  customizationArea = document.createElement('div');
332
  customizationArea.className = 'customization-input';
@@ -367,11 +346,6 @@ function displayOptions(options) {
367
  console.error('Chat messages container not found for options!');
368
  return;
369
  }
370
-
371
- // Clear and re-render conversation
372
- chatMessages.innerHTML = '';
373
- conversation.forEach(msg => addMessage(msg.role, msg.message));
374
-
375
  options.forEach(opt => {
376
  const button = document.createElement('button');
377
  button.textContent = opt.text;
@@ -384,6 +358,25 @@ function displayOptions(options) {
384
  chatMessages.appendChild(button);
385
  });
386
  chatMessages.appendChild(document.createElement('br'));
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
387
  }
388
 
389
  document.getElementById('userInput').addEventListener('keypress', function(e) {
 
15
  messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';
16
  messageDiv.textContent = message;
17
  chatMessages.appendChild(messageDiv);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  chatMessages.scrollTop = chatMessages.scrollHeight;
19
  console.log(`Added ${role} message: ${message}`);
20
  }
 
67
  return;
68
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
69
  botResponse = 'Here are some ingredients to customize your dish:';
70
+ fetchIngredients('both');
71
  return;
72
  } else if (lastMessage.includes('no') && selectedMenuItem) {
73
  addToCart(selectedMenuItem);
 
109
  });
110
  }
111
 
112
+ function fetchMenuItems(category) {
113
  fetch('/get_menu_items', {
114
  method: 'POST',
115
  headers: {
116
  'Content-Type': 'application/json',
117
  },
118
+ body: JSON.stringify({ category: category })
119
  })
120
  .then(response => response.json())
121
  .then(data => {
 
123
  addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
124
  } else {
125
  const menuItems = data.menu_items || [];
126
+ addMessage('bot', 'Here are some dishes based on your selection:');
127
  displayMenuItems(menuItems);
128
  }
129
  })
 
139
  return;
140
  }
141
 
 
 
 
 
142
  let ingredientsList = document.querySelector('.ingredients-list');
143
  if (!ingredientsList) {
144
  ingredientsList = document.createElement('div');
 
181
  return;
182
  }
183
 
 
 
 
 
184
  let menuItemsList = document.querySelector('.menu-items-list');
185
  if (!menuItemsList) {
186
  menuItemsList = document.createElement('div');
 
226
  return;
227
  }
228
 
 
 
 
 
229
  let selectedArea = document.querySelector('.selected-ingredients');
230
  if (!selectedArea) {
231
  selectedArea = document.createElement('div');
 
275
  });
276
  }
277
 
278
+ function fetchMenuItems(ingredientNames) {
279
+ fetch('/get_menu_items', {
280
+ method: 'POST',
281
+ headers: {
282
+ 'Content-Type': 'application/json',
283
+ },
284
+ body: JSON.stringify({ ingredient_names: ingredientNames })
285
+ })
286
+ .then(response => response.json())
287
+ .then(data => {
288
+ if (data.error) {
289
+ addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
290
+ } else {
291
+ const menuItems = data.menu_items || [];
292
+ addMessage('bot', 'Here are some dishes based on your selected ingredients:');
293
+ displayMenuItems(menuItems);
294
+ }
295
+ })
296
+ .catch(error => {
297
+ addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
298
+ });
299
+ }
300
+
301
  function displayCustomizationInput() {
302
  const chatMessages = document.getElementById('chatMessages');
303
  if (!chatMessages) {
 
305
  return;
306
  }
307
 
308
+ let customizationArea = document.query|:querySelector('.customization-input');
 
 
 
 
309
  if (!customizationArea) {
310
  customizationArea = document.createElement('div');
311
  customizationArea.className = 'customization-input';
 
346
  console.error('Chat messages container not found for options!');
347
  return;
348
  }
 
 
 
 
 
349
  options.forEach(opt => {
350
  const button = document.createElement('button');
351
  button.textContent = opt.text;
 
358
  chatMessages.appendChild(button);
359
  });
360
  chatMessages.appendChild(document.createElement('br'));
361
+ if (conversation.length > 1) { // Ensure back button appears only when there's a previous step
362
+ const backButton = document.createElement('button');
363
+ backButton.textContent = 'Go Back';
364
+ backButton.className = 'option-button';
365
+ backButton.onclick = () => {
366
+ if (conversation.length > 1) {
367
+ conversation.pop(); // Remove the last conversation entry
368
+ selectedIngredients = []; // Clear selected ingredients if going back
369
+ selectedMenuItem = null; // Clear selected menu item
370
+ chatMessages.innerHTML = ''; // Clear current messages
371
+ conversation.forEach(msg => addMessage(msg.role, msg.message)); // Re-render conversation
372
+ // Trigger response based on the now-last message
373
+ if (conversation.length > 0) {
374
+ setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
375
+ }
376
+ }
377
+ };
378
+ chatMessages.appendChild(backButton);
379
+ }
380
  }
381
 
382
  document.getElementById('userInput').addEventListener('keypress', function(e) {