nagasurendra commited on
Commit
578ff22
·
verified ·
1 Parent(s): 4e2869f

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +150 -121
static/script.js CHANGED
@@ -36,88 +36,7 @@ function sendMessage() {
36
  }
37
  }
38
 
39
- function handleResponse(userInput) {
40
- const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
41
- let botResponse = '';
42
- let options = [];
43
-
44
- if (conversation.length === 2) { // After name input
45
- botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
46
- options = [
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
- botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
53
- fetchIngredients('non-vegetarian');
54
- return;
55
- } else if (lastMessage.includes('vegetarian')) {
56
- conversation.push({ role: 'user', message: 'Vegetarian' });
57
- botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
58
- fetchIngredients('vegetarian');
59
- return;
60
- } else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
61
- conversation.push({ role: 'user', message: lastMessage });
62
- botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
63
- fetchIngredients(lastMessage.toLowerCase());
64
- return;
65
- } else if (lastMessage.includes('yes') && selectedMenuItem) {
66
- // After clicking 'Yes' to customize, show ingredient selection for customization
67
- displayCustomizationInput(); // This shows the customization input area
68
- return;
69
- } else if (lastMessage.includes('no') && selectedMenuItem) {
70
- addToCart(selectedMenuItem);
71
- botResponse = 'Item added to cart! Would you like to order more?';
72
- options = [
73
- { text: 'Yes', class: 'green' },
74
- { text: 'No', class: 'red' }
75
- ];
76
- selectedMenuItem = null;
77
- }
78
-
79
- addMessage('bot', botResponse);
80
- if (options.length > 0) {
81
- displayOptions(options);
82
- }
83
- }
84
-
85
- function displayCustomizationInput() {
86
- const chatMessages = document.getElementById('chatMessages');
87
- if (!chatMessages) {
88
- console.error('Chat messages container not found for customization input!');
89
- return;
90
- }
91
 
92
- let customizationArea = document.querySelector('.customization-input');
93
- if (!customizationArea) {
94
- customizationArea = document.createElement('div');
95
- customizationArea.className = 'customization-input';
96
- const textarea = document.createElement('textarea');
97
- textarea.placeholder = 'Enter any special instructions...';
98
- const submitButton = document.createElement('button');
99
- submitButton.textContent = 'Submit Instructions';
100
- submitButton.onclick = () => {
101
- const instructions = textarea.value.trim();
102
- if (instructions) {
103
- addMessage('user', instructions);
104
- conversation.push({ role: 'user', message: instructions });
105
- }
106
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
107
- addMessage('bot', 'Item added to cart! Would you like to order more?');
108
- const options = [
109
- { text: 'Yes', class: 'green' },
110
- { text: 'No', class: 'red' }
111
- ];
112
- displayOptions(options);
113
- selectedMenuItem = null;
114
- selectedIngredients = [];
115
- };
116
- customizationArea.appendChild(textarea);
117
- customizationArea.appendChild(submitButton);
118
- chatMessages.appendChild(customizationArea);
119
- }
120
- }
121
 
