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

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +55 -49
static/script.js CHANGED
@@ -15,6 +15,34 @@ function addMessage(role, message) {
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
  }
@@ -47,15 +75,13 @@ function handleResponse(userInput) {
47
  { text: 'Vegetarian', class: 'green' },
48
  { text: 'Non-Vegetarian', class: 'red' }
49
  ];
50
- }else if (lastMessage.includes('non-vegetarian')) {
51
  conversation.push({ role: 'user', message: 'Non-Vegetarian' });
52
  console.log("Food preference selected: Non-Vegetarian");
53
  botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
54
  fetchIngredients('non-vegetarian');
55
  return;
56
-
57
- }
58
- else if (lastMessage.includes('vegetarian')) {
59
  conversation.push({ role: 'user', message: 'Vegetarian' });
60
  console.log("Food preference selected: Vegetarian");
61
  botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
@@ -69,7 +95,7 @@ function handleResponse(userInput) {
69
  return;
70
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
71
  botResponse = 'Here are some ingredients to customize your dish:';
72
- fetchIngredients('both'); // Fetch non-veg ingredients for customization
73
  return;
74
  } else if (lastMessage.includes('no') && selectedMenuItem) {
75
  addToCart(selectedMenuItem);
@@ -111,13 +137,13 @@ function fetchIngredients(foodPreference) {
111
  });
112
  }
113
 
114
- function fetchMenuItems(category) {
115
  fetch('/get_menu_items', {
116
  method: 'POST',
117
  headers: {
118
  'Content-Type': 'application/json',
119
  },
120
- body: JSON.stringify({ category: category })
121
  })
122
  .then(response => response.json())
123
  .then(data => {
@@ -125,7 +151,7 @@ function fetchMenuItems(category) {
125
  addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
126
  } else {
127
  const menuItems = data.menu_items || [];
128
- addMessage('bot', 'Here are some dishes based on your selection:');
129
  displayMenuItems(menuItems);
130
  }
131
  })
@@ -141,6 +167,10 @@ function displayIngredientsList(ingredients) {
141
  return;
142
  }
143
 
 
 
 
 
144
  let ingredientsList = document.querySelector('.ingredients-list');
145
  if (!ingredientsList) {
146
  ingredientsList = document.createElement('div');
@@ -183,6 +213,10 @@ function displayMenuItems(menuItems) {
183
  return;
184
  }
185
 
 
 
 
 
186
  let menuItemsList = document.querySelector('.menu-items-list');
187
  if (!menuItemsList) {
188
  menuItemsList = document.createElement('div');
@@ -228,6 +262,10 @@ function displaySelectedIngredients() {
228
  return;
229
  }
230
 
 
 
 
 
231
  let selectedArea = document.querySelector('.selected-ingredients');
232
  if (!selectedArea) {
233
  selectedArea = document.createElement('div');
@@ -266,10 +304,7 @@ function submitIngredients() {
266
  .then(response => response.json())
267
  .then(data => {
268
  if (data.success) {
269
- // Extract the ingredient names from the selectedIngredients array
270
  const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
271
-
272
- // Fetch menu items based on ingredient names
273
  fetchMenuItems(ingredientNames);
274
  } else {
275
  addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
@@ -280,30 +315,6 @@ function submitIngredients() {
280
  });
281
  }
282
 
283
- function fetchMenuItems(ingredientNames) {
284
- fetch('/get_menu_items', {
285
- method: 'POST',
286
- headers: {
287
- 'Content-Type': 'application/json',
288
- },
289
- body: JSON.stringify({ ingredient_names: ingredientNames })
290
- })
291
- .then(response => response.json())
292
- .then(data => {
293
- if (data.error) {
294
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
295
- } else {
296
- const menuItems = data.menu_items || [];
297
- addMessage('bot', 'Here are some dishes based on your selected ingredients:');
298
- displayMenuItems(menuItems);
299
- }
300
- })
301
- .catch(error => {
302
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
303
- });
304
- }
305
-
306
-
307
  function displayCustomizationInput() {
308
  const chatMessages = document.getElementById('chatMessages');
309
  if (!chatMessages) {
@@ -311,6 +322,10 @@ function displayCustomizationInput() {
311
  return;
312
  }
313
 
 
 
 
 
314
  let customizationArea = document.querySelector('.customization-input');
315
  if (!customizationArea) {
316
  customizationArea = document.createElement('div');
@@ -352,6 +367,11 @@ function displayOptions(options) {
352
  console.error('Chat messages container not found for options!');
353
  return;
354
  }
 
 
 
 
 
355
  options.forEach(opt => {
356
  const button = document.createElement('button');
357
  button.textContent = opt.text;
@@ -359,25 +379,11 @@ function displayOptions(options) {
359
  button.onclick = () => {
360
  addMessage('user', opt.text);
361
  conversation.push({ role: 'user', message: opt.text });
362
- chatMessages.innerHTML = '';
363
- conversation.forEach(msg => addMessage(msg.role, msg.message));
364
  setTimeout(() => handleResponse(opt.text), 500);
365
  };
366
  chatMessages.appendChild(button);
367
  });
368
  chatMessages.appendChild(document.createElement('br'));
369
- const backButton = document.createElement('button');
370
- backButton.textContent = 'Go Back';
371
- backButton.className = 'option-button';
372
- backButton.onclick = () => {
373
- conversation.pop();
374
- selectedIngredients = [];
375
- selectedMenuItem = null;
376
- chatMessages.innerHTML = '';
377
- conversation.forEach(msg => addMessage(msg.role, msg.message));
378
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
379
- };
380
- chatMessages.appendChild(backButton);
381
  }
382
 
383
  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
+
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
  }
 
75
  { text: 'Vegetarian', class: 'green' },
76
  { text: 'Non-Vegetarian', class: 'red' }
77
  ];
78
+ } else if (lastMessage.includes('non-vegetarian')) {
79
  conversation.push({ role: 'user', message: 'Non-Vegetarian' });
80
  console.log("Food preference selected: Non-Vegetarian");
81
  botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
82
  fetchIngredients('non-vegetarian');
83
  return;
84
+ } else if (lastMessage.includes('vegetarian')) {
 
 
85
  conversation.push({ role: 'user', message: 'Vegetarian' });
86
  console.log("Food preference selected: Vegetarian");
87
  botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
 
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
  });
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
  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
  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
  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
  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');
 
304
  .then(response => response.json())
305
  .then(data => {
306
  if (data.success) {
 
307
  const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
 
 
308
  fetchMenuItems(ingredientNames);
309
  } else {
310
  addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
 
315
  });
316
  }
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318
  function displayCustomizationInput() {
319
  const chatMessages = document.getElementById('chatMessages');
320
  if (!chatMessages) {
 
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');
 
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;
 
379
  button.onclick = () => {
380
  addMessage('user', opt.text);
381
  conversation.push({ role: 'user', message: opt.text });
 
 
382
  setTimeout(() => handleResponse(opt.text), 500);
383
  };
384
  chatMessages.appendChild(button);
385
  });
386
  chatMessages.appendChild(document.createElement('br'));
 
 
 
 
 
 
 
 
 
 
 
 
387
  }
388
 
389
  document.getElementById('userInput').addEventListener('keypress', function(e) {