Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +43 -82
static/script.js
CHANGED
|
@@ -42,7 +42,7 @@ function handleResponse(userInput) {
|
|
| 42 |
let options = [];
|
| 43 |
|
| 44 |
if (conversation.length === 2) { // After name input
|
| 45 |
-
botResponse =
|
| 46 |
options = [
|
| 47 |
{ text: 'Vegetarian', class: 'green' },
|
| 48 |
{ text: 'Non-Vegetarian', class: 'red' }
|
|
@@ -63,8 +63,8 @@ function handleResponse(userInput) {
|
|
| 63 |
return;
|
| 64 |
} else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
|
| 65 |
conversation.push({ role: 'user', message: lastMessage });
|
| 66 |
-
console.log(
|
| 67 |
-
botResponse =
|
| 68 |
fetchIngredients(lastMessage.toLowerCase());
|
| 69 |
return;
|
| 70 |
} else if (lastMessage.includes('yes') && selectedMenuItem) {
|
|
@@ -87,96 +87,30 @@ function handleResponse(userInput) {
|
|
| 87 |
}
|
| 88 |
}
|
| 89 |
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
// Function to handle ingredient selection (example)
|
| 93 |
-
function selectIngredient(ingredient) {
|
| 94 |
-
selectedIngredient = ingredient; // Store the selected ingredient
|
| 95 |
-
console.log(`Selected ingredient: ${selectedIngredient}`);
|
| 96 |
-
// Fetch menu items related to the selected ingredient
|
| 97 |
-
fetchMenuItems(selectedIngredient);
|
| 98 |
-
}
|
| 99 |
-
|
| 100 |
-
function submitIngredients() {
|
| 101 |
-
fetch('/submit_ingredients', {
|
| 102 |
-
method: 'POST',
|
| 103 |
-
headers: {
|
| 104 |
-
'Content-Type': 'application/json',
|
| 105 |
-
},
|
| 106 |
-
body: JSON.stringify({ ingredients: selectedIngredients })
|
| 107 |
-
})
|
| 108 |
-
.then(response => response.json())
|
| 109 |
-
.then(data => {
|
| 110 |
-
if (data.success) {
|
| 111 |
-
addMessage('bot', 'Great choice! Now, let\'s see some menu items you can select.');
|
| 112 |
-
|
| 113 |
-
// Fetch the menu items dynamically based on the selected ingredient
|
| 114 |
-
fetchMenuItems(selectedIngredient); // Use dynamically passed selectedIngredient
|
| 115 |
-
|
| 116 |
-
} else {
|
| 117 |
-
addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
|
| 118 |
-
}
|
| 119 |
-
})
|
| 120 |
-
.catch(error => {
|
| 121 |
-
addMessage('bot', `Error: ${error.message}`);
|
| 122 |
-
});
|
| 123 |
-
}
|
| 124 |
-
|
| 125 |
-
function fetchMenuItems(ingredient) {
|
| 126 |
-
fetch('/get_menu_items', {
|
| 127 |
method: 'POST',
|
| 128 |
headers: {
|
| 129 |
'Content-Type': 'application/json',
|
| 130 |
},
|
| 131 |
-
body: JSON.stringify({
|
| 132 |
})
|
| 133 |
.then(response => response.json())
|
| 134 |
.then(data => {
|
| 135 |
if (data.error) {
|
| 136 |
-
addMessage('bot',
|
| 137 |
} else {
|
| 138 |
-
const
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
// Create buttons for each menu item to select
|
| 144 |
-
const options = menuItems.map(item => ({
|
| 145 |
-
text: item.name,
|
| 146 |
-
class: 'blue',
|
| 147 |
-
}));
|
| 148 |
-
displayOptions(options);
|
| 149 |
-
} else {
|
| 150 |
-
addMessage('bot', `Sorry, no items found with ${ingredient}.`);
|
| 151 |
-
}
|
| 152 |
}
|
| 153 |
})
|
| 154 |
.catch(error => {
|
| 155 |
-
addMessage('bot',
|
| 156 |
});
|
| 157 |
}
|
| 158 |
|
| 159 |
-
function displayOptions(options) {
|
| 160 |
-
const chatMessages = document.getElementById('chatMessages');
|
| 161 |
-
if (!chatMessages) {
|
| 162 |
-
console.error('Chat messages container not found for options!');
|
| 163 |
-
return;
|
| 164 |
-
}
|
| 165 |
-
|
| 166 |
-
options.forEach(opt => {
|
| 167 |
-
const button = document.createElement('button');
|
| 168 |
-
button.textContent = opt.text;
|
| 169 |
-
button.className = `option-button ${opt.class}`;
|
| 170 |
-
button.onclick = () => {
|
| 171 |
-
addMessage('user', opt.text);
|
| 172 |
-
conversation.push({ role: 'user', message: opt.text });
|
| 173 |
-
chatMessages.innerHTML = '';
|
| 174 |
-
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 175 |
-
setTimeout(() => handleResponse(opt.text), 500);
|
| 176 |
-
};
|
| 177 |
-
chatMessages.appendChild(button);
|
| 178 |
-
});
|
| 179 |
-
}
|
| 180 |
|
| 181 |
|
| 182 |
function displayIngredientsList(ingredients) {
|
|
@@ -252,7 +186,7 @@ function displayMenuItems(menuItems) {
|
|
| 252 |
button.textContent = 'Add to Cart';
|
| 253 |
button.onclick = () => {
|
| 254 |
selectedMenuItem = item;
|
| 255 |
-
addMessage('bot',
|
| 256 |
const options = [
|
| 257 |
{ text: 'Yes', class: 'green' },
|
| 258 |
{ text: 'No', class: 'red' }
|
|
@@ -311,8 +245,11 @@ function submitIngredients() {
|
|
| 311 |
.then(response => response.json())
|
| 312 |
.then(data => {
|
| 313 |
if (data.success) {
|
| 314 |
-
|
| 315 |
-
|
|
|
|
|
|
|
|
|
|
| 316 |
} else {
|
| 317 |
addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
|
| 318 |
}
|
|
@@ -322,6 +259,30 @@ function submitIngredients() {
|
|
| 322 |
});
|
| 323 |
}
|
| 324 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
function displayCustomizationInput() {
|
| 326 |
const chatMessages = document.getElementById('chatMessages');
|
| 327 |
if (!chatMessages) {
|
|
@@ -373,7 +334,7 @@ function displayOptions(options) {
|
|
| 373 |
options.forEach(opt => {
|
| 374 |
const button = document.createElement('button');
|
| 375 |
button.textContent = opt.text;
|
| 376 |
-
button.className =
|
| 377 |
button.onclick = () => {
|
| 378 |
addMessage('user', opt.text);
|
| 379 |
conversation.push({ role: 'user', message: opt.text });
|
|
|
|
| 42 |
let options = [];
|
| 43 |
|
| 44 |
if (conversation.length === 2) { // After name input
|
| 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' }
|
|
|
|
| 63 |
return;
|
| 64 |
} else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
|
| 65 |
conversation.push({ role: 'user', message: lastMessage });
|
| 66 |
+
console.log(Non-veg option selected: ${lastMessage});
|
| 67 |
+
botResponse = Great! Here are some ${lastMessage} ingredients available:;
|
| 68 |
fetchIngredients(lastMessage.toLowerCase());
|
| 69 |
return;
|
| 70 |
} else if (lastMessage.includes('yes') && selectedMenuItem) {
|
|
|
|
| 87 |
}
|
| 88 |
}
|
| 89 |
|
| 90 |
+
function fetchIngredients(foodPreference) {
|
| 91 |
+
fetch('/get_ingredients', {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
method: 'POST',
|
| 93 |
headers: {
|
| 94 |
'Content-Type': 'application/json',
|
| 95 |
},
|
| 96 |
+
body: JSON.stringify({ dietary_preference: foodPreference })
|
| 97 |
})
|
| 98 |
.then(response => response.json())
|
| 99 |
.then(data => {
|
| 100 |
if (data.error) {
|
| 101 |
+
addMessage('bot', Sorry, there was an error fetching ingredients: ${data.error});
|
| 102 |
} else {
|
| 103 |
+
const ingredients = data.ingredients || [];
|
| 104 |
+
addMessage('bot', 'Please select ingredients:');
|
| 105 |
+
displayIngredientsList(ingredients);
|
| 106 |
+
displaySelectedIngredients();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
}
|
| 108 |
})
|
| 109 |
.catch(error => {
|
| 110 |
+
addMessage('bot', Error: Unable to connect to the ingredient database. ${error.message});
|
| 111 |
});
|
| 112 |
}
|
| 113 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
|
| 115 |
|
| 116 |
function displayIngredientsList(ingredients) {
|
|
|
|
| 186 |
button.textContent = 'Add to Cart';
|
| 187 |
button.onclick = () => {
|
| 188 |
selectedMenuItem = item;
|
| 189 |
+
addMessage('bot', World-class selection! Would you like to customize your dish further?);
|
| 190 |
const options = [
|
| 191 |
{ text: 'Yes', class: 'green' },
|
| 192 |
{ text: 'No', class: 'red' }
|
|
|
|
| 245 |
.then(response => response.json())
|
| 246 |
.then(data => {
|
| 247 |
if (data.success) {
|
| 248 |
+
// Extract the ingredient names from the selectedIngredients array
|
| 249 |
+
const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
|
| 250 |
+
|
| 251 |
+
// Fetch menu items based on ingredient names
|
| 252 |
+
fetchMenuItems(ingredientNames);
|
| 253 |
} else {
|
| 254 |
addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
|
| 255 |
}
|
|
|
|
| 259 |
});
|
| 260 |
}
|
| 261 |
|
| 262 |
+
function fetchMenuItems(ingredientNames) {
|
| 263 |
+
fetch('/get_menu_items', {
|
| 264 |
+
method: 'POST',
|
| 265 |
+
headers: {
|
| 266 |
+
'Content-Type': 'application/json',
|
| 267 |
+
},
|
| 268 |
+
body: JSON.stringify({ ingredient_names: ingredientNames })
|
| 269 |
+
})
|
| 270 |
+
.then(response => response.json())
|
| 271 |
+
.then(data => {
|
| 272 |
+
if (data.error) {
|
| 273 |
+
addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
|
| 274 |
+
} else {
|
| 275 |
+
const menuItems = data.menu_items || [];
|
| 276 |
+
addMessage('bot', 'Here are some dishes based on your selected ingredients:');
|
| 277 |
+
displayMenuItems(menuItems);
|
| 278 |
+
}
|
| 279 |
+
})
|
| 280 |
+
.catch(error => {
|
| 281 |
+
addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
|
| 282 |
+
});
|
| 283 |
+
}
|
| 284 |
+
|
| 285 |
+
|
| 286 |
function displayCustomizationInput() {
|
| 287 |
const chatMessages = document.getElementById('chatMessages');
|
| 288 |
if (!chatMessages) {
|
|
|
|
| 334 |
options.forEach(opt => {
|
| 335 |
const button = document.createElement('button');
|
| 336 |
button.textContent = opt.text;
|
| 337 |
+
button.className = option-button ${opt.class};
|
| 338 |
button.onclick = () => {
|
| 339 |
addMessage('user', opt.text);
|
| 340 |
conversation.push({ role: 'user', message: opt.text });
|