Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +17 -12
static/script.js
CHANGED
@@ -47,8 +47,8 @@ function handleResponse(userInput) {
|
|
47 |
{ text: 'Both', class: 'gray' }
|
48 |
];
|
49 |
} else if (lastMessage.includes('vegetarian') || lastMessage.includes('non-vegetarian') || lastMessage.includes('both')) {
|
50 |
-
botResponse = `Great choice! 🍽️ These are the available ingredients for ${lastMessage}
|
51 |
-
fetchIngredients(lastMessage
|
52 |
return;
|
53 |
}
|
54 |
|
@@ -75,7 +75,6 @@ function fetchIngredients(dietaryPreference) {
|
|
75 |
addMessage('bot', 'Great choice! These are available ingredients:');
|
76 |
displayIngredientsList(ingredients);
|
77 |
displaySelectedIngredients();
|
78 |
-
// Log ingredients to console for debugging
|
79 |
console.log(`Ingredients fetched for ${dietaryPreference}:`, ingredients);
|
80 |
}
|
81 |
})
|
@@ -107,7 +106,7 @@ function displayIngredientsList(ingredients) {
|
|
107 |
img.src = ingredient.image_url || 'https://via.placeholder.com/80';
|
108 |
img.alt = ingredient.name;
|
109 |
const name = document.createElement('div');
|
110 |
-
name.textContent = ingredient.name;
|
111 |
name.style.textAlign = 'center';
|
112 |
name.style.marginTop = '5px';
|
113 |
name.style.fontSize = '12px';
|
@@ -144,7 +143,7 @@ function displaySelectedIngredients() {
|
|
144 |
|
145 |
selectedIngredients.forEach(ingredient => {
|
146 |
const div = document.createElement('div');
|
147 |
-
div.textContent = ingredient.name;
|
148 |
selectedArea.appendChild(div);
|
149 |
});
|
150 |
}
|
@@ -155,6 +154,8 @@ function displayOptions(options) {
|
|
155 |
console.error('Chat messages container not found for options!');
|
156 |
return;
|
157 |
}
|
|
|
|
|
158 |
options.forEach(opt => {
|
159 |
const button = document.createElement('button');
|
160 |
button.textContent = opt.text;
|
@@ -162,9 +163,7 @@ function displayOptions(options) {
|
|
162 |
button.onclick = () => {
|
163 |
addMessage('user', opt.text);
|
164 |
conversation.push({ role: 'user', message: opt.text });
|
165 |
-
|
166 |
-
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
167 |
-
setTimeout(() => handleResponse(opt.text), 500);
|
168 |
};
|
169 |
chatMessages.appendChild(button);
|
170 |
});
|
@@ -173,11 +172,17 @@ function displayOptions(options) {
|
|
173 |
backButton.textContent = 'Go Back';
|
174 |
backButton.className = 'option-button';
|
175 |
backButton.onclick = () => {
|
176 |
-
|
177 |
-
|
178 |
-
|
|
|
|
|
179 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
180 |
-
|
|
|
|
|
|
|
|
|
181 |
};
|
182 |
chatMessages.appendChild(backButton);
|
183 |
}
|
|
|
47 |
{ text: 'Both', class: 'gray' }
|
48 |
];
|
49 |
} else if (lastMessage.includes('vegetarian') || lastMessage.includes('non-vegetarian') || lastMessage.includes('both')) {
|
50 |
+
botResponse = `Great choice! 🍽️ These are the available ingredients for ${lastMessage}:`;
|
51 |
+
fetchIngredients(lastMessage);
|
52 |
return;
|
53 |
}
|
54 |
|
|
|
75 |
addMessage('bot', 'Great choice! These are available ingredients:');
|
76 |
displayIngredientsList(ingredients);
|
77 |
displaySelectedIngredients();
|
|
|
78 |
console.log(`Ingredients fetched for ${dietaryPreference}:`, ingredients);
|
79 |
}
|
80 |
})
|
|
|
106 |
img.src = ingredient.image_url || 'https://via.placeholder.com/80';
|
107 |
img.alt = ingredient.name;
|
108 |
const name = document.createElement('div');
|
109 |
+
name.textContent = ingredient.name;
|
110 |
name.style.textAlign = 'center';
|
111 |
name.style.marginTop = '5px';
|
112 |
name.style.fontSize = '12px';
|
|
|
143 |
|
144 |
selectedIngredients.forEach(ingredient => {
|
145 |
const div = document.createElement('div');
|
146 |
+
div.textContent = ingredient.name;
|
147 |
selectedArea.appendChild(div);
|
148 |
});
|
149 |
}
|
|
|
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 |
button.onclick = () => {
|
164 |
addMessage('user', opt.text);
|
165 |
conversation.push({ role: 'user', message: opt.text });
|
166 |
+
handleResponse(opt.text); // Process the selection immediately
|
|
|
|
|
167 |
};
|
168 |
chatMessages.appendChild(button);
|
169 |
});
|
|
|
172 |
backButton.textContent = 'Go Back';
|
173 |
backButton.className = 'option-button';
|
174 |
backButton.onclick = () => {
|
175 |
+
// Reset conversation to initial state
|
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 |
+
displayOptions([
|
182 |
+
{ text: 'Vegetarian', class: 'green' },
|
183 |
+
{ text: 'Non-Vegetarian', class: 'red' },
|
184 |
+
{ text: 'Both', class: 'gray' }
|
185 |
+
]);
|
186 |
};
|
187 |
chatMessages.appendChild(backButton);
|
188 |
}
|