Spaces:
Build error
Build error
Update static/script.js
Browse files14 unmodified lines1516171819202122232425262728293031323334353637383940414243444546474846 unmodified lines959697989910010135 unmodified lines1371381391401411421431441451461471481491 unmodified line1511521531541551561579 unmodified lines16716816917017117217317417517636 unmodified lines21321421521621721821922022122239 unmodified lines26226326426526626726826927027143 unmodified lines3153163173183193201 unmodified line32232332432532632732832933033133234 unmodified lines3673683693703713723733743753763776 unmodified lines38438538638738838914 unmodified linesmessageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';messageDiv.textContent = message;chatMessages.appendChild(messageDiv);// Add "Go Back" button after bot messages, except for the initial greetingif (role === 'bot' && conversation.length > 1) {const backButton = document.createElement('button');backButton.textContent = 'Go Back';backButton.className = 'option-button';backButton.onclick = () => {// Remove the last user message and bot responseif (conversation.length > 1) {conversation.pop(); // Remove bot responseif (conversation.length > 1) {conversation.pop(); // Remove user input}selectedIngredients = []; // Clear selected ingredientsselectedMenuItem = null; // Clear selected menu itemchatMessages.innerHTML = ''; // Clear chat// Re-render conversationconversation.forEach(msg => addMessage(msg.role, msg.message));// Reprocess the last user input, if anyif (conversation.length > 1) {setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);}}};chatMessages.appendChild(backButton);chatMessages.appendChild(document.createElement('br'));}chatMessages.scrollTop = chatMessages.scrollHeight;console.log(`Added ${role} message: ${message}`);}46 unmodified linesreturn;} else if (lastMessage.includes('yes') && selectedMenuItem) {botResponse = 'Here are some ingredients to customize your dish:';fetchIngredients('both'); // Fetch both veg and non-veg ingredients for customizationreturn;} else if (lastMessage.includes('no') && selectedMenuItem) {addToCart(selectedMenuItem);35 unmodified lines});}function fetchMenuItems(ingredientNames) {fetch('/get_menu_items', {method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify({ ingredient_names: ingredientNames })}).then(response => response.json()).then(data => {1 unmodified lineaddMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);} else {const menuItems = data.menu_items || [];addMessage('bot', 'Here are some dishes based on your selected ingredients:');displayMenuItems(menuItems);}})9 unmodified linesreturn;}// Clear and re-render conversationchatMessages.innerHTML = '';conversation.forEach(msg => addMessage(msg.role, msg.message));let ingredientsList = document.querySelector('.ingredients-list');if (!ingredientsList) {ingredientsList = document.createElement('div');36 unmodified linesreturn;}// Clear and re-render conversationchatMessages.innerHTML = '';conversation.forEach(msg => addMessage(msg.role, msg.message));let menuItemsList = document.querySelector('.menu-items-list');if (!menuItemsList) {menuItemsList = document.createElement('div');39 unmodified linesreturn;}// Clear and re-render conversationchatMessages.innerHTML = '';conversation.forEach(msg => addMessage(msg.role, msg.message));let selectedArea = document.querySelector('.selected-ingredients');if (!selectedArea) {selectedArea = document.createElement('div');43 unmodified lines});}function displayCustomizationInput() {const chatMessages = document.getElementById('chatMessages');if (!chatMessages) {1 unmodified linereturn;}// Clear and re-render conversationchatMessages.innerHTML = '';conversation.forEach(msg => addMessage(msg.role, msg.message));let customizationArea = document.querySelector('.customization-input');if (!customizationArea) {customizationArea = document.createElement('div');customizationArea.className = 'customization-input';34 unmodified linesconsole.error('Chat messages container not found for options!');return;}// Clear and re-render conversationchatMessages.innerHTML = '';conversation.forEach(msg => addMessage(msg.role, msg.message));options.forEach(opt => {const button = document.createElement('button');button.textContent = opt.text;6 unmodified lineschatMessages.appendChild(button);});chatMessages.appendChild(document.createElement('br'));}document.getElementById('userInput').addEventListener('keypress', function(e) {14 unmodified lines15161718192046 unmodified lines6768697071727335 unmodified lines1091101111121131141151161171181191201211 unmodified line1231241251261271281299 unmodified lines13914014114214314436 unmodified lines18118218318418518639 unmodified lines22622722822923023143 unmodified lines2752762772782792802812822832842852862872882892902912922932942952962972982993003013023031 unmodified line30530630730830931031134 unmodified lines3463473483493503516 unmodified lines35835936036136236336436536636736836937037137237337437537637737837938038138214 unmodified linesmessageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';messageDiv.textContent = message;chatMessages.appendChild(messageDiv);chatMessages.scrollTop = chatMessages.scrollHeight;console.log(`Added ${role} message: ${message}`);}46 unmodified linesreturn;} else if (lastMessage.includes('yes') && selectedMenuItem) {botResponse = 'Here are some ingredients to customize your dish:';fetchIngredients('both');return;} else if (lastMessage.includes('no') && selectedMenuItem) {addToCart(selectedMenuItem);35 unmodified lines});}function fetchMenuItems(category) {fetch('/get_menu_items', {method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify({ category: category })}).then(response => response.json()).then(data => {1 unmodified lineaddMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);} else {const menuItems = data.menu_items || [];addMessage('bot', 'Here are some dishes based on your selection:');displayMenuItems(menuItems);}})9 unmodified linesreturn;}let ingredientsList = document.querySelector('.ingredients-list');if (!ingredientsList) {ingredientsList = document.createElement('div');36 unmodified linesreturn;}let menuItemsList = document.querySelector('.menu-items-list');if (!menuItemsList) {menuItemsList = document.createElement('div');39 unmodified linesreturn;}let selectedArea = document.querySelector('.selected-ingredients');if (!selectedArea) {selectedArea = document.createElement('div');43 unmodified lines});}function fetchMenuItems(ingredientNames) {fetch('/get_menu_items', {method: 'POST',headers: {'Content-Type': 'application/json',},body: JSON.stringify({ ingredient_names: ingredientNames })}).then(response => response.json()).then(data => {if (data.error) {addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);} else {const menuItems = data.menu_items || [];addMessage('bot', 'Here are some dishes based on your selected ingredients:');displayMenuItems(menuItems);}}).catch(error => {addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);});}function displayCustomizationInput() {const chatMessages = document.getElementById('chatMessages');if (!chatMessages) {1 unmodified linereturn;}let customizationArea = document.query|:querySelector('.customization-input');if (!customizationArea) {customizationArea = document.createElement('div');customizationArea.className = 'customization-input';34 unmodified linesconsole.error('Chat messages container not found for options!');return;}options.forEach(opt => {const button = document.createElement('button');button.textContent = opt.text;6 unmodified lineschatMessages.appendChild(button);});chatMessages.appendChild(document.createElement('br'));if (conversation.length > 1) { // Ensure back button appears only when there's a previous stepconst backButton = document.createElement('button');backButton.textContent = 'Go Back';backButton.className = 'option-button';backButton.onclick = () => {if (conversation.length > 1) {conversation.pop(); // Remove the last conversation entryselectedIngredients = []; // Clear selected ingredients if going backselectedMenuItem = null; // Clear selected menu itemchatMessages.innerHTML = ''; // Clear current messagesconversation.forEach(msg => addMessage(msg.role, msg.message)); // Re-render conversation// Trigger response based on the now-last messageif (conversation.length > 0) {setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);}}};chatMessages.appendChild(backButton);}}document.getElementById('userInput').addEventListener('keypress', function(e) {
static/script.js+47-54