Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +16 -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,16 @@ function displayOptions(options) {
|
|
173 |
backButton.textContent = 'Go Back';
|
174 |
backButton.className = 'option-button';
|
175 |
backButton.onclick = () => {
|
176 |
-
conversation
|
177 |
selectedIngredients = []; // Clear selected ingredients
|
178 |
-
chatMessages.innerHTML = ''; // Clear
|
179 |
-
conversation.
|
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 options and messages
|
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); // Directly handle response without clearing
|
|
|
|
|
167 |
};
|
168 |
chatMessages.appendChild(button);
|
169 |
});
|
|
|
172 |
backButton.textContent = 'Go Back';
|
173 |
backButton.className = 'option-button';
|
174 |
backButton.onclick = () => {
|
175 |
+
conversation = [{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }]; // Reset conversation
|
176 |
selectedIngredients = []; // Clear selected ingredients
|
177 |
+
chatMessages.innerHTML = ''; // Clear messages
|
178 |
+
const userName = conversation[0].message.split('!')[0].split('name?')[0].trim().split(' ')[4]; // Extract name
|
179 |
+
addMessage('bot', `Nice to meet you, ${userName}! 😊 Let's create your perfect meal! What type of food would you prefer?`);
|
180 |
+
displayOptions([
|
181 |
+
{ text: 'Vegetarian', class: 'green' },
|
182 |
+
{ text: 'Non-Vegetarian', class: 'red' },
|
183 |
+
{ text: 'Both', class: 'gray' }
|
184 |
+
]);
|
185 |
};
|
186 |
chatMessages.appendChild(backButton);
|
187 |
}
|