Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +116 -58
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 or
|
46 |
displayOptions([
|
47 |
{ text: 'Vegetarian', class: 'green' },
|
48 |
{ text: 'Non-Vegetarian', class: 'red' },
|
@@ -50,13 +50,12 @@ 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 |
-
fetchMenuItems(lowerInput);
|
56 |
} else {
|
57 |
botResponse = `Looking for "${userInput}"...`;
|
58 |
addMessage('bot', botResponse);
|
59 |
-
fetchMenuItems(
|
60 |
}
|
61 |
}
|
62 |
|
@@ -75,19 +74,31 @@ function updateSelectionBox() {
|
|
75 |
const vegButton = document.createElement('button');
|
76 |
vegButton.textContent = 'Veg';
|
77 |
vegButton.className = 'dietary-button green';
|
78 |
-
vegButton.onclick = () =>
|
|
|
|
|
|
|
|
|
79 |
selectionBox.appendChild(vegButton);
|
80 |
|
81 |
const nonVegButton = document.createElement('button');
|
82 |
nonVegButton.textContent = 'Non-Veg';
|
83 |
nonVegButton.className = 'dietary-button red';
|
84 |
-
nonVegButton.onclick = () =>
|
|
|
|
|
|
|
|
|
85 |
selectionBox.appendChild(nonVegButton);
|
86 |
|
87 |
const bothButton = document.createElement('button');
|
88 |
bothButton.textContent = 'Both';
|
89 |
bothButton.className = 'dietary-button gray';
|
90 |
-
bothButton.onclick = () =>
|
|
|
|
|
|
|
|
|
91 |
selectionBox.appendChild(bothButton);
|
92 |
|
93 |
const label = document.createElement('span');
|
@@ -161,14 +172,11 @@ function updateSelectionBox() {
|
|
161 |
console.log('Updated selection box:', selectedItems.map(item => ({ name: item.name, category: item.category })));
|
162 |
}
|
163 |
|
164 |
-
function fetchMenuItems(
|
165 |
-
const payload = {};
|
166 |
-
if (dietaryPreference) payload.dietary_preference = dietaryPreference;
|
167 |
-
if (searchTerm) payload.search_term = searchTerm;
|
168 |
fetch('/get_menu_items', {
|
169 |
method: 'POST',
|
170 |
headers: { 'Content-Type': 'application/json' },
|
171 |
-
body: JSON.stringify(
|
172 |
})
|
173 |
.then(response => response.json())
|
174 |
.then(data => {
|
@@ -178,13 +186,13 @@ function fetchMenuItems(dietaryPreference, searchTerm = '') {
|
|
178 |
addMessage('bot', `--- Found ${data.menu_items.length} item${data.menu_items.length > 1 ? 's' : ''} ---`);
|
179 |
displayItemsList(data.menu_items);
|
180 |
} else {
|
181 |
-
addMessage('bot', `No matches for "${searchTerm
|
182 |
}
|
183 |
-
console.log(`Fetched items for ${searchTerm
|
184 |
})
|
185 |
.catch(error => {
|
186 |
addMessage('bot', `Connection issue: ${error.message}. Retrying...`);
|
187 |
-
setTimeout(() => fetchMenuItems(
|
188 |
});
|
189 |
}
|
190 |
|
@@ -197,14 +205,15 @@ function fetchSectorItemDetails(itemName) {
|
|
197 |
.then(response => response.json())
|
198 |
.then(data => {
|
199 |
if (data.error) {
|
200 |
-
addMessage('bot', `No "${itemName}" found. Try another!`);
|
201 |
} else {
|
202 |
const details = data.item_details;
|
203 |
if (selectedItems.some(item => item.name === details.name)) {
|
204 |
addMessage('bot', `"${details.name}" already selected!`);
|
205 |
} else {
|
206 |
selectedItems.push({ ...details, quantity: 1 });
|
207 |
-
addMessage('bot',
|
|
|
208 |
updateSelectionBox();
|
209 |
}
|
210 |
}
|
@@ -245,7 +254,7 @@ function displayItemsList(items) {
|
|
245 |
items.forEach(item => {
|
246 |
const itemDiv = document.createElement('div');
|
247 |
itemDiv.className = 'item-card';
|
248 |
-
itemDiv.dataset.hidden =
|
249 |
|
250 |
const img = document.createElement('img');
|
251 |
img.src = item.image_url || 'https://via.placeholder.com/60';
|
@@ -261,40 +270,30 @@ function displayItemsList(items) {
|
|
261 |
nameDiv.className = 'item-name';
|
262 |
contentDiv.appendChild(nameDiv);
|
263 |
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
|
269 |
-
|
270 |
-
|
271 |
-
|
272 |
-
|
273 |
-
|
274 |
-
|
275 |
-
|
276 |
-
|
277 |
-
|
278 |
-
|
279 |
-
|
280 |
-
|
281 |
-
|
282 |
-
});
|
283 |
-
}
|
284 |
|
285 |
itemDiv.appendChild(contentDiv);
|
286 |
|
287 |
const buttonContainer = document.createElement('div');
|
288 |
buttonContainer.className = 'button-container';
|
289 |
|
290 |
-
if (item.source === 'Sector_Detail__c') {
|
291 |
-
const showButton = document.createElement('button');
|
292 |
-
showButton.textContent = 'Show';
|
293 |
-
showButton.className = 'show-button';
|
294 |
-
showButton.onclick = () => toggleDescription(itemDiv, item.description, item.name);
|
295 |
-
buttonContainer.appendChild(showButton);
|
296 |
-
}
|
297 |
-
|
298 |
const addButton = document.createElement('button');
|
299 |
addButton.textContent = 'Add';
|
300 |
addButton.className = 'add-button';
|
@@ -305,17 +304,15 @@ function displayItemsList(items) {
|
|
305 |
category: item.category || 'Not specified',
|
306 |
description: item.description || 'No description available',
|
307 |
source: item.source,
|
308 |
-
quantity: 1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
309 |
};
|
310 |
-
if (item.source === 'Menu_Item__c') {
|
311 |
-
selectedItem.ingredients = item.ingredients;
|
312 |
-
selectedItem.nutritional_info = item.nutritional_info;
|
313 |
-
selectedItem.price = item.price;
|
314 |
-
selectedItem.sector = item.sector;
|
315 |
-
selectedItem.spice_levels = item.spice_levels;
|
316 |
-
selectedItem.veg_nonveg = item.veg_nonveg;
|
317 |
-
selectedItem.dynamic_dish = item.dynamic_dish;
|
318 |
-
}
|
319 |
if (selectedItems.some(existing => existing.name === selectedItem.name)) {
|
320 |
addMessage('bot', `"${selectedItem.name}" already selected!`);
|
321 |
} else {
|
@@ -334,6 +331,67 @@ function displayItemsList(items) {
|
|
334 |
chatMessages.scrollTop = chatMessages.scrollHeight;
|
335 |
}
|
336 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
337 |
function displayOptions(options) {
|
338 |
const chatMessages = document.getElementById('chatMessages');
|
339 |
if (!chatMessages) {
|
@@ -416,7 +474,7 @@ function resetConversation() {
|
|
416 |
conversation = [
|
417 |
{ role: 'bot', message: `Hello! I’m Chef Bot, your culinary assistant! What’s your name?` },
|
418 |
{ role: 'user', message: userName },
|
419 |
-
{ role: 'bot', message: `Hi ${userName}! 🍳 Search for a dish or
|
420 |
];
|
421 |
selectedItems = [];
|
422 |
selectionBoxVisible = true;
|
|
|
31 |
selectionBoxVisible = true;
|
32 |
handleResponse(message);
|
33 |
} else {
|
34 |
+
addMessage('bot', 'Please type a dish name! 😄');
|
35 |
}
|
36 |
userInput.value = '';
|
37 |
updateSelectionBox();
|
|
|
42 |
let botResponse = '';
|
43 |
|
44 |
if (conversation.length === 2) {
|
45 |
+
botResponse = `Hi ${userInput}! 🍳 Search for a dish like "chicken" or add items 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 = `Selected ${lowerInput}. Search for a dish or add an item!`;
|
54 |
addMessage('bot', botResponse);
|
|
|
55 |
} else {
|
56 |
botResponse = `Looking for "${userInput}"...`;
|
57 |
addMessage('bot', botResponse);
|
58 |
+
fetchMenuItems(userInput);
|
59 |
}
|
60 |
}
|
61 |
|
|
|
74 |
const vegButton = document.createElement('button');
|
75 |
vegButton.textContent = 'Veg';
|
76 |
vegButton.className = 'dietary-button green';
|
77 |
+
vegButton.onclick = () => {
|
78 |
+
addMessage('user', 'Vegetarian');
|
79 |
+
conversation.push({ role: 'user', message: 'Vegetarian' });
|
80 |
+
handleResponse('vegetarian');
|
81 |
+
};
|
82 |
selectionBox.appendChild(vegButton);
|
83 |
|
84 |
const nonVegButton = document.createElement('button');
|
85 |
nonVegButton.textContent = 'Non-Veg';
|
86 |
nonVegButton.className = 'dietary-button red';
|
87 |
+
nonVegButton.onclick = () => {
|
88 |
+
addMessage('user', 'Non-Vegetarian');
|
89 |
+
conversation.push({ role: 'user', message: 'Non-Vegetarian' });
|
90 |
+
handleResponse('non-vegetarian');
|
91 |
+
};
|
92 |
selectionBox.appendChild(nonVegButton);
|
93 |
|
94 |
const bothButton = document.createElement('button');
|
95 |
bothButton.textContent = 'Both';
|
96 |
bothButton.className = 'dietary-button gray';
|
97 |
+
bothButton.onclick = () => {
|
98 |
+
addMessage('user', 'Both');
|
99 |
+
conversation.push({ role: 'user', message: 'Both' });
|
100 |
+
handleResponse('both');
|
101 |
+
};
|
102 |
selectionBox.appendChild(bothButton);
|
103 |
|
104 |
const label = document.createElement('span');
|
|
|
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({ search_term: searchTerm })
|
180 |
})
|
181 |
.then(response => response.json())
|
182 |
.then(data => {
|
|
|
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 "chicken"!`);
|
190 |
}
|
191 |
+
console.log(`Fetched Menu_Item__c items for ${searchTerm}:`, data.menu_items);
|
192 |
})
|
193 |
.catch(error => {
|
194 |
addMessage('bot', `Connection issue: ${error.message}. Retrying...`);
|
195 |
+
setTimeout(() => fetchMenuItems(searchTerm), 2000);
|
196 |
});
|
197 |
}
|
198 |
|
|
|
205 |
.then(response => response.json())
|
206 |
.then(data => {
|
207 |
if (data.error) {
|
208 |
+
addMessage('bot', `No "${itemName}" found in Sector_Detail__c. Try another!`);
|
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', `--- Added "${details.name}" ---`);
|
216 |
+
displaySingleItem(details);
|
217 |
updateSelectionBox();
|
218 |
}
|
219 |
}
|
|
|
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';
|
|
|
270 |
nameDiv.className = 'item-name';
|
271 |
contentDiv.appendChild(nameDiv);
|
272 |
|
273 |
+
const fields = [
|
274 |
+
{ label: 'Price', value: item.price ? `$${item.price.toFixed(2)}` : 'N/A' },
|
275 |
+
{ label: 'Veg/Non-Veg', value: item.veg_nonveg },
|
276 |
+
{ label: 'Spice', value: item.spice_levels },
|
277 |
+
{ label: 'Category', value: item.category },
|
278 |
+
{ label: 'Ingredients', value: item.ingredients },
|
279 |
+
{ label: 'Nutrition', value: item.nutritional_info },
|
280 |
+
{ label: 'Sector', value: item.sector },
|
281 |
+
{ label: 'Dynamic', value: item.dynamic_dish ? 'Yes' : 'No' }
|
282 |
+
];
|
283 |
+
fields.forEach(field => {
|
284 |
+
if (field.value) {
|
285 |
+
const p = document.createElement('p');
|
286 |
+
p.className = 'item-field';
|
287 |
+
p.innerHTML = `<strong>${field.label}:</strong> ${field.value}`;
|
288 |
+
contentDiv.appendChild(p);
|
289 |
+
}
|
290 |
+
});
|
|
|
|
|
291 |
|
292 |
itemDiv.appendChild(contentDiv);
|
293 |
|
294 |
const buttonContainer = document.createElement('div');
|
295 |
buttonContainer.className = 'button-container';
|
296 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
297 |
const addButton = document.createElement('button');
|
298 |
addButton.textContent = 'Add';
|
299 |
addButton.className = 'add-button';
|
|
|
304 |
category: item.category || 'Not specified',
|
305 |
description: item.description || 'No description available',
|
306 |
source: item.source,
|
307 |
+
quantity: 1,
|
308 |
+
ingredients: item.ingredients,
|
309 |
+
nutritional_info: item.nutritional_info,
|
310 |
+
price: item.price,
|
311 |
+
sector: item.sector,
|
312 |
+
spice_levels: item.spice_levels,
|
313 |
+
veg_nonveg: item.veg_nonveg,
|
314 |
+
dynamic_dish: item.dynamic_dish
|
315 |
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
316 |
if (selectedItems.some(existing => existing.name === selectedItem.name)) {
|
317 |
addMessage('bot', `"${selectedItem.name}" already selected!`);
|
318 |
} else {
|
|
|
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 |
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 like "chicken" or add items below!` }
|
478 |
];
|
479 |
selectedItems = [];
|
480 |
selectionBoxVisible = true;
|