122
  function fetchIngredients(foodPreference) {
123
  fetch('/get_ingredients', {
@@ -166,47 +85,7 @@ function fetchMenuItems(category) {
166
  });
167
  }
168
 
169
- function displayIngredientsList(ingredients) {
170
- const chatMessages = document.getElementById('chatMessages');
171
- if (!chatMessages) {
172
- console.error('Chat messages container not found for ingredients!');
173
- return;
174
- }
175
-
176
- let ingredientsList = document.querySelector('.ingredients-list');
177
- if (!ingredientsList) {
178
- ingredientsList = document.createElement('div');
179
- ingredientsList.className = 'ingredients-list';
180
- chatMessages.appendChild(ingredientsList);
181
- } else {
182
- ingredientsList.innerHTML = '';
183
- }
184
 
185
- ingredients.forEach(ingredient => {
186
- const item = document.createElement('div');
187
- item.className = 'ingredient-item';
188
- const img = document.createElement('img');
189
- img.src = ingredient.image_url || 'https://via.placeholder.com/80';
190
- img.alt = ingredient.name;
191
- const name = document.createElement('div');
192
- name.textContent = ingredient.name;
193
- name.style.textAlign = 'center';
194
- name.style.marginTop = '5px';
195
- name.style.fontSize = '12px';
196
- const button = document.createElement('button');
197
- button.textContent = 'Add';
198
- button.onclick = () => {
199
- if (!selectedIngredients.some(item => item.name === ingredient.name)) {
200
- selectedIngredients.push(ingredient);
201
- displaySelectedIngredients();
202
- }
203
- };
204
- item.appendChild(img);
205
- item.appendChild(name);
206
- item.appendChild(button);
207
- ingredientsList.appendChild(item);
208
- });
209
- }
210
 
211
  function displayMenuItems(menuItems) {
212
  const chatMessages = document.getElementById('chatMessages');
@@ -373,6 +252,155 @@ function displayCustomizationInput() {
373
  }
374
  }
375
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
376
  function addToCart(item) {
377
  cart.push(item);
378
  console.log('Cart:', cart);
@@ -412,6 +440,7 @@ function displayOptions(options) {
412
  chatMessages.appendChild(backButton);
413
  }
414
 
 
415
  document.getElementById('userInput').addEventListener('keypress', function(e) {
416
  if (e.key === 'Enter') {
417
  sendMessage();
 
36
  }
37
  }
38
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  function fetchIngredients(foodPreference) {
42
  fetch('/get_ingredients', {
 
85
  });
86
  }
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
89
 
90
  function displayMenuItems(menuItems) {
91
  const chatMessages = document.getElementById('chatMessages');
 
252
  }
253
  }
254
 
255
+ function handleResponse(userInput) {
256
+ const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
257
+ let botResponse = '';
258
+ let options = [];
259
+
260
+ if (conversation.length === 2) { // After name input
261
+ botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
262
+ options = [
263
+ { text: 'Vegetarian', class: 'green' },
264
+ { text: 'Non-Vegetarian', class: 'red' }
265
+ ];
266
+ } else if (lastMessage.includes('non-vegetarian')) {
267
+ conversation.push({ role: 'user', message: 'Non-Vegetarian' });
268
+ botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
269
+ fetchIngredients('non-vegetarian');
270
+ return;
271
+ } else if (lastMessage.includes('vegetarian')) {
272
+ conversation.push({ role: 'user', message: 'Vegetarian' });
273
+ botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
274
+ fetchIngredients('vegetarian');
275
+ return;
276
+ } else if (lastMessage.includes('yes') && selectedMenuItem) {
277
+ // New step after "Yes" is clicked: Fetch ingredients based on 'both'
278
+ fetchNewStepIngredients();
279
+ return;
280
+ } else if (lastMessage.includes('no') && selectedMenuItem) {
281
+ addToCart(selectedMenuItem);
282
+ botResponse = 'Item added to cart! Would you like to order more?';
283
+ options = [
284
+ { text: 'Yes', class: 'green' },
285
+ { text: 'No', class: 'red' }
286
+ ];
287
+ selectedMenuItem = null;
288
+ }
289
+
290
+ addMessage('bot', botResponse);
291
+ if (options.length > 0) {
292
+ displayOptions(options);
293
+ }
294
+ }
295
+
296
+ // New function to fetch ingredients based on 'both' condition after clicking 'Yes'
297
+ function fetchNewStepIngredients() {
298
+ fetch('/get_ingredients', {
299
+ method: 'POST',
300
+ headers: {
301
+ 'Content-Type': 'application/json',
302
+ },
303
+ body: JSON.stringify({ dietary_preference: 'both' })
304
+ })
305
+ .then(response => response.json())
306
+ .then(data => {
307
+ if (data.error) {
308
+ addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
309
+ } else {
310
+ const ingredients = data.ingredients || [];
311
+ addMessage('bot', 'Please select ingredients to add to your dish:');
312
+ displayIngredientsList(ingredients);
313
+ displaySelectedIngredients();
314
+ // Show the instruction box
315
+ displayCustomizationInput();
316
+ }
317
+ })
318
+ .catch(error => {
319
+ addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
320
+ });
321
+ }
322
+
323
+ // New step to display ingredients from the 'both' condition
324
+ function displayIngredientsList(ingredients) {
325
+ const chatMessages = document.getElementById('chatMessages');
326
+ if (!chatMessages) {
327
+ console.error('Chat messages container not found for ingredients!');
328
+ return;
329
+ }
330
+
331
+ let ingredientsList = document.querySelector('.ingredients-list');
332
+ if (!ingredientsList) {
333
+ ingredientsList = document.createElement('div');
334
+ ingredientsList.className = 'ingredients-list';
335
+ chatMessages.appendChild(ingredientsList);
336
+ } else {
337
+ ingredientsList.innerHTML = '';
338
+ }
339
+
340
+ ingredients.forEach(ingredient => {
341
+ const item = document.createElement('div');
342
+ item.className = 'ingredient-item';
343
+ const img = document.createElement('img');
344
+ img.src = ingredient.image_url || 'https://via.placeholder.com/80';
345
+ img.alt = ingredient.name;
346
+ const name = document.createElement('div');
347
+ name.textContent = ingredient.name;
348
+ name.style.textAlign = 'center';
349
+ name.style.marginTop = '5px';
350
+ name.style.fontSize = '12px';
351
+ const button = document.createElement('button');
352
+ button.textContent = 'Add';
353
+ button.onclick = () => {
354
+ if (!selectedIngredients.some(item => item.name === ingredient.name)) {
355
+ selectedIngredients.push(ingredient);
356
+ displaySelectedIngredients();
357
+ }
358
+ };
359
+ item.appendChild(img);
360
+ item.appendChild(name);
361
+ item.appendChild(button);
362
+ ingredientsList.appendChild(item);
363
+ });
364
+ }
365
+
366
+ // Show the instruction box for entering custom instructions
367
+ function displayCustomizationInput() {
368
+ const chatMessages = document.getElementById('chatMessages');
369
+ if (!chatMessages) {
370
+ console.error('Chat messages container not found for customization input!');
371
+ return;
372
+ }
373
+
374
+ let customizationArea = document.querySelector('.customization-input');
375
+ if (!customizationArea) {
376
+ customizationArea = document.createElement('div');
377
+ customizationArea.className = 'customization-input';
378
+ const textarea = document.createElement('textarea');
379
+ textarea.placeholder = 'Enter any special instructions...';
380
+ const submitButton = document.createElement('button');
381
+ submitButton.textContent = 'Submit Instructions';
382
+ submitButton.onclick = () => {
383
+ const instructions = textarea.value.trim();
384
+ if (instructions) {
385
+ addMessage('user', instructions);
386
+ conversation.push({ role: 'user', message: instructions });
387
+ }
388
+ addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
389
+ addMessage('bot', 'Item added to cart! Would you like to order more?');
390
+ const options = [
391
+ { text: 'Yes', class: 'green' },
392
+ { text: 'No', class: 'red' }
393
+ ];
394
+ displayOptions(options);
395
+ selectedMenuItem = null;
396
+ selectedIngredients = [];
397
+ };
398
+ customizationArea.appendChild(textarea);
399
+ customizationArea.appendChild(submitButton);
400
+ chatMessages.appendChild(customizationArea);
401
+ }
402
+ }
403
+
404
  function addToCart(item) {
405
  cart.push(item);
406
  console.log('Cart:', cart);
 
440
  chatMessages.appendChild(backButton);
441
  }
442
 
443
+
444
  document.getElementById('userInput').addEventListener('keypress', function(e) {
445
  if (e.key === 'Enter') {
446
  sendMessage();