lokesh341 commited on
Commit
109dc1d
·
verified ·
1 Parent(s): 9f57ab7

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +178 -144
templates/index.html CHANGED
@@ -158,7 +158,7 @@
158
  background-color: #218838;
159
  }
160
 
161
- .selected-ingredients {
162
  background-color: #f1f8ff;
163
  padding: 15px;
164
  border: 1px solid #b3d7ff;
@@ -170,7 +170,7 @@
170
  gap: 10px;
171
  }
172
 
173
- .selected-ingredients div {
174
  background-color: #d6e9ff;
175
  padding: 5px 10px;
176
  border-radius: 6px;
@@ -181,7 +181,7 @@
181
  margin: 10px 0;
182
  }
183
 
184
- .customization-input textarea {
185
  width: 100%;
186
  padding: 8px;
187
  border: 1px solid #b3d7ff;
@@ -191,7 +191,7 @@
191
  margin-bottom: 10px;
192
  }
193
 
194
- .customization-input button, .submit-button {
195
  padding: 10px 20px;
196
  background-color: #0078d4;
197
  color: #ffffff;
@@ -202,7 +202,7 @@
202
  transition: background-color 0.2s;
203
  }
204
 
205
- .customization-input button:hover, .submit-button:hover {
206
  background-color: #005ea2;
207
  }
208
 
@@ -337,7 +337,7 @@
337
  padding: 8px 15px;
338
  }
339
 
340
- .selected-ingredients, .cart-items {
341
  padding: 10px;
342
  gap: 8px;
343
  }
@@ -426,6 +426,7 @@
426
  } else {
427
  console.warn('Empty message!');
428
  }
 
429
  }
430
 
431
  function handleResponse(userInput) {
@@ -458,8 +459,8 @@
458
  fetchIngredients(lastMessage.toLowerCase());
459
  return;
460
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
461
- botResponse = 'Awesome! Let’s customize your dish. Here are some ingredients you can add:';
462
- fetchCustomizationIngredients('both');
463
  return;
464
  } else if (lastMessage.includes('no') && selectedMenuItem) {
465
  addToCart(selectedMenuItem);
@@ -469,36 +470,60 @@
469
  { text: 'No', class: 'red' }
470
  ];
471
  selectedMenuItem = null;
 
472
  }
473
 
474
  addMessage('bot', botResponse);
475
  if (options.length > 0) {
476
  displayOptions(options);
477
  }
 
478
  }
479
 
480
- function fetchCustomizationIngredients(foodPreference) {
481
- fetch('/get_ingredients', {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
482
  method: 'POST',
483
- headers: {
484
- 'Content-Type': 'application/json',
485
- },
486
  body: JSON.stringify({ dietary_preference: foodPreference })
487
  })
488
- .then(response => response.json())
489
- .then(data => {
490
- if (data.error) {
491
- addMessage('bot', `Sorry, there was an error fetching customization ingredients: ${data.error}`);
492
- } else {
493
- const ingredients = data.ingredients || [];
494
- addMessage('bot', 'Select ingredients to customize your dish:');
495
- displayCustomizationIngredients(ingredients);
496
- displaySelectedIngredientsForCustomization();
497
- }
498
- })
499
- .catch(error => {
500
- addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
501
- });
502
  }
503
 
504
  function displayCustomizationIngredients(ingredients) {
@@ -529,8 +554,8 @@
529
  button.textContent = 'Add';
530
  button.onclick = () => {
531
  if (!selectedIngredients.some(item => item.name === ingredient.name)) {
532
- selectedIngredients.push(ingredient);
533
- displaySelectedIngredientsForCustomization();
534
  }
535
  };
536
  item.appendChild(img);
@@ -538,24 +563,34 @@
538
  item.appendChild(button);
539
  ingredientsList.appendChild(item);
540
  });
 
 
541
  }
542
 
