Spaces:
Sleeping
Sleeping
Update templates/index.html
Browse files- templates/index.html +149 -153
templates/index.html
CHANGED
|
@@ -430,121 +430,157 @@
|
|
| 430 |
}
|
| 431 |
displayCart();
|
| 432 |
}
|
| 433 |
-
|
| 434 |
function handleResponse(userInput) {
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
| 440 |
-
|
| 441 |
-
|
| 442 |
-
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
| 450 |
-
|
| 451 |
-
|
| 452 |
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
| 457 |
-
|
| 458 |
-
|
| 459 |
-
|
| 460 |
-
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
|
| 464 |
-
|
| 465 |
-
|
| 466 |
-
|
| 467 |
-
|
| 468 |
-
|
| 469 |
-
|
| 470 |
-
|
| 471 |
-
|
| 472 |
-
|
| 473 |
-
|
| 474 |
-
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
| 479 |
-
|
| 480 |
-
|
| 481 |
-
|
| 482 |
-
|
| 483 |
-
|
| 484 |
-
|
| 485 |
-
|
| 486 |
-
|
| 487 |
-
|
| 488 |
-
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
}
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
-
|
| 507 |
-
|
| 508 |
-
|
| 509 |
-
|
| 510 |
-
|
| 511 |
-
|
| 512 |
-
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
| 516 |
-
|
| 517 |
-
|
| 518 |
-
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
| 522 |
-
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
| 530 |
-
|
| 531 |
-
|
| 532 |
-
|
| 533 |
-
|
| 534 |
-
|
| 535 |
-
|
| 536 |
-
|
| 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 |
-
|
| 546 |
-
|
| 547 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
|
| 549 |
|
| 550 |
function handleYesResponse() {
|
|
@@ -783,47 +819,7 @@
|
|
| 783 |
});
|
| 784 |
}
|
| 785 |
|
| 786 |
-
|
| 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');
|