nagasurendra commited on
Commit
e658490
·
verified ·
1 Parent(s): cdc2844

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +149 -153
templates/index.html CHANGED
@@ -430,121 +430,157 @@
430
  }
431
  displayCart();
432
  }
433
-
434
  function handleResponse(userInput) {
435
- const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
436
- let botResponse = '';
437
- let options = [];
438
-
439
- // Check for previous states and handle accordingly
440
- if (conversation.length === 2) { // After name input
441
- currentStep = 'food_type';
442
- botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
443
- options = [
444
- { text: 'Vegetarian', class: 'green' },
445
- { text: 'Non-Vegetarian', class: 'red' }
446
- ];
447
- } else if (lastMessage.includes('non-vegetarian')) {
448
- console.log('User selected non-vegetarian option.');
449
- currentStep = 'select_ingredients';
450
- botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
451
- fetchIngredients('non-vegetarian');
452
- return;
453
- } else if (lastMessage.includes('vegetarian')) {
454
- console.log('User selected vegetarian option.');
455
- currentStep = 'select_ingredients';
456
- botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
457
- fetchIngredients('vegetarian');
458
- return;
459
- } else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
460
- currentStep = 'select_ingredients';
461
- botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
462
- fetchIngredients(lastMessage.toLowerCase());
463
- return;
464
- } else if (lastMessage.includes('yes') && selectedMenuItem) {
465
- botResponse = 'Here are some ingredients to customize your dish:';
466
- handleYesResponse();
467
- return;
468
- } else if (lastMessage.includes('no') && selectedMenuItem) {
469
- // Submit the dish without customization
470
- submitCustomizationIngredients();
471
- return;
472
- } else if (lastMessage.includes('yes') && currentStep === 'post_cart') {
473
- // After user says 'yes', ask again for food preference (veg or non-veg)
474
- botResponse = `Let's get started again! What type of food would you prefer this time?`;
475
- options = [
476
- { text: 'Vegetarian', class: 'green' },
477
- { text: 'Non-Vegetarian', class: 'red' }
478
- ];
479
- currentStep = 'food_type';
480
- addMessage('bot', botResponse);
481
- displayOptions(options);
482
- return;
483
- } else if (lastMessage.includes('no') && currentStep === 'post_cart') {
484
- addMessage('bot', 'Awesome! 🧾 Here’s your final cart:');
485
- displayCart(); // Optional: show final cart again
486
- addMessage('bot', 'Thank you for your order! 👨‍🍳🍲');
487
- currentStep = 'end'; // Step after cart
488
- return;
489
- } else if (currentStep === 'customization') {
490
- currentStep = 'menu_display'; // Reverting to menu display after customization
491
- console.log('Step changed to menu_display.');
492
- fetchMenuItems({ ingredient_names: ingredientNames });
493
- return;
494
- }
495
-
496
- addMessage('bot', botResponse);
497
- if (options.length > 0) {
498
- displayOptions(options);
499
- }
500
- displayCart();
501
- }
502
-
503
- function resetToPreviousStep() {
504
- console.log('Resetting to previous step. Current step:', currentStep);
505
-
506
- // Clear selected ingredients and selected menu item, but not the cart
507
- selectedIngredients = [];
508
- selectedMenuItem = null;
509
- cart = [];
510
-
511
- // Based on the currentStep, go back to the previous step
512
- if (currentStep === 'food_type') {
513
- currentStep = 'greeting';
514
- conversation.push({ role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" });
515
- console.log('Step changed to greeting. Conversation updated.');
516
- } else if (currentStep === 'select_ingredients') {
517
- currentStep = 'food_type';
518
- conversation.push({ role: 'bot', message: 'What type of food would you prefer?' });
519
- console.log('Step changed to food_type. Conversation updated.');
520
- } else if (currentStep === 'customization') {
521
- currentStep = 'menu_display';
522
- console.log('Step changed to menu_display.');
523
- // If needed, fetch the menu items again
524
- //fetchMenuItems({ ingredient_names: ingredientNames });
525
- } else if (currentStep === 'post_cart') {
526
- currentStep = 'food_type';
527
- conversation.push({ role: 'bot', message: 'What type of food would you prefer?' });
528
- console.log('Step changed to food_type after post_cart. Conversation updated.');
529
- } else {
530
- console.error('Unexpected currentStep value:', currentStep);
531
- }
532
-
533
- // After going back to 'select_ingredients', trigger the fetchIngredients again
534
- if (currentStep === 'select_ingredients') {
535
- const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
536
- if (lastMessage.includes('vegetarian')) {
537
- console.log('Fetching vegetarian ingredients again after back.');
538
- fetchIngredients('vegetarian');
539
- } else if (lastMessage.includes('non-vegetarian')) {
540
- console.log('Fetching non-vegetarian ingredients again after back.');
541
- fetchIngredients('non-vegetarian');
542
  }
543
- }
544
 
545
- // Ensure the bot message is updated according to the previous step
546
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
547
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
548
 
549
 
550
  function handleYesResponse() {
@@ -783,47 +819,7 @@
783
  });
784
  }
785
 
786
- function displayIngredientsList(ingredients) {
787
- const chatMessages = document.getElementById('chatMessages');
788
- if (!chatMessages) {
789
- console.error('Chat messages container not found for ingredients!');
790
- return;
791
- }
792
-
793
- // Clear previous ingredients list if it exists
794
- let ingredientsList = document.querySelector('.ingredients-list');
795
- if (!ingredientsList) {
796
- ingredientsList = document.createElement('div');
797
- ingredientsList.className = 'ingredients-list';
798
- chatMessages.appendChild(ingredientsList);
799
- } else {
800
- // Clear existing items
801
- ingredientsList.innerHTML = '';
802
- }
803
-
804
- ingredients.forEach(ingredient => {
805
- const item = document.createElement('div');
806
- item.className = 'ingredient-item';
807
- const img = document.createElement('img');
808
- img.src = ingredient.image_url || 'https://via.placeholder.com/120';
809
- img.alt = ingredient.name;
810
- const name = document.createElement('div');
811
- name.textContent = ingredient.name;
812
- const button = document.createElement('button');
813
- button.textContent = 'Add';
814
- button.onclick = () => {
815
- if (!selectedIngredients.some(item => item.name === ingredient.name)) {
816
- selectedIngredients.push(ingredient);
817
- displaySelectedIngredients();
818
- }
819
- };
820
- item.appendChild(img);
821
- item.appendChild(name);
822
- item.appendChild(button);
823
- ingredientsList.appendChild(item);
824
- });
825
- }
826
-
827
 
828
  function displayMenuItems(menuItems) {
829
  const chatMessages = document.getElementById('chatMessages');
 
430
  }
431
  displayCart();
432
  }
 
