Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +36 -81
static/script.js
CHANGED
@@ -31,7 +31,7 @@ function sendMessage() {
|
|
31 |
selectionBoxVisible = true;
|
32 |
handleResponse(message);
|
33 |
} else {
|
34 |
-
addMessage('bot', 'Please type a dish
|
35 |
}
|
36 |
userInput.value = '';
|
37 |
updateSelectionBox();
|
@@ -42,7 +42,7 @@ function handleResponse(userInput) {
|
|
42 |
let botResponse = '';
|
43 |
|
44 |
if (conversation.length === 2) {
|
45 |
-
botResponse = `Hi ${userInput}! 🍳 Search for a dish
|
46 |
displayOptions([
|
47 |
{ text: 'Vegetarian', class: 'green' },
|
48 |
{ text: 'Non-Vegetarian', class: 'red' },
|
@@ -50,12 +50,13 @@ function handleResponse(userInput) {
|
|
50 |
]);
|
51 |
addMessage('bot', botResponse);
|
52 |
} else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
|
53 |
-
botResponse = `
|
54 |
addMessage('bot', botResponse);
|
|
|
55 |
} else {
|
56 |
botResponse = `Looking for "${userInput}"...`;
|
57 |
addMessage('bot', botResponse);
|
58 |
-
fetchMenuItems(userInput);
|
59 |
}
|
60 |
}
|
61 |
|
@@ -108,6 +109,7 @@ function updateSelectionBox() {
|
|
108 |
selectedItems.forEach((item, index) => {
|
109 |
const itemContainer = document.createElement('div');
|
110 |
itemContainer.className = 'selected-item';
|
|
|
111 |
|
112 |
const img = document.createElement('img');
|
113 |
img.src = item.image_url || 'https://via.placeholder.com/30';
|
@@ -115,9 +117,22 @@ function updateSelectionBox() {
|
|
115 |
img.className = 'selected-item-image';
|
116 |
itemContainer.appendChild(img);
|
117 |
|
|
|
|
|
|
|
118 |
const itemSpan = document.createElement('span');
|
119 |
itemSpan.textContent = `${item.name} (Qty: ${item.quantity || 1})`;
|
120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
121 |
|
122 |
const removeButton = document.createElement('button');
|
123 |
removeButton.textContent = 'X';
|
@@ -172,11 +187,14 @@ function updateSelectionBox() {
|
|
172 |
console.log('Updated selection box:', selectedItems.map(item => ({ name: item.name, category: item.category })));
|
173 |
}
|
174 |
|
175 |
-
function fetchMenuItems(searchTerm) {
|
|
|
|
|
|
|
176 |
fetch('/get_menu_items', {
|
177 |
method: 'POST',
|
178 |
headers: { 'Content-Type': 'application/json' },
|
179 |
-
body: JSON.stringify(
|
180 |
})
|
181 |
.then(response => response.json())
|
182 |
.then(data => {
|
@@ -186,13 +204,13 @@ function fetchMenuItems(searchTerm) {
|
|
186 |
addMessage('bot', `--- Found ${data.menu_items.length} item${data.menu_items.length > 1 ? 's' : ''} ---`);
|
187 |
displayItemsList(data.menu_items);
|
188 |
} else {
|
189 |
-
addMessage('bot', `No matches for "${searchTerm}". Try "
|
190 |
}
|
191 |
-
console.log(`Fetched
|
192 |
})
|
193 |
.catch(error => {
|
194 |
addMessage('bot', `Connection issue: ${error.message}. Retrying...`);
|
195 |
-
setTimeout(() => fetchMenuItems(searchTerm), 2000);
|
196 |
});
|
197 |
}
|
198 |
|
@@ -205,15 +223,14 @@ function fetchSectorItemDetails(itemName) {
|
|
205 |
.then(response => response.json())
|
206 |
.then(data => {
|
207 |
if (data.error) {
|
208 |
-
addMessage('bot', `No "${itemName}" found
|
209 |
} else {
|
210 |
const details = data.item_details;
|
211 |
if (selectedItems.some(item => item.name === details.name)) {
|
212 |
addMessage('bot', `"${details.name}" already selected!`);
|
213 |
} else {
|
214 |
selectedItems.push({ ...details, quantity: 1 });
|
215 |
-
addMessage('bot',
|
216 |
-
displaySingleItem(details);
|
217 |
updateSelectionBox();
|
218 |
}
|
219 |
}
|
@@ -224,18 +241,18 @@ function fetchSectorItemDetails(itemName) {
|
|
224 |
});
|
225 |
}
|
226 |
|
227 |
-
function toggleDescription(
|
228 |
-
let descElement =
|
229 |
if (!descElement) {
|
230 |
descElement = document.createElement('p');
|
231 |
descElement.className = 'item-description';
|
232 |
descElement.textContent = description;
|
233 |
-
|
234 |
-
|
235 |
console.log(`Showed description for ${itemName}`);
|
236 |
} else {
|
237 |
descElement.remove();
|
238 |
-
|
239 |
console.log(`Hid description for ${itemName}`);
|
240 |
}
|
241 |
}
|
@@ -254,7 +271,6 @@ function displayItemsList(items) {
|
|
254 |
items.forEach(item => {
|
255 |
const itemDiv = document.createElement('div');
|
256 |
itemDiv.className = 'item-card';
|
257 |
-
itemDiv.dataset.hidden = 'false';
|
258 |
|
259 |
const img = document.createElement('img');
|
260 |
img.src = item.image_url || 'https://via.placeholder.com/60';
|
@@ -331,67 +347,6 @@ function displayItemsList(items) {
|
|
331 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
332 |
}
|
333 |
|
334 |
-
function displaySingleItem(item) {
|
335 |
-
const chatMessages = document.getElementById('chatMessages');
|
336 |
-
if (!chatMessages) {
|
337 |
-
console.error('Chat messages container not found!');
|
338 |
-
addMessage('bot', 'Display issue. Try again?');
|
339 |
-
return;
|
340 |
-
}
|
341 |
-
|
342 |
-
const itemsGrid = document.createElement('div');
|
343 |
-
itemsGrid.className = 'items-grid';
|
344 |
-
|
345 |
-
const itemDiv = document.createElement('div');
|
346 |
-
itemDiv.className = 'item-card';
|
347 |
-
itemDiv.dataset.hidden = 'true';
|
348 |
-
|
349 |
-
const img = document.createElement('img');
|
350 |
-
img.src = item.image_url || 'https://via.placeholder.com/60';
|
351 |
-
img.alt = item.name;
|
352 |
-
img.className = 'item-image';
|
353 |
-
itemDiv.appendChild(img);
|
354 |
-
|
355 |
-
const contentDiv = document.createElement('div');
|
356 |
-
contentDiv.className = 'item-content';
|
357 |
-
|
358 |
-
const nameDiv = document.createElement('div');
|
359 |
-
nameDiv.textContent = item.name;
|
360 |
-
nameDiv.className = 'item-name';
|
361 |
-
contentDiv.appendChild(nameDiv);
|
362 |
-
|
363 |
-
itemDiv.appendChild(contentDiv);
|
364 |
-
|
365 |
-
const buttonContainer = document.createElement('div');
|
366 |
-
buttonContainer.className = 'button-container';
|
367 |
-
|
368 |
-
const showButton = document.createElement('button');
|
369 |
-
showButton.textContent = 'Show';
|
370 |
-
showButton.className = 'show-button';
|
371 |
-
showButton.onclick = () => toggleDescription(itemDiv, item.description, item.name);
|
372 |
-
buttonContainer.appendChild(showButton);
|
373 |
-
|
374 |
-
const addButton = document.createElement('button');
|
375 |
-
addButton.textContent = 'Add';
|
376 |
-
addButton.className = 'add-button';
|
377 |
-
addButton.onclick = () => {
|
378 |
-
if (selectedItems.some(existing => existing.name === item.name)) {
|
379 |
-
addMessage('bot', `"${item.name}" already selected!`);
|
380 |
-
} else {
|
381 |
-
selectedItems.push({ ...item, quantity: 1 });
|
382 |
-
addMessage('bot', `Added "${item.name}"!`);
|
383 |
-
updateSelectionBox();
|
384 |
-
}
|
385 |
-
};
|
386 |
-
buttonContainer.appendChild(addButton);
|
387 |
-
|
388 |
-
itemDiv.appendChild(buttonContainer);
|
389 |
-
itemsGrid.appendChild(itemDiv);
|
390 |
-
|
391 |
-
chatMessages.appendChild(itemsGrid);
|
392 |
-
chatMessages.scrollTop = chatMessages.scrollHeight;
|
393 |
-
}
|
394 |
-
|
395 |
function displayOptions(options) {
|
396 |
const chatMessages = document.getElementById('chatMessages');
|
397 |
if (!chatMessages) {
|
@@ -474,7 +429,7 @@ function resetConversation() {
|
|
474 |
conversation = [
|
475 |
{ role: 'bot', message: `Hello! I’m Chef Bot, your culinary assistant! What’s your name?` },
|
476 |
{ role: 'user', message: userName },
|
477 |
-
{ role: 'bot', message: `Hi ${userName}! 🍳 Search for a dish
|
478 |
];
|
479 |
selectedItems = [];
|
480 |
selectionBoxVisible = true;
|
|
|
31 |
selectionBoxVisible = true;
|
32 |
handleResponse(message);
|
33 |
} else {
|
34 |
+
addMessage('bot', 'Please type a dish or preference! 😄');
|
35 |
}
|
36 |
userInput.value = '';
|
37 |
updateSelectionBox();
|
|
|
42 |
let botResponse = '';
|
43 |
|
44 |
if (conversation.length === 2) {
|
45 |
+
botResponse = `Hi ${userInput}! 🍳 Search for a dish or choose a preference below!`;
|
46 |
displayOptions([
|
47 |
{ text: 'Vegetarian', class: 'green' },
|
48 |
{ text: 'Non-Vegetarian', class: 'red' },
|
|
|
50 |
]);
|
51 |
addMessage('bot', botResponse);
|
52 |
} else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
|
53 |
+
botResponse = `Fetching ${lowerInput} dishes...`;
|
54 |
addMessage('bot', botResponse);
|
55 |
+
fetchMenuItems(lowerInput);
|
56 |
} else {
|
57 |
botResponse = `Looking for "${userInput}"...`;
|
58 |
addMessage('bot', botResponse);
|
59 |
+
fetchMenuItems(null, userInput);
|
60 |
}
|
61 |
}
|
62 |
|
|
|
109 |
selectedItems.forEach((item, index) => {
|
110 |
const itemContainer = document.createElement('div');
|
111 |
itemContainer.className = 'selected-item';
|
112 |
+
itemContainer.dataset.hidden = item.source === 'Sector_Detail__c' ? 'true' : 'false';
|
113 |
|
114 |
const img = document.createElement('img');
|
115 |
img.src = item.image_url || 'https://via.placeholder.com/30';
|
|
|
117 |
img.className = 'selected-item-image';
|
118 |
itemContainer.appendChild(img);
|
119 |
|
120 |
+
const contentDiv = document.createElement('div');
|
121 |
+
contentDiv.className = 'selected-item-content';
|
122 |
+
|
123 |
const itemSpan = document.createElement('span');
|
124 |
itemSpan.textContent = `${item.name} (Qty: ${item.quantity || 1})`;
|
125 |
+
contentDiv.appendChild(itemSpan);
|
126 |
+
|
127 |
+
if (item.source === 'Sector_Detail__c') {
|
128 |
+
const showButton = document.createElement('button');
|
129 |
+
showButton.textContent = 'Show';
|
130 |
+
showButton.className = 'show-button';
|
131 |
+
showButton.onclick = () => toggleDescription(itemContainer, item.description, item.name);
|
132 |
+
contentDiv.appendChild(showButton);
|
133 |
+
}
|
134 |
+
|
135 |
+
itemContainer.appendChild(contentDiv);
|
136 |
|
137 |
const removeButton = document.createElement('button');
|
138 |
removeButton.textContent = 'X';
|
|
|
187 |
console.log('Updated selection box:', selectedItems.map(item => ({ name: item.name, category: item.category })));
|
188 |
}
|
189 |
|
190 |
+
function fetchMenuItems(dietaryPreference = '', searchTerm = '') {
|
191 |
+
const payload = {};
|
192 |
+
if (dietaryPreference) payload.dietary_preference = dietaryPreference;
|
193 |
+
if (searchTerm) payload.search_term = searchTerm;
|
194 |
fetch('/get_menu_items', {
|
195 |
method: 'POST',
|
196 |
headers: { 'Content-Type': 'application/json' },
|
197 |
+
body: JSON.stringify(payload)
|
198 |
})
|
199 |
.then(response => response.json())
|
200 |
.then(data => {
|
|
|
204 |
addMessage('bot', `--- Found ${data.menu_items.length} item${data.menu_items.length > 1 ? 's' : ''} ---`);
|
205 |
displayItemsList(data.menu_items);
|
206 |
} else {
|
207 |
+
addMessage('bot', `No matches for "${searchTerm || dietaryPreference}". Try "paneer"!`);
|
208 |
}
|
209 |
+
console.log(`Fetched items for ${searchTerm || dietaryPreference}:`, data.menu_items);
|
210 |
})
|
211 |
.catch(error => {
|
212 |
addMessage('bot', `Connection issue: ${error.message}. Retrying...`);
|
213 |
+
setTimeout(() => fetchMenuItems(dietaryPreference, searchTerm), 2000);
|
214 |
});
|
215 |
}
|
216 |
|
|
|
223 |
.then(response => response.json())
|
224 |
.then(data => {
|
225 |
if (data.error) {
|
226 |
+
addMessage('bot', `No "${itemName}" found. Try another!`);
|
227 |
} else {
|
228 |
const details = data.item_details;
|
229 |
if (selectedItems.some(item => item.name === details.name)) {
|
230 |
addMessage('bot', `"${details.name}" already selected!`);
|
231 |
} else {
|
232 |
selectedItems.push({ ...details, quantity: 1 });
|
233 |
+
addMessage('bot', `Added "${details.name}"!`);
|
|
|
234 |
updateSelectionBox();
|
235 |
}
|
236 |
}
|
|
|
241 |
});
|
242 |
}
|
243 |
|
244 |
+
function toggleDescription(itemContainer, description, itemName) {
|
245 |
+
let descElement = itemContainer.querySelector('.item-description');
|
246 |
if (!descElement) {
|
247 |
descElement = document.createElement('p');
|
248 |
descElement.className = 'item-description';
|
249 |
descElement.textContent = description;
|
250 |
+
itemContainer.querySelector('.selected-item-content').appendChild(descElement);
|
251 |
+
itemContainer.dataset.hidden = 'false';
|
252 |
console.log(`Showed description for ${itemName}`);
|
253 |
} else {
|
254 |
descElement.remove();
|
255 |
+
itemContainer.dataset.hidden = 'true';
|
256 |
console.log(`Hid description for ${itemName}`);
|
257 |
}
|
258 |
}
|
|
|
271 |
items.forEach(item => {
|
272 |
const itemDiv = document.createElement('div');
|
273 |
itemDiv.className = 'item-card';
|
|
|
274 |
|
275 |
const img = document.createElement('img');
|
276 |
img.src = item.image_url || 'https://via.placeholder.com/60';
|
|
|
347 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
348 |
}
|
349 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
350 |
function displayOptions(options) {
|
351 |
const chatMessages = document.getElementById('chatMessages');
|
352 |
if (!chatMessages) {
|
|
|
429 |
conversation = [
|
430 |
{ role: 'bot', message: `Hello! I’m Chef Bot, your culinary assistant! What’s your name?` },
|
431 |
{ role: 'user', message: userName },
|
432 |
+
{ role: 'bot', message: `Hi ${userName}! 🍳 Search for a dish or choose a preference below!` }
|
433 |
];
|
434 |
selectedItems = [];
|
435 |
selectionBoxVisible = true;
|