543
- function displaySelectedIngredientsForCustomization() {
544
  const chatMessages = document.getElementById('chatMessages');
545
  if (!chatMessages) {
546
- console.error('Chat messages container not found for selected ingredients!');
547
  return;
548
  }
549
 
550
- let selectedArea = document.querySelector('.selected-ingredients');
551
  if (!selectedArea) {
552
  selectedArea = document.createElement('div');
553
- selectedArea.className = 'selected-ingredients';
554
  chatMessages.appendChild(selectedArea);
555
  } else {
556
  selectedArea.innerHTML = '';
557
  }
558
 
 
 
 
 
 
 
 
 
559
  selectedIngredients.forEach(ingredient => {
560
  const div = document.createElement('div');
561
  div.textContent = ingredient.name;
@@ -570,58 +605,106 @@
570
  removeButton.style.cursor = 'pointer';
571
  removeButton.onclick = () => {
572
  selectedIngredients = selectedIngredients.filter(item => item.name !== ingredient.name);
573
- displaySelectedIngredientsForCustomization();
574
  };
575
  div.appendChild(removeButton);
576
  selectedArea.appendChild(div);
577
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
578
  }
579
 
580
  function fetchIngredients(foodPreference) {
581
  fetch('/get_ingredients', {
582
  method: 'POST',
583
- headers: {
584
- 'Content-Type': 'application/json',
585
- },
586
  body: JSON.stringify({ dietary_preference: foodPreference })
587
  })
588
- .then(response => response.json())
589
- .then(data => {
590
- if (data.error) {
591
- addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
592
- } else {
593
- const ingredients = data.ingredients || [];
594
- addMessage('bot', 'Please select ingredients:');
595
- displayIngredientsList(ingredients);
596
- displaySelectedIngredients();
597
- }
598
- })
599
- .catch(error => {
600
- addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
601
- });
602
  }
603
 
604
- function fetchMenuItems(ingredientNames) {
605
  fetch('/get_menu_items', {
606
  method: 'POST',
607
- headers: {
608
- 'Content-Type': 'application/json',
609
- },
610
- body: JSON.stringify({ ingredient_names: ingredientNames })
611
- })
612
- .then(response => response.json())
613
- .then(data => {
614
- if (data.error) {
615
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
616
- } else {
617
- const menuItems = data.menu_items || [];
618
- addMessage('bot', 'Here are some dishes based on your selection:');
619
- displayMenuItems(menuItems);
620
- }
621
  })
622
- .catch(error => {
623
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
624
- });
 
 
 
 
 
 
 
 
 
 
625
  }
626
 
627
  function displayIngredientsList(ingredients) {
@@ -754,62 +837,13 @@
754
  }
755
 
756
  function submitIngredients() {
757
- fetch('/submit_ingredients', {
758
- method: 'POST',
759
- headers: {
760
- 'Content-Type': 'application/json',
761
- },
762
- body: JSON.stringify({ ingredients: selectedIngredients })
763
- })
764
- .then(response => response.json())
765
- .then(data => {
766
- if (data.success) {
767
- const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
768
- fetchMenuItems(ingredientNames);
769
- } else {
770
- addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
771
- }
772
- })
773
- .catch(error => {
774
- addMessage('bot', `Error: ${error.message}`);
775
- });
776
- }
777
-
778
- function displayCustomizationInput() {
779
- const chatMessages = document.getElementById('chatMessages');
780
- if (!chatMessages) {
781
- console.error('Chat messages container not found for customization input!');
782
  return;
783
  }
784
 
785
- let customizationArea = document.querySelector('.customization-input');
786
- if (!customizationArea) {
787
- customizationArea = document.createElement('div');
788
- customizationArea.className = 'customization-input';
789
- const textarea = document.createElement('textarea');
790
- textarea.placeholder = 'Enter any special instructions...';
791
- const submitButton = document.createElement('button');
792
- submitButton.textContent = 'Submit Instructions';
793
- submitButton.onclick = () => {
794
- const instructions = textarea.value.trim();
795
- if (instructions) {
796
- addMessage('user', instructions);
797
- conversation.push({ role: 'user', message: instructions });
798
- }
799
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
800
- addMessage('bot', 'Item added to cart! Would you like to order more?');
801
- const options = [
802
- { text: 'Yes', class: 'green' },
803
- { text: 'No', class: 'red' }
804
- ];
805
- displayOptions(options);
806
- selectedMenuItem = null;
807
- selectedIngredients = [];
808
- };
809
- customizationArea.appendChild(textarea);
810
- customizationArea.appendChild(submitButton);
811
- chatMessages.appendChild(customizationArea);
812
- }
813
  }
814
 
815
  function addToCart(item) {
@@ -846,7 +880,12 @@
846
  img.src = item.image_url || 'https://via.placeholder.com/30';
847
  img.alt = item.name;
848
  const name = document.createElement('div');
849
- name.textContent = item.name + (item.instructions ? ` (${item.instructions})` : '');
 
 
 
 
 
850
  const removeButton = document.createElement('button');
851
  removeButton.className = 'remove-button';
852
  removeButton.textContent = 'X';
@@ -877,26 +916,24 @@
877
  return;
878
  }
879
 
880
- fetch('/submit_ingredients', {
881
  method: 'POST',
882
- headers: {
883
- 'Content-Type': 'application/json',
884
- },
885
  body: JSON.stringify({ items: cart })
886
  })
887
- .then(response => response.json())
888
- .then(data => {
889
- if (data.success) {
890
- addMessage('bot', 'Cart submitted successfully!');
891
- cart = [];
892
- displayCart();
893
- } else {
894
- addMessage('bot', `Error submitting cart: ${data.error}`);
895
- }
896
- })
897
- .catch(error => {
898
- addMessage('bot', `Error submitting cart: ${error.message}`);
899
- });
900
  }
901
 
902
  function displayOptions(options) {
@@ -912,8 +949,6 @@
912
  button.onclick = () => {
913
  addMessage('user', opt.text);
914
  conversation.push({ role: 'user', message: opt.text });
915
- chatMessages.innerHTML = '';
916
- conversation.forEach(msg => addMessage(msg.role, msg.message));
917
  setTimeout(() => handleResponse(opt.text), 500);
918
  };
919
  chatMessages.appendChild(button);
@@ -926,11 +961,10 @@
926
  conversation.pop();
927
  selectedIngredients = [];
928
  selectedMenuItem = null;
929
- chatMessages.innerHTML = '';
930
- conversation.forEach(msg => addMessage(msg.role, msg.message));
931
  setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
932
  };
933
  chatMessages.appendChild(backButton);
 
934
  }
