Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +24 -23
static/script.js
CHANGED
@@ -45,27 +45,20 @@ function handleResponse(userInput) {
|
|
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') || lastMessage.includes('non-vegetarian')) {
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
|
55 |
-
fetchIngredients('vegetable');
|
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! 🍽️ Here are some non-vegetarian ingredients:';
|
60 |
-
fetchIngredients('non-vegetarian');
|
61 |
-
}
|
62 |
return;
|
63 |
} else if (selectedIngredients.length > 0) {
|
64 |
fetchMenuItemsByIngredients();
|
65 |
return;
|
66 |
} else if (lastMessage.includes('yes') && selectedMenuItem) {
|
67 |
botResponse = 'Here are some ingredients to customize your dish:';
|
68 |
-
fetchIngredients('
|
69 |
return;
|
70 |
} else if (lastMessage.includes('no') && selectedMenuItem) {
|
71 |
addToCart(selectedMenuItem);
|
@@ -83,13 +76,13 @@ function handleResponse(userInput) {
|
|
83 |
}
|
84 |
}
|
85 |
|
86 |
-
function fetchIngredients(
|
87 |
fetch('/get_ingredients', {
|
88 |
method: 'POST',
|
89 |
headers: {
|
90 |
'Content-Type': 'application/json',
|
91 |
},
|
92 |
-
body: JSON.stringify({ dietary_preference:
|
93 |
})
|
94 |
.then(response => response.json())
|
95 |
.then(data => {
|
@@ -97,9 +90,10 @@ function fetchIngredients(foodPreference) {
|
|
97 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
98 |
} else {
|
99 |
const ingredients = data.ingredients || [];
|
100 |
-
addMessage('bot', '
|
101 |
displayIngredientsList(ingredients);
|
102 |
displaySelectedIngredients();
|
|
|
103 |
}
|
104 |
})
|
105 |
.catch(error => {
|
@@ -303,6 +297,8 @@ function displayOptions(options) {
|
|
303 |
console.error('Chat messages container not found for options!');
|
304 |
return;
|
305 |
}
|
|
|
|
|
306 |
options.forEach(opt => {
|
307 |
const button = document.createElement('button');
|
308 |
button.textContent = opt.text;
|
@@ -310,9 +306,7 @@ function displayOptions(options) {
|
|
310 |
button.onclick = () => {
|
311 |
addMessage('user', opt.text);
|
312 |
conversation.push({ role: 'user', message: opt.text });
|
313 |
-
|
314 |
-
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
315 |
-
setTimeout(() => handleResponse(opt.text), 500);
|
316 |
};
|
317 |
chatMessages.appendChild(button);
|
318 |
});
|
@@ -321,20 +315,27 @@ function displayOptions(options) {
|
|
321 |
backButton.textContent = 'Go Back';
|
322 |
backButton.className = 'option-button';
|
323 |
backButton.onclick = () => {
|
324 |
-
conversation
|
|
|
|
|
325 |
selectedIngredients = [];
|
326 |
-
selectedMenuItem = null;
|
327 |
chatMessages.innerHTML = '';
|
328 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
329 |
-
|
|
|
|
|
|
|
|
|
330 |
};
|
331 |
chatMessages.appendChild(backButton);
|
332 |
}
|
333 |
|
|
|
334 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
335 |
if (e.key === 'Enter') {
|
336 |
sendMessage();
|
337 |
}
|
338 |
});
|
339 |
|
|
|
340 |
console.log('Script loaded successfully');
|
|
|
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 |
+
{ text: 'Both', class: 'gray' }
|
50 |
];
|
51 |
+
} else if (lastMessage.includes('vegetarian') || lastMessage.includes('non-vegetarian') || lastMessage.includes('both')) {
|
52 |
+
let dietaryPreference = lastMessage;
|
53 |
+
botResponse = `Great choice! 🍽️ These are the available ingredients for ${lastMessage}:`;
|
54 |
+
fetchIngredients(dietaryPreference);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
return;
|
56 |
} else if (selectedIngredients.length > 0) {
|
57 |
fetchMenuItemsByIngredients();
|
58 |
return;
|
59 |
} else if (lastMessage.includes('yes') && selectedMenuItem) {
|
60 |
botResponse = 'Here are some ingredients to customize your dish:';
|
61 |
+
fetchIngredients('both'); // Allow customization from all ingredients
|
62 |
return;
|
63 |
} else if (lastMessage.includes('no') && selectedMenuItem) {
|
64 |
addToCart(selectedMenuItem);
|
|
|
76 |
}
|
77 |
}
|
78 |
|
79 |
+
function fetchIngredients(dietaryPreference) {
|
80 |
fetch('/get_ingredients', {
|
81 |
method: 'POST',
|
82 |
headers: {
|
83 |
'Content-Type': 'application/json',
|
84 |
},
|
85 |
+
body: JSON.stringify({ dietary_preference: dietaryPreference.toLowerCase() })
|
86 |
})
|
87 |
.then(response => response.json())
|
88 |
.then(data => {
|
|
|
90 |
addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
|
91 |
} else {
|
92 |
const ingredients = data.ingredients || [];
|
93 |
+
addMessage('bot', 'Great choice! These are available ingredients:');
|
94 |
displayIngredientsList(ingredients);
|
95 |
displaySelectedIngredients();
|
96 |
+
console.log(`Ingredients fetched for ${dietaryPreference}:`, ingredients);
|
97 |
}
|
98 |
})
|
99 |
.catch(error => {
|
|
|
297 |
console.error('Chat messages container not found for options!');
|
298 |
return;
|
299 |
}
|
300 |
+
// Clear existing content
|
301 |
+
chatMessages.innerHTML = '';
|
302 |
options.forEach(opt => {
|
303 |
const button = document.createElement('button');
|
304 |
button.textContent = opt.text;
|
|
|
306 |
button.onclick = () => {
|
307 |
addMessage('user', opt.text);
|
308 |
conversation.push({ role: 'user', message: opt.text });
|
309 |
+
handleResponse(opt.text); // Process the selection immediately
|
|
|
|
|
310 |
};
|
311 |
chatMessages.appendChild(button);
|
312 |
});
|
|
|
315 |
backButton.textContent = 'Go Back';
|
316 |
backButton.className = 'option-button';
|
317 |
backButton.onclick = () => {
|
318 |
+
// Reset conversation to initial state
|
319 |
+
const userName = conversation.length > 1 ? conversation[1].message : 'User';
|
320 |
+
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?` }];
|
321 |
selectedIngredients = [];
|
|
|
322 |
chatMessages.innerHTML = '';
|
323 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
324 |
+
displayOptions([
|
325 |
+
{ text: 'Vegetarian', class: 'green' },
|
326 |
+
{ text: 'Non-Vegetarian', class: 'red' },
|
327 |
+
{ text: 'Both', class: 'gray' }
|
328 |
+
]);
|
329 |
};
|
330 |
chatMessages.appendChild(backButton);
|
331 |
}
|
332 |
|
333 |
+
// Add event listener for Enter key
|
334 |
document.getElementById('userInput').addEventListener('keypress', function(e) {
|
335 |
if (e.key === 'Enter') {
|
336 |
sendMessage();
|
337 |
}
|
338 |
});
|
339 |
|
340 |
+
// Initial load check
|
341 |
console.log('Script loaded successfully');
|