nagasurendra commited on
Commit
861bf89
·
verified ·
1 Parent(s): 82750f3

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +102 -223
static/script.js CHANGED
@@ -35,6 +35,7 @@ function sendMessage() {
35
  console.warn('Empty message!');
36
  }
37
  }
 
38
  function handleResponse(userInput) {
39
  const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
40
  let botResponse = '';
@@ -48,17 +49,25 @@ function handleResponse(userInput) {
48
  ];
49
  } else if (lastMessage.includes('non-vegetarian')) {
50
  conversation.push({ role: 'user', message: 'Non-Vegetarian' });
 
51
  botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
52
  fetchIngredients('non-vegetarian');
53
  return;
54
  } else if (lastMessage.includes('vegetarian')) {
55
  conversation.push({ role: 'user', message: 'Vegetarian' });
 
56
  botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
57
  fetchIngredients('vegetarian');
58
  return;
 
 
 
 
 
 
59
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
60
- // New step after "Yes" clicked: Fetch ingredients for 'both'
61
- fetchNewStepIngredients(); // This is where ingredients are fetched
62
  return;
63
  } else if (lastMessage.includes('no') && selectedMenuItem) {
64
  addToCart(selectedMenuItem);
@@ -75,160 +84,40 @@ function handleResponse(userInput) {
75
  displayOptions(options);
76
  }
77
  }
78
-
79
- function fetchNewStepIngredients() {
80
  fetch('/get_ingredients', {
81
  method: 'POST',
82
  headers: {
83
  'Content-Type': 'application/json',
84
  },
85
- body: JSON.stringify({ dietary_preference: 'both' })
86
  })
87
  .then(response => response.json())
88
  .then(data => {
89
- // Log the whole response to inspect the structure
90
- console.log('API Response:', data);
91
-
92
- // Check if ingredients is present in the response
93
- if (data && data.ingredients && Array.isArray(data.ingredients)) {
94
- const ingredients = data.ingredients;
95
- if (ingredients.length === 0) {
96
- addMessage('bot', 'No ingredients found. Please try again later.');
97
- } else {
98
- addMessage('bot', 'Please select ingredients to add to your dish:');
99
- // Display the ingredients without the submit button
100
- displayMenuItemIngredientsWithoutSubmit(selectedMenuItem); // Show ingredients for the selected menu item
101
- displayCustomizationInput(); // Show the customization input below the ingredients
102
- }
103
  } else {
104
- addMessage('bot', 'Error: Ingredients not found in the response.');
 
 
 
105
  }
106
  })
107
  .catch(error => {
108
  addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
109
  });
110
  }