935
 
936
  document.getElementById('userInput').addEventListener('keypress', function(e) {
 
158
  background-color: #218838;
159
  }
160
 
161
+ .selected-ingredients, .selected-customization-ingredients {
162
  background-color: #f1f8ff;
163
  padding: 15px;
164
  border: 1px solid #b3d7ff;
 
170
  gap: 10px;
171
  }
172
 
173
+ .selected-ingredients div, .selected-customization-ingredients div {
174
  background-color: #d6e9ff;
175
  padding: 5px 10px;
176
  border-radius: 6px;
 
181
  margin: 10px 0;
182
  }
183
 
184
+ .customization-input textarea, .selected-customization-ingredients textarea {
185
  width: 100%;
186
  padding: 8px;
187
  border: 1px solid #b3d7ff;
 
191
  margin-bottom: 10px;
192
  }
193
 
194
+ .customization-input button, .submit-button, .submit-customization-button, .submit-cart-button {
195
  padding: 10px 20px;
196
  background-color: #0078d4;
197
  color: #ffffff;
 
202
  transition: background-color 0.2s;
203
  }
204
 
205
+ .customization-input button:hover, .submit-button:hover, .submit-customization-button:hover, .submit-cart-button:hover {
206
  background-color: #005ea2;
207
  }
208
 
 
337
  padding: 8px 15px;
338
  }
339
 
340
+ .selected-ingredients, .selected-customization-ingredients, .cart-items {
341
  padding: 10px;
342
  gap: 8px;
343
  }
 
426
  } else {
427
  console.warn('Empty message!');
428
  }
429
+ displayCart();
430
  }
431
 
432
  function handleResponse(userInput) {
 
459
  fetchIngredients(lastMessage.toLowerCase());
460
  return;
461
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
462
+ botResponse = 'Here are some ingredients to customize your dish:';
463
+ handleYesResponse();
464
  return;
465
  } else if (lastMessage.includes('no') && selectedMenuItem) {
466
  addToCart(selectedMenuItem);
 
470
  { text: 'No', class: 'red' }
471
  ];
472
  selectedMenuItem = null;
473
+ selectedIngredients = [];
474
  }
475
 
476
  addMessage('bot', botResponse);
477
  if (options.length > 0) {
478
  displayOptions(options);
479
  }
480
+ displayCart();
481
  }
482
 