433
  function handleResponse(userInput) {
434
+ const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
435
+ let botResponse = '';
436
+ let options = [];
437
+
438
+ // Reset the ingredients list before displaying the new ingredients
439
+ const chatMessages = document.getElementById('chatMessages');
440
+ if (!chatMessages) {
441
+ console.error('Chat messages container not found!');
442
+ return;
443
+ }
444
+
445
+ // Clear any previous ingredient lists
446
+ let ingredientList = document.querySelector('.ingredients-list');
447
+ if (ingredientList) {
448
+ ingredientList.innerHTML = '';
449
+ }
450
+
451
+ // Handle food type and display ingredients accordingly
452
+ if (conversation.length === 2) { // After name input
453
+ currentStep = 'food_type';
454
+ botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
455
+ options = [
456
+ { text: 'Vegetarian', class: 'green' },
457
+ { text: 'Non-Vegetarian', class: 'red' }
458
+ ];
459
+ } else if (lastMessage.includes('non-vegetarian')) {
460
+ currentStep = 'select_ingredients';
461
+ botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
462
+ fetchIngredients('non-vegetarian');
463
+ return;
464
+ } else if (lastMessage.includes('vegetarian')) {
465
+ currentStep = 'select_ingredients';
466
+ botResponse = 'Great choice! 🍽️ Please select a vegetarian option:';
467
+ fetchIngredients('vegetarian');
468
+ return;
469
+ }
470
+
471
+ // Continue with other conditions and send response
472
+ addMessage('bot', botResponse);
473
+ if (options.length > 0) {
474
+ displayOptions(options);
475
+ }
476
+ displayCart();
477
+ }
478
+
479
+ function fetchIngredients(foodPreference) {
480
+ console.log('Fetching ingredients for preference:', foodPreference); // Log the request to ensure it's being called
481
+ fetch('/get_ingredients', {
482
+ method: 'POST',
483
+ headers: { 'Content-Type': 'application/json' },
484
+ body: JSON.stringify({ dietary_preference: foodPreference })
485
+ })
486
+ .then(response => response.json())
487
+ .then(data => {
488
+ if (data.error) {
489
+ addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
490
+ } else {
491
+ const ingredients = data.ingredients || [];
492
+ addMessage('bot', 'Please select ingredients:');
493
+ displayIngredientsList(ingredients);
494
+ }
495
+ })
496
+ .catch(error => {
497
+ console.error('Error fetching ingredients:', error); // Log any errors in the fetch call
498
+ addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
499
+ });
500
+ }
501
+
502
+ function displayIngredientsList(ingredients) {
503
+ const chatMessages = document.getElementById('chatMessages');
504
+ if (!chatMessages) {
505
+ console.error('Chat messages container not found for ingredients!');
506
+ return;
507
+ }
508
+
509
+ let ingredientsList = document.createElement('div');
510
+ ingredientsList.className = 'ingredients-list';
511
+ chatMessages.appendChild(ingredientsList);
512
+
513
+ ingredients.forEach(ingredient => {
514
+ const item = document.createElement('div');
515
+ item.className = 'ingredient-item';
516
+ const img = document.createElement('img');
517
+ img.src = ingredient.image_url || 'https://via.placeholder.com/120';
518
+ img.alt = ingredient.name;
519
+ const name = document.createElement('div');
520
+ name.textContent = ingredient.name;
521
+ const button = document.createElement('button');
522
+ button.textContent = 'Add';
523
+ button.onclick = () => {
524
+ if (!selectedIngredients.some(item => item.name === ingredient.name)) {
525
+ selectedIngredients.push(ingredient);
526
+ displaySelectedIngredients();
527
+ }
528
+ };
529
+ item.appendChild(img);
530
+ item.appendChild(name);
531
+ item.appendChild(button);
532
+ ingredientsList.appendChild(item);
533
+ });
534
+
535
+ displaySelectedIngredients();
 
 
 
 
 
536
  }
 
