Spaces:
Build error
Build error
Update static/script.js
Browse files- static/script.js +188 -23
static/script.js
CHANGED
|
@@ -1,7 +1,9 @@
|
|
| 1 |
let conversation = [
|
| 2 |
-
{ role: 'bot', message:
|
| 3 |
];
|
| 4 |
let selectedIngredients = [];
|
|
|
|
|
|
|
| 5 |
|
| 6 |
function addMessage(role, message) {
|
| 7 |
const chatMessages = document.getElementById('chatMessages');
|
|
@@ -43,13 +45,41 @@ function handleResponse(userInput) {
|
|
| 43 |
botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
|
| 44 |
options = [
|
| 45 |
{ text: 'Vegetarian', class: 'green' },
|
| 46 |
-
{ text: 'Non-Vegetarian', class: 'red' }
|
| 47 |
-
{ text: 'Both', class: 'gray' }
|
| 48 |
];
|
| 49 |
-
} else if (lastMessage.includes('vegetarian')
|
| 50 |
-
|
| 51 |
-
|
|
|
|
|
|
|
| 52 |
return;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
addMessage('bot', botResponse);
|
|
@@ -58,13 +88,13 @@ function handleResponse(userInput) {
|
|
| 58 |
}
|
| 59 |
}
|
| 60 |
|
| 61 |
-
function fetchIngredients(
|
| 62 |
fetch('/get_ingredients', {
|
| 63 |
method: 'POST',
|
| 64 |
headers: {
|
| 65 |
'Content-Type': 'application/json',
|
| 66 |
},
|
| 67 |
-
body: JSON.stringify({ dietary_preference:
|
| 68 |
})
|
| 69 |
.then(response => response.json())
|
| 70 |
.then(data => {
|
|
@@ -72,10 +102,9 @@ function fetchIngredients(dietaryPreference) {
|
|
| 72 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
| 73 |
} else {
|
| 74 |
const ingredients = data.ingredients || [];
|
| 75 |
-
addMessage('bot', '
|
| 76 |
displayIngredientsList(ingredients);
|
| 77 |
displaySelectedIngredients();
|
| 78 |
-
console.log(`Ingredients fetched for ${dietaryPreference}:`, ingredients);
|
| 79 |
}
|
| 80 |
})
|
| 81 |
.catch(error => {
|
|
@@ -83,6 +112,29 @@ function fetchIngredients(dietaryPreference) {
|
|
| 83 |
});
|
| 84 |
}
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
function displayIngredientsList(ingredients) {
|
| 87 |
const chatMessages = document.getElementById('chatMessages');
|
| 88 |
if (!chatMessages) {
|
|
@@ -125,6 +177,51 @@ function displayIngredientsList(ingredients) {
|
|
| 125 |
});
|
| 126 |
}
|
| 127 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
function displaySelectedIngredients() {
|
| 129 |
const chatMessages = document.getElementById('chatMessages');
|
| 130 |
if (!chatMessages) {
|
|
@@ -146,6 +243,81 @@ function displaySelectedIngredients() {
|
|
| 146 |
div.textContent = ingredient.name;
|
| 147 |
selectedArea.appendChild(div);
|
| 148 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
}
|
| 150 |
|
| 151 |
function displayOptions(options) {
|
|
@@ -154,8 +326,6 @@ function displayOptions(options) {
|
|
| 154 |
console.error('Chat messages container not found for options!');
|
| 155 |
return;
|
| 156 |
}
|
| 157 |
-
// Clear existing content
|
| 158 |
-
chatMessages.innerHTML = '';
|
| 159 |
options.forEach(opt => {
|
| 160 |
const button = document.createElement('button');
|
| 161 |
button.textContent = opt.text;
|
|
@@ -163,7 +333,9 @@ function displayOptions(options) {
|
|
| 163 |
button.onclick = () => {
|
| 164 |
addMessage('user', opt.text);
|
| 165 |
conversation.push({ role: 'user', message: opt.text });
|
| 166 |
-
|
|
|
|
|
|
|
| 167 |
};
|
| 168 |
chatMessages.appendChild(button);
|
| 169 |
});
|
|
@@ -172,27 +344,20 @@ function displayOptions(options) {
|
|
| 172 |
backButton.textContent = 'Go Back';
|
| 173 |
backButton.className = 'option-button';
|
| 174 |
backButton.onclick = () => {
|
| 175 |
-
|
| 176 |
-
const userName = conversation.length > 1 ? conversation[1].message : 'User';
|
| 177 |
-
conversation = [{ role: 'bot', message: `Hi there! I'm Chef Bot! May I know your name?` }, { role: 'user', message: userName }, { role: 'bot', message: `Nice to meet you, ${userName}! 😊 Let's create your perfect meal! What type of food would you prefer?` }];
|
| 178 |
selectedIngredients = [];
|
|
|
|
| 179 |
chatMessages.innerHTML = '';
|
| 180 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 181 |
-
|
| 182 |
-
{ text: 'Vegetarian', class: 'green' },
|
| 183 |
-
{ text: 'Non-Vegetarian', class: 'red' },
|
| 184 |
-
{ text: 'Both', class: 'gray' }
|
| 185 |
-
]);
|
| 186 |
};
|
| 187 |
chatMessages.appendChild(backButton);
|
| 188 |
}
|
| 189 |
|
| 190 |
-
// Add event listener for Enter key
|
| 191 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
| 192 |
if (e.key === 'Enter') {
|
| 193 |
sendMessage();
|
| 194 |
}
|
| 195 |
});
|
| 196 |
|
| 197 |
-
// Initial load check
|
| 198 |
console.log('Script loaded successfully');
|
|
|
|
| 1 |
let conversation = [
|
| 2 |
+
{ role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" }
|
| 3 |
];
|
| 4 |
let selectedIngredients = [];
|
| 5 |
+
let selectedMenuItem = null;
|
| 6 |
+
let cart = [];
|
| 7 |
|
| 8 |
function addMessage(role, message) {
|
| 9 |
const chatMessages = document.getElementById('chatMessages');
|
|
|
|
| 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('vegetarian')) {
|
| 51 |
+
conversation.push({ role: 'user', message: 'Vegetarian' });
|
| 52 |
+
console.log("Food preference selected: Vegetarian");
|
| 53 |
+
botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
|
| 54 |
+
fetchIngredients('vegetable');
|
| 55 |
return;
|
| 56 |
+
} else if (lastMessage.includes('non-vegetarian')) {
|
| 57 |
+
conversation.push({ role: 'user', message: 'Non-Vegetarian' });
|
| 58 |
+
console.log("Food preference selected: Non-Vegetarian");
|
| 59 |
+
botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
|
| 60 |
+
options = [
|
| 61 |
+
{ text: 'Chicken', class: '' },
|
| 62 |
+
{ text: 'Beef', class: '' },
|
| 63 |
+
{ text: 'Lamb', class: '' }
|
| 64 |
+
];
|
| 65 |
+
} else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
|
| 66 |
+
conversation.push({ role: 'user', message: lastMessage });
|
| 67 |
+
console.log(`Non-veg option selected: ${lastMessage}`);
|
| 68 |
+
botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
|
| 69 |
+
fetchIngredients(lastMessage.toLowerCase());
|
| 70 |
+
return;
|
| 71 |
+
} else if (lastMessage.includes('yes') && selectedMenuItem) {
|
| 72 |
+
botResponse = 'Here are some ingredients to customize your dish:';
|
| 73 |
+
fetchIngredients('non-vegetarian'); // Fetch non-veg ingredients for customization
|
| 74 |
+
return;
|
| 75 |
+
} else if (lastMessage.includes('no') && selectedMenuItem) {
|
| 76 |
+
addToCart(selectedMenuItem);
|
| 77 |
+
botResponse = 'Item added to cart! Would you like to order more?';
|
| 78 |
+
options = [
|
| 79 |
+
{ text: 'Yes', class: 'green' },
|
| 80 |
+
{ text: 'No', class: 'red' }
|
| 81 |
+
];
|
| 82 |
+
selectedMenuItem = null;
|
| 83 |
}
|
| 84 |
|
| 85 |
addMessage('bot', botResponse);
|
|
|
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
+
function fetchIngredients(foodPreference) {
|
| 92 |
fetch('/get_ingredients', {
|
| 93 |
method: 'POST',
|
| 94 |
headers: {
|
| 95 |
'Content-Type': 'application/json',
|
| 96 |
},
|
| 97 |
+
body: JSON.stringify({ dietary_preference: foodPreference })
|
| 98 |
})
|
| 99 |
.then(response => response.json())
|
| 100 |
.then(data => {
|
|
|
|
| 102 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
| 103 |
} else {
|
| 104 |
const ingredients = data.ingredients || [];
|
| 105 |
+
addMessage('bot', 'Please select ingredients:');
|
| 106 |
displayIngredientsList(ingredients);
|
| 107 |
displaySelectedIngredients();
|
|
|
|
| 108 |
}
|
| 109 |
})
|
| 110 |
.catch(error => {
|
|
|
|
| 112 |
});
|
| 113 |
}
|
| 114 |
|
| 115 |
+
function fetchMenuItems(category) {
|
| 116 |
+
fetch('/get_menu_items', {
|
| 117 |
+
method: 'POST',
|
| 118 |
+
headers: {
|
| 119 |
+
'Content-Type': 'application/json',
|
| 120 |
+
},
|
| 121 |
+
body: JSON.stringify({ category: category })
|
| 122 |
+
})
|
| 123 |
+
.then(response => response.json())
|
| 124 |
+
.then(data => {
|
| 125 |
+
if (data.error) {
|
| 126 |
+
addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
|
| 127 |
+
} else {
|
| 128 |
+
const menuItems = data.menu_items || [];
|
| 129 |
+
addMessage('bot', 'Here are some dishes based on your selection:');
|
| 130 |
+
displayMenuItems(menuItems);
|
| 131 |
+
}
|
| 132 |
+
})
|
| 133 |
+
.catch(error => {
|
| 134 |
+
addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
|
| 135 |
+
});
|
| 136 |
+
}
|
| 137 |
+
|
| 138 |
function displayIngredientsList(ingredients) {
|
| 139 |
const chatMessages = document.getElementById('chatMessages');
|
| 140 |
if (!chatMessages) {
|
|
|
|
| 177 |
});
|
| 178 |
}
|
| 179 |
|
| 180 |
+
function displayMenuItems(menuItems) {
|
| 181 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 182 |
+
if (!chatMessages) {
|
| 183 |
+
console.error('Chat messages container not found for menu items!');
|
| 184 |
+
return;
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
let menuItemsList = document.querySelector('.menu-items-list');
|
| 188 |
+
if (!menuItemsList) {
|
| 189 |
+
menuItemsList = document.createElement('div');
|
| 190 |
+
menuItemsList.className = 'menu-items-list';
|
| 191 |
+
chatMessages.appendChild(menuItemsList);
|
| 192 |
+
} else {
|
| 193 |
+
menuItemsList.innerHTML = '';
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
menuItems.forEach(item => {
|
| 197 |
+
const menuItem = document.createElement('div');
|
| 198 |
+
menuItem.className = 'menu-item';
|
| 199 |
+
const img = document.createElement('img');
|
| 200 |
+
img.src = item.image_url || 'https://via.placeholder.com/80';
|
| 201 |
+
img.alt = item.name;
|
| 202 |
+
const name = document.createElement('div');
|
| 203 |
+
name.textContent = item.name;
|
| 204 |
+
name.style.textAlign = 'center';
|
| 205 |
+
name.style.marginTop = '5px';
|
| 206 |
+
name.style.fontSize = '12px';
|
| 207 |
+
const button = document.createElement('button');
|
| 208 |
+
button.textContent = 'Add to Cart';
|
| 209 |
+
button.onclick = () => {
|
| 210 |
+
selectedMenuItem = item;
|
| 211 |
+
addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
|
| 212 |
+
const options = [
|
| 213 |
+
{ text: 'Yes', class: 'green' },
|
| 214 |
+
{ text: 'No', class: 'red' }
|
| 215 |
+
];
|
| 216 |
+
displayOptions(options);
|
| 217 |
+
};
|
| 218 |
+
menuItem.appendChild(img);
|
| 219 |
+
menuItem.appendChild(name);
|
| 220 |
+
menuItem.appendChild(button);
|
| 221 |
+
menuItemsList.appendChild(menuItem);
|
| 222 |
+
});
|
| 223 |
+
}
|
| 224 |
+
|
| 225 |
function displaySelectedIngredients() {
|
| 226 |
const chatMessages = document.getElementById('chatMessages');
|
| 227 |
if (!chatMessages) {
|
|
|
|
| 243 |
div.textContent = ingredient.name;
|
| 244 |
selectedArea.appendChild(div);
|
| 245 |
});
|
| 246 |
+
|
| 247 |
+
if (selectedIngredients.length > 0) {
|
| 248 |
+
let submitButton = document.querySelector('.submit-button');
|
| 249 |
+
if (!submitButton) {
|
| 250 |
+
submitButton = document.createElement('button');
|
| 251 |
+
submitButton.className = 'submit-button';
|
| 252 |
+
submitButton.textContent = 'Submit Ingredients';
|
| 253 |
+
submitButton.onclick = submitIngredients;
|
| 254 |
+
chatMessages.appendChild(submitButton);
|
| 255 |
+
}
|
| 256 |
+
}
|
| 257 |
+
}
|
| 258 |
+
|
| 259 |
+
function submitIngredients() {
|
| 260 |
+
fetch('/submit_ingredients', {
|
| 261 |
+
method: 'POST',
|
| 262 |
+
headers: {
|
| 263 |
+
'Content-Type': 'application/json',
|
| 264 |
+
},
|
| 265 |
+
body: JSON.stringify({ ingredients: selectedIngredients })
|
| 266 |
+
})
|
| 267 |
+
.then(response => response.json())
|
| 268 |
+
.then(data => {
|
| 269 |
+
if (data.success) {
|
| 270 |
+
addMessage('bot', 'Great choice! Would you like to add any special instructions for your dish?');
|
| 271 |
+
displayCustomizationInput();
|
| 272 |
+
} else {
|
| 273 |
+
addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
|
| 274 |
+
}
|
| 275 |
+
})
|
| 276 |
+
.catch(error => {
|
| 277 |
+
addMessage('bot', `Error: ${error.message}`);
|
| 278 |
+
});
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
function displayCustomizationInput() {
|
| 282 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 283 |
+
if (!chatMessages) {
|
| 284 |
+
console.error('Chat messages container not found for customization input!');
|
| 285 |
+
return;
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
let customizationArea = document.querySelector('.customization-input');
|
| 289 |
+
if (!customizationArea) {
|
| 290 |
+
customizationArea = document.createElement('div');
|
| 291 |
+
customizationArea.className = 'customization-input';
|
| 292 |
+
const textarea = document.createElement('textarea');
|
| 293 |
+
textarea.placeholder = 'Enter any special instructions...';
|
| 294 |
+
const submitButton = document.createElement('button');
|
| 295 |
+
submitButton.textContent = 'Submit Instructions';
|
| 296 |
+
submitButton.onclick = () => {
|
| 297 |
+
const instructions = textarea.value.trim();
|
| 298 |
+
if (instructions) {
|
| 299 |
+
addMessage('user', instructions);
|
| 300 |
+
conversation.push({ role: 'user', message: instructions });
|
| 301 |
+
}
|
| 302 |
+
addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
|
| 303 |
+
addMessage('bot', 'Item added to cart! Would you like to order more?');
|
| 304 |
+
const options = [
|
| 305 |
+
{ text: 'Yes', class: 'green' },
|
| 306 |
+
{ text: 'No', class: 'red' }
|
| 307 |
+
];
|
| 308 |
+
displayOptions(options);
|
| 309 |
+
selectedMenuItem = null;
|
| 310 |
+
selectedIngredients = [];
|
| 311 |
+
};
|
| 312 |
+
customizationArea.appendChild(textarea);
|
| 313 |
+
customizationArea.appendChild(submitButton);
|
| 314 |
+
chatMessages.appendChild(customizationArea);
|
| 315 |
+
}
|
| 316 |
+
}
|
| 317 |
+
|
| 318 |
+
function addToCart(item) {
|
| 319 |
+
cart.push(item);
|
| 320 |
+
console.log('Cart:', cart);
|
| 321 |
}
|
| 322 |
|
| 323 |
function displayOptions(options) {
|
|
|
|
| 326 |
console.error('Chat messages container not found for options!');
|
| 327 |
return;
|
| 328 |
}
|
|
|
|
|
|
|
| 329 |
options.forEach(opt => {
|
| 330 |
const button = document.createElement('button');
|
| 331 |
button.textContent = opt.text;
|
|
|
|
| 333 |
button.onclick = () => {
|
| 334 |
addMessage('user', opt.text);
|
| 335 |
conversation.push({ role: 'user', message: opt.text });
|
| 336 |
+
chatMessages.innerHTML = '';
|
| 337 |
+
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 338 |
+
setTimeout(() => handleResponse(opt.text), 500);
|
| 339 |
};
|
| 340 |
chatMessages.appendChild(button);
|
| 341 |
});
|
|
|
|
| 344 |
backButton.textContent = 'Go Back';
|
| 345 |
backButton.className = 'option-button';
|
| 346 |
backButton.onclick = () => {
|
| 347 |
+
conversation.pop();
|
|
|
|
|
|
|
| 348 |
selectedIngredients = [];
|
| 349 |
+
selectedMenuItem = null;
|
| 350 |
chatMessages.innerHTML = '';
|
| 351 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 352 |
+
setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
|
|
|
|
|
|
|
|
|
|
|
|
|
| 353 |
};
|
| 354 |
chatMessages.appendChild(backButton);
|
| 355 |
}
|
| 356 |
|
|
|
|
| 357 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
| 358 |
if (e.key === 'Enter') {
|
| 359 |
sendMessage();
|
| 360 |
}
|
| 361 |
});
|
| 362 |
|
|
|
|
| 363 |
console.log('Script loaded successfully');
|