483
+ function handleYesResponse() {
484
+ if (!selectedMenuItem) {
485
+ addMessage('bot', 'No dish selected. Please choose a dish first.');
486
+ return;
487
+ }
488
+ const botResponse = `Here is your selected dish: ${selectedMenuItem.name}`;
489
+ addMessage('bot', botResponse);
490
+
491
+ // Display selected menu item
492
+ const chatMessages = document.getElementById('chatMessages');
493
+ const menuItemDiv = document.createElement('div');
494
+ menuItemDiv.className = 'menu-item';
495
+ const img = document.createElement('img');
496
+ img.src = selectedMenuItem.image_url || 'https://via.placeholder.com/120';
497
+ img.alt = selectedMenuItem.name;
498
+ const name = document.createElement('div');
499
+ name.textContent = selectedMenuItem.name;
500
+ menuItemDiv.appendChild(img);
501
+ menuItemDiv.appendChild(name);
502
+ chatMessages.appendChild(menuItemDiv);
503
+
504
+ // Fetch ingredients for customization
505
+ fetchIngredientsForCustomization('both');
506
+ }
507
+
508
+ function fetchIngredientsForCustomization(foodPreference) {
509
+ fetch('/get_ingredients', { // Reuse existing endpoint
510
  method: 'POST',
511
+ headers: { 'Content-Type': 'application/json' },
 
 
512
  body: JSON.stringify({ dietary_preference: foodPreference })
513
  })
514
+ .then(response => response.json())
515
+ .then(data => {
516
+ if (data.error) {
517
+ addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
518
+ } else {
519
+ const ingredients = data.ingredients || [];
520
+ addMessage('bot', 'Please select ingredients to customize:');
521
+ displayCustomizationIngredients(ingredients);
522
+ }
523
+ })
524
+ .catch(error => {
525
+ addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
526
+ });
 
527
  }
528
 
529
  function displayCustomizationIngredients(ingredients) {
 
554
  button.textContent = 'Add';
555
  button.onclick = () => {
556
  if (!selectedIngredients.some(item => item.name === ingredient.name)) {
557
+ selectedIngredients.push({ name: ingredient.name, image_url: ingredient.image_url });
558
+ displaySelectedCustomizationIngredients();
559
  }
560
  };
561
  item.appendChild(img);
 
563
  item.appendChild(button);
564
  ingredientsList.appendChild(item);
565
  });
566
+
567
+ displaySelectedCustomizationIngredients();
568
  }
569
 
570
+ function displaySelectedCustomizationIngredients() {
571
  const chatMessages = document.getElementById('chatMessages');
572
  if (!chatMessages) {
573
+ console.error('Chat messages container not found for selected customization ingredients!');
574
  return;
575
  }
576
 
577
+ let selectedArea = document.querySelector('.selected-customization-ingredients');
578
  if (!selectedArea) {
579
  selectedArea = document.createElement('div');
580
+ selectedArea.className = 'selected-customization-ingredients';
581
  chatMessages.appendChild(selectedArea);
582
  } else {
583
  selectedArea.innerHTML = '';
584
  }
585
 
586
+ const selectedIngredientsText = selectedIngredients.length > 0
587
+ ? `${selectedMenuItem.name} with ${selectedIngredients.map(ingredient => ingredient.name).join(', ')}`
588
+ : selectedMenuItem.name;
589
+
590
+ const ingredientsDiv = document.createElement('div');
591
+ ingredientsDiv.textContent = selectedIngredientsText;
592
+ selectedArea.appendChild(ingredientsDiv);
593
+
594
  selectedIngredients.forEach(ingredient => {
595
  const div = document.createElement('div');
596
  div.textContent = ingredient.name;
 
605
  removeButton.style.cursor = 'pointer';
606
  removeButton.onclick = () => {
607
  selectedIngredients = selectedIngredients.filter(item => item.name !== ingredient.name);
608
+ displaySelectedCustomizationIngredients();
609
  };
610
  div.appendChild(removeButton);
611
  selectedArea.appendChild(div);
612
  });
613
+
614
+ if (!document.querySelector('.submit-customization-button')) {
615
+ const textarea = document.createElement('textarea');
616
+ textarea.placeholder = 'Enter any special instructions...';
617
+ selectedArea.appendChild(textarea);
618
+
619
+ const submitButton = document.createElement('button');
620
+ submitButton.className = 'submit-customization-button';
621
+ submitButton.textContent = 'Submit Customization';
622
+ submitButton.onclick = submitCustomizationIngredients;
623
+ selectedArea.appendChild(submitButton);
624
+ }
625
+ }
626
+
627
+ function submitCustomizationIngredients() {
628
+ if (!selectedMenuItem) {
629
+ addMessage('bot', 'No dish selected. Please choose a dish first.');
630
+ return;
631
+ }
632
+
633
+ const textarea = document.querySelector('.selected-customization-ingredients textarea');
634
+ const instructions = textarea ? textarea.value.trim() : '';
635
+
636
+ fetch('/submit_customization_ingredients', {
637
+ method: 'POST',
638
+ headers: { 'Content-Type': 'application/json' },
639
+ body: JSON.stringify({
640
+ menu_item: selectedMenuItem,
641
+ ingredients: selectedIngredients,
642
+ instructions: instructions
643
+ })
644
+ })
645
+ .then(response => response.json())
646
+ .then(data => {
647
+ if (data.success) {
648
+ addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
649
+ addMessage('bot', 'Customization submitted successfully! Item added to cart. Would you like to order more?');
650
+ const options = [
651
+ { text: 'Yes', class: 'green' },
652
+ { text: 'No', class: 'red' }
653
+ ];
654
+ displayOptions(options);
655
+ selectedMenuItem = null;
656
+ selectedIngredients = [];
657
+ displayCart();
658
+ } else {
659
+ addMessage('bot', `Error: ${data.error}`);
660
+ }
661
+ })
662
+ .catch(error => {
663
+ addMessage('bot', `Error submitting customization: ${error.message}`);
664
+ });
665
  }