111
-
112
-
113
- function displayIngredientsWithoutSubmit(ingredients) {
114
- const chatMessages = document.getElementById('chatMessages');
115
- if (!chatMessages) {
116
- console.error('Chat messages container not found for ingredients!');
117
- return;
118
- }
119
-
120
- let ingredientsList = document.querySelector('.ingredients-list-without-submit');
121
- if (!ingredientsList) {
122
- ingredientsList = document.createElement('div');
123
- ingredientsList.className = 'ingredients-list-without-submit';
124
- ingredientsList.style.overflowX = 'auto'; // Enable horizontal scrolling
125
- ingredientsList.style.whiteSpace = 'nowrap'; // Prevent line breaks to make it horizontal scroll
126
- chatMessages.appendChild(ingredientsList);
127
- } else {
128
- ingredientsList.innerHTML = ''; // Clear previous content
129
- }
130
-
131
- // Loop through each ingredient and display it with the "Add" button
132
- ingredients.forEach(ingredient => {
133
- const item = document.createElement('div');
134
- item.className = 'ingredient-item';
135
- item.style.display = 'inline-block'; // Align items horizontally
136
- item.style.marginRight = '10px'; // Add some space between items
137
- item.style.textAlign = 'center'; // Center align the ingredient name and button
138
-
139
- const img = document.createElement('img');
140
- img.src = ingredient.image_url || 'https://via.placeholder.com/80';
141
- img.alt = ingredient.name;
142
- img.style.width = '80px'; // Set the image size
143
- img.style.height = '80px'; // Set the image size
144
-
145
- const name = document.createElement('div');
146
- name.textContent = ingredient.name;
147
- name.style.marginTop = '5px';
148
- name.style.fontSize = '12px'; // Adjust the font size of the name
149
-
150
- const button = document.createElement('button');
151
- button.textContent = 'Add';
152
- button.onclick = () => {
153
- if (!selectedIngredients.some(item => item.name === ingredient.name)) {
154
- selectedIngredients.push(ingredient);
155
- displaySelectedIngredients(); // Optionally update the list of selected ingredients elsewhere
156
- }
157
- };
158
-
159
- item.appendChild(img);
160
- item.appendChild(name);
161
- item.appendChild(button);
162
- ingredientsList.appendChild(item);
163
- });
164
- }
165
- // Display selected ingredients for a menu item (without submit button)
166
- function displayMenuItemIngredientsWithoutSubmit(menuItem) {
167
  const chatMessages = document.getElementById('chatMessages');
168
  if (!chatMessages) {
169
- console.error('Chat messages container not found for ingredients!');
170
  return;
171
  }
172
 
173
- // Create or find the menu item ingredients list container
174
- let ingredientsList = document.querySelector(`.ingredients-list-for-${menuItem.name}`);
175
  if (!ingredientsList) {
176
  ingredientsList = document.createElement('div');
177
- ingredientsList.className = `ingredients-list-for-${menuItem.name}`;
178
- ingredientsList.style.overflowX = 'auto'; // Enable horizontal scrolling
179
- ingredientsList.style.whiteSpace = 'nowrap'; // Prevent line breaks to make it horizontal scroll
180
- chatMessages.appendChild(ingredientsList);
181
- } else {
182
- ingredientsList.innerHTML = ''; // Clear previous content
183
- }
184
-
185
- // Loop through each selected ingredient and display it with the "Add" button
186
- menuItem.ingredients.forEach(ingredient => {
187
- const item = document.createElement('div');
188
- item.className = 'ingredient-item';
189
- item.style.display = 'inline-block'; // Align items horizontally
190
- item.style.marginRight = '10px'; // Add some space between items
191
- item.style.textAlign = 'center'; // Center align the ingredient name and button
192
-
193
- const img = document.createElement('img');
194
- img.src = ingredient.image_url || 'https://via.placeholder.com/80';
195
- img.alt = ingredient.name;
196
- img.style.width = '80px'; // Set the image size
197
- img.style.height = '80px'; // Set the image size
198
-
199
- const name = document.createElement('div');
200
- name.textContent = ingredient.name;
201
- name.style.marginTop = '5px';
202
- name.style.fontSize = '12px'; // Adjust the font size of the name
203
-
204
- const button = document.createElement('button');
205
- button.textContent = 'Add';
206
- button.onclick = () => {
207
- if (!selectedIngredients.some(item => item.name === ingredient.name)) {
208
- selectedIngredients.push(ingredient); // Add ingredient to the selected list
209
- displayMenuItemIngredientsWithoutSubmit(menuItem); // Update the list of selected ingredients
210
- }
211
- };
212
-
213
- item.appendChild(img);
214
- item.appendChild(name);
215
- item.appendChild(button);
216
- ingredientsList.appendChild(item);
217
- });
218
- }
219
-
220
- // Display the fetched ingredients
221
- function displayIngredientsList(ingredients) {
222
- const chatMessages = document.getElementById('chatMessages');
223
- if (!chatMessages) {
224
- console.error('Chat messages container not found for ingredients!');
225
- return;
226
- }
227
-
228
- let ingredientsList = document.querySelector('.ingredients-list');
229
- if (!ingredientsList) {
230
- ingredientsList = document.createElement('div');
231
- ingredientsList.className = 'ingredients-list';
232
  chatMessages.appendChild(ingredientsList);
233
  } else {
234
  ingredientsList.innerHTML = '';
@@ -236,7 +125,7 @@ function displayIngredientsList(ingredients) {
236
 
237
  ingredients.forEach(ingredient => {
238
  const item = document.createElement('div');
239
- item.className = 'ingredient-item';
240
  const img = document.createElement('img');
241
  img.src = ingredient.image_url || 'https://via.placeholder.com/80';
242
  img.alt = ingredient.name;
@@ -250,7 +139,7 @@ function displayIngredientsList(ingredients) {
250
  button.onclick = () => {
251
  if (!selectedIngredients.some(item => item.name === ingredient.name)) {
252
  selectedIngredients.push(ingredient);
253
- displaySelectedIngredients();
254
  }
255
  };
256
  item.appendChild(img);
@@ -260,93 +149,6 @@ function displayIngredientsList(ingredients) {
260
  });
261
  }
262
 
263
- // Show the instruction box for entering custom instructions
264
- function displayCustomizationInput() {
265
- const chatMessages = document.getElementById('chatMessages');
266
- if (!chatMessages) {
267
- console.error('Chat messages container not found for customization input!');
268
- return;
269
- }
270
-
271
- let customizationArea = document.querySelector('.customization-input');
272
- if (!customizationArea) {
273
- customizationArea = document.createElement('div');
274
- customizationArea.className = 'customization-input';
275
- const textarea = document.createElement('textarea');
276
- textarea.placeholder = 'Enter any special instructions...';
277
- const submitButton = document.createElement('button');
278
- submitButton.textContent = 'Submit Instructions';
279
- submitButton.onclick = () => {
280
- const instructions = textarea.value.trim();
281
- if (instructions) {
282
- addMessage('user', instructions);
283
- conversation.push({ role: 'user', message: instructions });
284
- }
285
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
286
- addMessage('bot', 'Item added to cart! Would you like to order more?');
287
- const options = [
288
- { text: 'Yes', class: 'green' },
289
- { text: 'No', class: 'red' }
290
- ];
291
- displayOptions(options);
292
- selectedMenuItem = null;
293
- selectedIngredients = [];
294
- };
295
- customizationArea.appendChild(textarea);
296
- customizationArea.appendChild(submitButton);
297
- chatMessages.appendChild(customizationArea);
298
- }
299
- }
300
-
301
- function addToCart(item) {
302
- cart.push(item);
303
- console.log('Cart:', cart);
304
- }
305
-
306
-
307
- // New step to display ingredients from the 'both' condition
308
-
309
-
310
-
311
-
312
-
313
-
314
- function displayOptions(options) {
315
- const chatMessages = document.getElementById('chatMessages');
316
- if (!chatMessages) {
317
- console.error('Chat messages container not found for options!');
318
- return;
319
- }
320
- options.forEach(opt => {
321
- const button = document.createElement('button');
322
- button.textContent = opt.text;
323
- button.className = `option-button ${opt.class}`;
324
- button.onclick = () => {
325
- addMessage('user', opt.text);
326
- conversation.push({ role: 'user', message: opt.text });
327
- chatMessages.innerHTML = '';
328
- conversation.forEach(msg => addMessage(msg.role, msg.message));
329
- setTimeout(() => handleResponse(opt.text), 500);
330
- };
331
- chatMessages.appendChild(button);
332
- });
333
- chatMessages.appendChild(document.createElement('br'));
334
- const backButton = document.createElement('button');
335
- backButton.textContent = 'Go Back';
336
- backButton.className = 'option-button';
337
- backButton.onclick = () => {
338
- conversation.pop();
339
- selectedIngredients = [];
340
- selectedMenuItem = null;
341
- chatMessages.innerHTML = '';
342
- conversation.forEach(msg => addMessage(msg.role, msg.message));
343
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
344
- };
345
- chatMessages.appendChild(backButton);
346
- }
347
-
348
-
349
-
350
  function fetchIngredients(foodPreference) {
351
  fetch('/get_ingredients', {
352
  method: 'POST',
@@ -394,7 +196,47 @@ function fetchMenuItems(category) {
394
  });
395
  }
396
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
397
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
398
 
399
  function displayMenuItems(menuItems) {
400
  const chatMessages = document.getElementById('chatMessages');
@@ -561,7 +403,44 @@ function displayCustomizationInput() {
561
  }
562
  }
563
 
 
 
 
 
564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565
 
566
  document.getElementById('userInput').addEventListener('keypress', function(e) {
567
  if (e.key === 'Enter') {
 
35
  console.warn('Empty message!');
36
  }
37
  }
38
+
39
  function handleResponse(userInput) {
40
  const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
41
  let botResponse = '';
 
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
  } 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('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
63
+ conversation.push({ role: 'user', message: lastMessage });
64
+ console.log(`Non-veg option selected: ${lastMessage}`);
65
+ botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
66
+ fetchIngredients(lastMessage.toLowerCase());
67
+ return;
68
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
69
+ botResponse = 'Awesome! Let’s customize your dish. Here are some ingredients you can add:';
70
+ fetchCustomizationIngredients('both'); // New function for customization
71
  return;
72
  } else if (lastMessage.includes('no') && selectedMenuItem) {
73
  addToCart(selectedMenuItem);
 
84
  displayOptions(options);
85
  }
86
  }
87
+ function fetchCustomizationIngredients(foodPreference) {
 
88
  fetch('/get_ingredients', {
89
  method: 'POST',
90
  headers: {
91
  'Content-Type': 'application/json',
92
  },
93
+ body: JSON.stringify({ dietary_preference: foodPreference })
94
  })
95
  .then(response => response.json())
96
  .then(data => {
97
+ if (data.error) {
98
+ addMessage('bot', `Sorry, there was an error fetching customization ingredients: ${data.error}`);
 
 
 
 
 
 
 
 
 
 
 
 
99
  } else {
100
+ const ingredients = data.ingredients || [];
101
+ addMessage('bot', 'Select ingredients to customize your dish:');
102
+ displayCustomizationIngredients(ingredients); // New function for customization UI
103
+ displaySelectedIngredientsForCustomization(); // New function for selected ingredients
104
  }
105
  })
106
  .catch(error => {
107
  addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
108
  });
109
  }
110
+ function displayCustomizationIngredients(ingredients) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111
  const chatMessages = document.getElementById('chatMessages');
112
  if (!chatMessages) {
113
+ console.error('Chat messages container not found for customization ingredients!');
114
  return;
115
  }
116
 
117
+ let ingredientsList = document.querySelector('.customization-ingredients-list');
 
118
  if (!ingredientsList) {
119
  ingredientsList = document.createElement('div');
120
+ ingredientsList.className = 'customization-ingredients-list ingredients-list'; // Reuse .ingredients-list CSS
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  chatMessages.appendChild(ingredientsList);
122
  } else {
123
  ingredientsList.innerHTML = '';
 
125
 
126
  ingredients.forEach(ingredient => {
127
  const item = document.createElement('div');
128
+ item.className = 'ingredient-item'; // Same CSS as original
129
  const img = document.createElement('img');
130
  img.src = ingredient.image_url || 'https://via.placeholder.com/80';
131
  img.alt = ingredient.name;
 
139
  button.onclick = () => {
140
  if (!selectedIngredients.some(item => item.name === ingredient.name)) {
141
  selectedIngredients.push(ingredient);
142
+ displaySelectedIngredientsForCustomization();
143
  }
144
  };
145
  item.appendChild(img);
 
149
  });
150
  }
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  function fetchIngredients(foodPreference) {
153
  fetch('/get_ingredients', {
154
  method: 'POST',
 
196
  });
197
  }
198
 
199
+ function displayIngredientsList(ingredients) {
200
+ const chatMessages = document.getElementById('chatMessages');
201
+ if (!chatMessages) {
202
+ console.error('Chat messages container not found for ingredients!');
203
+ return;
204
+ }
205
+
206
+ let ingredientsList = document.querySelector('.ingredients-list');
207
+ if (!ingredientsList) {
208
+ ingredientsList = document.createElement('div');
209
+ ingredientsList.className = 'ingredients-list';
210
+ chatMessages.appendChild(ingredientsList);
211
+ } else {
212
+ ingredientsList.innerHTML = '';
213
+ }
214
 
215
+ ingredients.forEach(ingredient => {
216
+ const item = document.createElement('div');
217
+ item.className = 'ingredient-item';
218
+ const img = document.createElement('img');
219
+ img.src = ingredient.image_url || 'https://via.placeholder.com/80';
220
+ img.alt = ingredient.name;
221
+ const name = document.createElement('div');
222
+ name.textContent = ingredient.name;
223
+ name.style.textAlign = 'center';
224
+ name.style.marginTop = '5px';
225
+ name.style.fontSize = '12px';
226
+ const button = document.createElement('button');
227
+ button.textContent = 'Add';
228
+ button.onclick = () => {
229
+ if (!selectedIngredients.some(item => item.name === ingredient.name)) {
230
+ selectedIngredients.push(ingredient);
231
+ displaySelectedIngredients();
232
+ }
233
+ };
234
+ item.appendChild(img);
235
+ item.appendChild(name);
236
+ item.appendChild(button);
237
+ ingredientsList.appendChild(item);
238
+ });
239
+ }
240
 
241
  function displayMenuItems(menuItems) {
242
  const chatMessages = document.getElementById('chatMessages');
 
403
  }
404
  }
405
 
406
+ function addToCart(item) {
407
+ cart.push(item);
408
+ console.log('Cart:', cart);
409
+ }
410
 
411
+ function displayOptions(options) {
412
+ const chatMessages = document.getElementById('chatMessages');
413
+ if (!chatMessages) {
414
+ console.error('Chat messages container not found for options!');
415
+ return;
416
+ }
417
+ options.forEach(opt => {
418
+ const button = document.createElement('button');
419
+ button.textContent = opt.text;
420
+ button.className = `option-button ${opt.class}`;
421
+ button.onclick = () => {
422
+ addMessage('user', opt.text);
423
+ conversation.push({ role: 'user', message: opt.text });
424
+ chatMessages.innerHTML = '';
425
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
426
+ setTimeout(() => handleResponse(opt.text), 500);
427
+ };
428
+ chatMessages.appendChild(button);
429
+ });
430
+ chatMessages.appendChild(document.createElement('br'));
431
+ const backButton = document.createElement('button');
432
+ backButton.textContent = 'Go Back';
433
+ backButton.className = 'option-button';
434
+ backButton.onclick = () => {
435
+ conversation.pop();
436
+ selectedIngredients = [];
437
+ selectedMenuItem = null;
438
+ chatMessages.innerHTML = '';
439
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
440
+ setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
441
+ };
442
+ chatMessages.appendChild(backButton);
443
+ }
444
 
445
  document.getElementById('userInput').addEventListener('keypress', function(e) {
446
  if (e.key === 'Enter') {