Yaswanth56 commited on
Commit
3cd3487
·
verified ·
1 Parent(s): 4dc506d

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +35 -23
static/script.js CHANGED
@@ -50,15 +50,24 @@ function handleResponse(userInput) {
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
  } else if (lastMessage.includes('vegetarian')) {
57
  conversation.push({ role: 'user', message: 'Vegetarian' });
58
  console.log("Food preference selected: Vegetarian");
59
- botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
60
  fetchIngredients('vegetarian');
61
  return;
 
 
 
 
 
 
 
 
 
62
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
63
  botResponse = 'Here are some ingredients to customize your dish:';
64
  fetchIngredients('non-vegetarian'); // Fetch non-veg ingredients for customization
@@ -146,11 +155,12 @@ function displayIngredientsList(ingredients) {
146
  const item = document.createElement('div');
147
  item.className = 'ingredient-item';
148
  const img = document.createElement('img');
149
- img.src = ingredient.image_url || 'https://via.placeholder.com/80';
150
  img.alt = ingredient.name;
151
  console.log(`Setting image src for ${ingredient.name} to: ${img.src}`); // Debug log
152
  img.style.width = '80px';
153
  img.style.height = '80px';
 
154
  const name = document.createElement('div');
155
  name.textContent = ingredient.name;
156
  name.style.textAlign = 'center';
@@ -169,6 +179,20 @@ function displayIngredientsList(ingredients) {
169
  item.appendChild(button);
170
  ingredientsList.appendChild(item);
171
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
172
  }
173
 
174
  function displayMenuItems(menuItems) {
@@ -191,11 +215,12 @@ function displayMenuItems(menuItems) {
191
  const menuItem = document.createElement('div');
192
  menuItem.className = 'menu-item';
193
  const img = document.createElement('img');
194
- img.src = item.image_url || 'https://via.placeholder.com/80';
195
  img.alt = item.name;
196
  console.log(`Setting image src for ${item.name} to: ${img.src}`); // Debug log
197
  img.style.width = '80px';
198
  img.style.height = '80px';
 
199
  const name = document.createElement('div');
200
  name.textContent = item.name;
201
  name.style.textAlign = 'center';
@@ -250,17 +275,6 @@ function displaySelectedIngredients() {
250
  div.textContent = ingredient.name;
251
  selectedArea.appendChild(div);
252
  });
253
-
254
- if (selectedIngredients.length > 0) {
255
- let submitButton = document.querySelector('.submit-button');
256
- if (!submitButton) {
257
- submitButton = document.createElement('button');
258
- submitButton.className = 'submit-button';
259
- submitButton.textContent = 'Submit Ingredients';
260
- submitButton.onclick = submitIngredients;
261
- chatMessages.appendChild(submitButton);
262
- }
263
- }
264
  }
265
 
266
  function submitIngredients() {
@@ -275,13 +289,11 @@ function submitIngredients() {
275
  .then(data => {
276
  if (data.success) {
277
  // Determine category based on selected ingredients
278
- let category = 'non-vegetarian'; // Default for now
279
- if (selectedIngredients.some(ing => ing.name.toLowerCase() === 'chicken')) {
280
- category = 'chicken';
281
- } else if (selectedIngredients.some(ing => ing.name.toLowerCase() === 'mutton')) {
282
- category = 'mutton';
283
- }
284
- fetchMenuItems(category); // Fetch menu items based on category
285
  } else {
286
  addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
287
  }
@@ -354,7 +366,7 @@ function displayOptions(options) {
354
  });
355
  chatMessages.appendChild(document.createElement('br'));
356
  const backButton = document.createElement('button');
357
- backButton.textText = 'Go Back';
358
  backButton.className = 'option-button';
359
  backButton.onclick = () => {
360
  conversation.pop();
 
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 ingredients:';
54
  fetchIngredients('non-vegetarian');
55
  return;
56
  } else if (lastMessage.includes('vegetarian')) {
57
  conversation.push({ role: 'user', message: 'Vegetarian' });
58
  console.log("Food preference selected: Vegetarian");
59
+ botResponse = 'Great choice! 🍽️ Please select ingredients:';
60
  fetchIngredients('vegetarian');
61
  return;
62
+ } else if (lastMessage.includes('submit ingredients') && selectedIngredients.length > 0) {
63
+ botResponse = 'Ingredients submitted! Here are some dishes based on your selection:';
64
+ let category = 'non-vegetarian';
65
+ if (selectedIngredients.some(ing => ing.name.toLowerCase().includes('vegetarian'))) category = 'vegetarian';
66
+ else if (selectedIngredients.some(ing => ing.name.toLowerCase().includes('chicken'))) category = 'chicken';
67
+ else if (selectedIngredients.some(ing => ing.name.toLowerCase().includes('mutton'))) category = 'mutton';
68
+ fetchMenuItems(category);
69
+ selectedIngredients = [];
70
+ return;
71
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
72
  botResponse = 'Here are some ingredients to customize your dish:';
73
  fetchIngredients('non-vegetarian'); // Fetch non-veg ingredients for customization
 
155
  const item = document.createElement('div');
156
  item.className = 'ingredient-item';
157
  const img = document.createElement('img');
158
+ img.src = ingredient.image_url || 'https://picsum.photos/80';
159
  img.alt = ingredient.name;
160
  console.log(`Setting image src for ${ingredient.name} to: ${img.src}`); // Debug log
161
  img.style.width = '80px';
162
  img.style.height = '80px';
163
+ img.onerror = () => console.error(`Image failed to load for ${ingredient.name}`);
164
  const name = document.createElement('div');
165
  name.textContent = ingredient.name;
166
  name.style.textAlign = 'center';
 
179
  item.appendChild(button);
180
  ingredientsList.appendChild(item);
181
  });
182
+
183
+ // Add submit button if not present
184
+ let submitButton = document.querySelector('.submit-button');
185
+ if (!submitButton && ingredients.length > 0) {
186
+ submitButton = document.createElement('button');
187
+ submitButton.className = 'submit-button';
188
+ submitButton.textContent = 'Submit Ingredients';
189
+ submitButton.onclick = () => {
190
+ addMessage('user', 'Submit Ingredients');
191
+ conversation.push({ role: 'user', message: 'submit ingredients' });
192
+ handleResponse('submit ingredients');
193
+ };
194
+ chatMessages.appendChild(submitButton);
195
+ }
196
  }
197
 
198
  function displayMenuItems(menuItems) {
 
215
  const menuItem = document.createElement('div');
216
  menuItem.className = 'menu-item';
217
  const img = document.createElement('img');
218
+ img.src = item.image_url || 'https://picsum.photos/80';
219
  img.alt = item.name;
220
  console.log(`Setting image src for ${item.name} to: ${img.src}`); // Debug log
221
  img.style.width = '80px';
222
  img.style.height = '80px';
223
+ img.onerror = () => console.error(`Image failed to load for ${item.name}`);
224
  const name = document.createElement('div');
225
  name.textContent = item.name;
226
  name.style.textAlign = 'center';
 
275
  div.textContent = ingredient.name;
276
  selectedArea.appendChild(div);
277
  });
 
 
 
 
 
 
 
 
 
 
 
278
  }
279
 
280
  function submitIngredients() {
 
289
  .then(data => {
290
  if (data.success) {
291
  // Determine category based on selected ingredients
292
+ let category = 'non-vegetarian';
293
+ if (selectedIngredients.some(ing => ing.name.toLowerCase().includes('vegetarian'))) category = 'vegetarian';
294
+ else if (selectedIngredients.some(ing => ing.name.toLowerCase().includes('chicken'))) category = 'chicken';
295
+ else if (selectedIngredients.some(ing => ing.name.toLowerCase().includes('mutton'))) category = 'mutton';
296
+ fetchMenuItems(category);
 
 
297
  } else {
298
  addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
299
  }
 
366
  });
367
  chatMessages.appendChild(document.createElement('br'));
368
  const backButton = document.createElement('button');
369
+ backButton.textContent = 'Go Back';
370
  backButton.className = 'option-button';
371
  backButton.onclick = () => {
372
  conversation.pop();