Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +38 -4
static/script.js
CHANGED
|
@@ -111,13 +111,13 @@ function fetchIngredients(foodPreference) {
|
|
| 111 |
});
|
| 112 |
}
|
| 113 |
|
| 114 |
-
function fetchMenuItems(
|
| 115 |
fetch('/get_menu_items', {
|
| 116 |
method: 'POST',
|
| 117 |
headers: {
|
| 118 |
'Content-Type': 'application/json',
|
| 119 |
},
|
| 120 |
-
body: JSON.stringify({
|
| 121 |
})
|
| 122 |
.then(response => response.json())
|
| 123 |
.then(data => {
|
|
@@ -125,8 +125,19 @@ function fetchMenuItems(category) {
|
|
| 125 |
addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
|
| 126 |
} else {
|
| 127 |
const menuItems = data.menu_items || [];
|
| 128 |
-
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
}
|
| 131 |
})
|
| 132 |
.catch(error => {
|
|
@@ -134,6 +145,29 @@ function fetchMenuItems(category) {
|
|
| 134 |
});
|
| 135 |
}
|
| 136 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
function displayIngredientsList(ingredients) {
|
| 138 |
const chatMessages = document.getElementById('chatMessages');
|
| 139 |
if (!chatMessages) {
|
|
|
|
| 111 |
});
|
| 112 |
}
|
| 113 |
|
| 114 |
+
function fetchMenuItems(ingredient) {
|
| 115 |
fetch('/get_menu_items', {
|
| 116 |
method: 'POST',
|
| 117 |
headers: {
|
| 118 |
'Content-Type': 'application/json',
|
| 119 |
},
|
| 120 |
+
body: JSON.stringify({ ingredient: ingredient })
|
| 121 |
})
|
| 122 |
.then(response => response.json())
|
| 123 |
.then(data => {
|
|
|
|
| 125 |
addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
|
| 126 |
} else {
|
| 127 |
const menuItems = data.menu_items || [];
|
| 128 |
+
if (menuItems.length > 0) {
|
| 129 |
+
let menuItemNames = menuItems.map(item => item.name).join(", ");
|
| 130 |
+
addMessage('bot', `Here are some items with ${ingredient}: ${menuItemNames}. Please select one:`);
|
| 131 |
+
|
| 132 |
+
// Create buttons for each menu item to select
|
| 133 |
+
const options = menuItems.map(item => ({
|
| 134 |
+
text: item.name,
|
| 135 |
+
class: 'blue',
|
| 136 |
+
}));
|
| 137 |
+
displayOptions(options);
|
| 138 |
+
} else {
|
| 139 |
+
addMessage('bot', `Sorry, no items found with ${ingredient}.`);
|
| 140 |
+
}
|
| 141 |
}
|
| 142 |
})
|
| 143 |
.catch(error => {
|
|
|
|
| 145 |
});
|
| 146 |
}
|
| 147 |
|
| 148 |
+
function displayOptions(options) {
|
| 149 |
+
const chatMessages = document.getElementById('chatMessages');
|
| 150 |
+
if (!chatMessages) {
|
| 151 |
+
console.error('Chat messages container not found for options!');
|
| 152 |
+
return;
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
options.forEach(opt => {
|
| 156 |
+
const button = document.createElement('button');
|
| 157 |
+
button.textContent = opt.text;
|
| 158 |
+
button.className = `option-button ${opt.class}`;
|
| 159 |
+
button.onclick = () => {
|
| 160 |
+
addMessage('user', opt.text);
|
| 161 |
+
conversation.push({ role: 'user', message: opt.text });
|
| 162 |
+
chatMessages.innerHTML = '';
|
| 163 |
+
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
| 164 |
+
setTimeout(() => handleResponse(opt.text), 500);
|
| 165 |
+
};
|
| 166 |
+
chatMessages.appendChild(button);
|
| 167 |
+
});
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
|
| 171 |
function displayIngredientsList(ingredients) {
|
| 172 |
const chatMessages = document.getElementById('chatMessages');
|
| 173 |
if (!chatMessages) {
|