537
 
538
+
539
+ function resetToPreviousStep() {
540
+ console.log('Resetting to previous step. Current step:', currentStep);
541
+
542
+ // Clear selected ingredients and selected menu item, but not the cart
543
+ selectedIngredients = [];
544
+ selectedMenuItem = null;
545
+ cart = [];
546
+
547
+ // Based on the currentStep, go back to the previous step
548
+ if (currentStep === 'food_type') {
549
+ currentStep = 'greeting';
550
+ conversation.push({ role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" });
551
+ console.log('Step changed to greeting. Conversation updated.');
552
+ } else if (currentStep === 'select_ingredients') {
553
+ currentStep = 'food_type';
554
+ conversation.push({ role: 'bot', message: 'What type of food would you prefer?' });
555
+ console.log('Step changed to food_type. Conversation updated.');
556
+ } else if (currentStep === 'customization') {
557
+ currentStep = 'menu_display';
558
+ console.log('Step changed to menu_display.');
559
+ // If needed, fetch the menu items again
560
+ //fetchMenuItems({ ingredient_names: ingredientNames });
561
+ } else if (currentStep === 'post_cart') {
562
+ currentStep = 'food_type';
563
+ conversation.push({ role: 'bot', message: 'What type of food would you prefer?' });
564
+ console.log('Step changed to food_type after post_cart. Conversation updated.');
565
+ } else {
566
+ console.error('Unexpected currentStep value:', currentStep);
567
+ }
568
+
569
+ // After going back to 'select_ingredients', trigger the fetchIngredients again
570
+ if (currentStep === 'select_ingredients') {
571
+ const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
572
+ if (lastMessage.includes('vegetarian')) {
573
+ console.log('Fetching vegetarian ingredients again after back.');
574
+ fetchIngredients('vegetarian');
575
+ } else if (lastMessage.includes('non-vegetarian')) {
576
+ console.log('Fetching non-vegetarian ingredients again after back.');
577
+ fetchIngredients('non-vegetarian');
578
+ }
579
+ }
580
+
581
+ // Ensure the bot message is updated according to the previous step
582
+ setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
583
+ }
584
 
585
 
586
  function handleYesResponse() {
 
819
  });
820
  }
821
 
822
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
823
 
824
  function displayMenuItems(menuItems) {
825
  const chatMessages = document.getElementById('chatMessages');