Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +60 -5
static/script.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
let conversation = [
|
2 |
{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }
|
3 |
];
|
|
|
4 |
|
5 |
function addMessage(role, message) {
|
6 |
const chatMessages = document.getElementById('chatMessages');
|
@@ -92,11 +93,8 @@ function fetchIngredients(dietaryPreference) {
|
|
92 |
} else {
|
93 |
const ingredients = data.ingredients || [];
|
94 |
addMessage('bot', 'Great choice! These are available ingredients:');
|
95 |
-
|
96 |
-
|
97 |
-
} else {
|
98 |
-
addMessage('bot', '- No ingredients available.');
|
99 |
-
}
|
100 |
}
|
101 |
})
|
102 |
.catch(error => {
|
@@ -104,6 +102,62 @@ function fetchIngredients(dietaryPreference) {
|
|
104 |
});
|
105 |
}
|
106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
function displayOptions(options) {
|
108 |
const chatMessages = document.getElementById('chatMessages');
|
109 |
if (!chatMessages) {
|
@@ -130,6 +184,7 @@ function displayOptions(options) {
|
|
130 |
backButton.onclick = () => {
|
131 |
conversation.pop(); // Remove last user input
|
132 |
chatMessages.innerHTML = ''; // Clear previous messages
|
|
|
133 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
134 |
setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
|
135 |
};
|
|
|
1 |
let conversation = [
|
2 |
{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }
|
3 |
];
|
4 |
+
let selectedIngredients = [];
|
5 |
|
6 |
function addMessage(role, message) {
|
7 |
const chatMessages = document.getElementById('chatMessages');
|
|
|
93 |
} else {
|
94 |
const ingredients = data.ingredients || [];
|
95 |
addMessage('bot', 'Great choice! These are available ingredients:');
|
96 |
+
displayIngredientsList(ingredients);
|
97 |
+
displaySelectedIngredients();
|
|
|
|
|
|
|
98 |
}
|
99 |
})
|
100 |
.catch(error => {
|
|
|
102 |
});
|
103 |
}
|
104 |
|
105 |
+
function displayIngredientsList(ingredients) {
|
106 |
+
const chatMessages = document.getElementById('chatMessages');
|
107 |
+
if (!chatMessages) {
|
108 |
+
console.error('Chat messages container not found for ingredients!');
|
109 |
+
return;
|
110 |
+
}
|
111 |
+
|
112 |
+
// Create or update ingredients list
|
113 |
+
let ingredientsList = document.querySelector('.ingredients-list');
|
114 |
+
if (!ingredientsList) {
|
115 |
+
ingredientsList = document.createElement('div');
|
116 |
+
ingredientsList.className = 'ingredients-list';
|
117 |
+
chatMessages.appendChild(ingredientsList);
|
118 |
+
} else {
|
119 |
+
ingredientsList.innerHTML = '';
|
120 |
+
}
|
121 |
+
|
122 |
+
ingredients.forEach(ingredient => {
|
123 |
+
const button = document.createElement('button');
|
124 |
+
button.textContent = ingredient;
|
125 |
+
button.onclick = () => {
|
126 |
+
if (!selectedIngredients.includes(ingredient)) {
|
127 |
+
selectedIngredients.push(ingredient);
|
128 |
+
displaySelectedIngredients();
|
129 |
+
}
|
130 |
+
};
|
131 |
+
ingredientsList.appendChild(button);
|
132 |
+
});
|
133 |
+
}
|
134 |
+
|
135 |
+
function displaySelectedIngredients() {
|
136 |
+
const chatMessages = document.getElementById('chatMessages');
|
137 |
+
if (!chatMessages) {
|
138 |
+
console.error('Chat messages container not found for selected ingredients!');
|
139 |
+
return;
|
140 |
+
}
|
141 |
+
|
142 |
+
// Create or update selected ingredients area
|
143 |
+
let selectedArea = document.querySelector('.selected-ingredients');
|
144 |
+
if (!selectedArea) {
|
145 |
+
selectedArea = document.createElement('div');
|
146 |
+
selectedArea.className = 'selected-ingredients';
|
147 |
+
chatMessages.appendChild(selectedArea);
|
148 |
+
}
|
149 |
+
|
150 |
+
const textarea = selectedArea.querySelector('textarea') || document.createElement('textarea');
|
151 |
+
textarea.value = selectedIngredients.join('\n');
|
152 |
+
textarea.onchange = () => {
|
153 |
+
selectedIngredients = textarea.value.split('\n').filter(item => item.trim() !== '');
|
154 |
+
};
|
155 |
+
if (!selectedArea.contains(textarea)) {
|
156 |
+
selectedArea.innerHTML = '';
|
157 |
+
selectedArea.appendChild(textarea);
|
158 |
+
}
|
159 |
+
}
|
160 |
+
|
161 |
function displayOptions(options) {
|
162 |
const chatMessages = document.getElementById('chatMessages');
|
163 |
if (!chatMessages) {
|
|
|
184 |
backButton.onclick = () => {
|
185 |
conversation.pop(); // Remove last user input
|
186 |
chatMessages.innerHTML = ''; // Clear previous messages
|
187 |
+
selectedIngredients = []; // Clear selected ingredients
|
188 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
189 |
setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
|
190 |
};
|