666
 
667
  function fetchIngredients(foodPreference) {
668
  fetch('/get_ingredients', {
669
  method: 'POST',
670
+ headers: { 'Content-Type': 'application/json' },
 
 
671
  body: JSON.stringify({ dietary_preference: foodPreference })
672
  })
673
+ .then(response => response.json())
674
+ .then(data => {
675
+ if (data.error) {
676
+ addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
677
+ } else {
678
+ const ingredients = data.ingredients || [];
679
+ addMessage('bot', 'Please select ingredients:');
680
+ displayIngredientsList(ingredients);
681
+ displaySelectedIngredients();
682
+ }
683
+ })
684
+ .catch(error => {
685
+ addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
686
+ });
687
  }
688
 
689
+ function fetchMenuItems(params) {
690
  fetch('/get_menu_items', {
691
  method: 'POST',
692
+ headers: { 'Content-Type': 'application/json' },
693
+ body: JSON.stringify(params)
 
 
 
 
 
 
 
 
 
 
 
 
694
  })
695
+ .then(response => response.json())
696
+ .then(data => {
697
+ if (data.error) {
698
+ addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
699
+ } else {
700
+ const menuItems = data.menu_items || [];
701
+ addMessage('bot', 'Here are some dishes based on your selection:');
702
+ displayMenuItems(menuItems);
703
+ }
704
+ })
705
+ .catch(error => {
706
+ addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
707
+ });
708
  }
709
 
710
  function displayIngredientsList(ingredients) {
 
837
  }
838
 
839
  function submitIngredients() {
840
+ if (selectedIngredients.length === 0) {
841
+ addMessage('bot', 'No ingredients selected. Please choose some ingredients.');
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
842
  return;
843
  }
844
 
845
+ const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
846
+ fetchMenuItems({ ingredient_names: ingredientNames });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
847
  }
848
 
849
  function addToCart(item) {
 
880
  img.src = item.image_url || 'https://via.placeholder.com/30';
881
  img.alt = item.name;
882
  const name = document.createElement('div');
883
+ const text = item.instructions
884
+ ? `${item.name} (${item.instructions})`
885
+ : item.name;
886
+ name.textContent = item.ingredients.length > 0
887
+ ? `${text} with ${item.ingredients.map(i => i.name).join(', ')}`
888
+ : text;
889
  const removeButton = document.createElement('button');
890
  removeButton.className = 'remove-button';
891
  removeButton.textContent = 'X';
 
916
  return;
917
  }
918
 
919
+ fetch('/submit_customization_ingredients', {
920
  method: 'POST',
921
+ headers: { 'Content-Type': 'application/json' },
 
 
922
  body: JSON.stringify({ items: cart })
923
  })
924
+ .then(response => response.json())
925
+ .then(data => {
926
+ if (data.success) {
927
+ addMessage('bot', 'Cart submitted successfully!');
928
+ cart = [];
929
+ displayCart();
930
+ } else {
931
+ addMessage('bot', `Error submitting cart: ${data.error}`);
932
+ }
933
+ })
934
+ .catch(error => {
935
+ addMessage('bot', `Error submitting cart: ${error.message}`);
936
+ });
937
  }
938
 
939
  function displayOptions(options) {
 
949
  button.onclick = () => {
950
  addMessage('user', opt.text);
951
  conversation.push({ role: 'user', message: opt.text });
 
 
952
  setTimeout(() => handleResponse(opt.text), 500);
953
  };
954
  chatMessages.appendChild(button);
 
961
  conversation.pop();
962
  selectedIngredients = [];
963
  selectedMenuItem = null;
 
 
964
  setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
965
  };
966
  chatMessages.appendChild(backButton);
967
+ chatMessages.scrollTop = chatMessages.scrollHeight;
968
  }
969
 
970
  document.getElementById('userInput').addEventListener('keypress', function(e) {