Spaces:
Running
Running
Update static/script.js
Browse files- static/script.js +81 -59
static/script.js
CHANGED
@@ -100,12 +100,12 @@ function updateSelectionBox() {
|
|
100 |
|
101 |
const textInput = document.createElement('input');
|
102 |
textInput.type = 'text';
|
103 |
-
textInput.placeholder = 'Add
|
104 |
textInput.className = 'manual-input';
|
105 |
textInput.addEventListener('keypress', (e) => {
|
106 |
if (e.key === 'Enter' && textInput.value.trim()) {
|
107 |
const itemName = textInput.value.trim();
|
108 |
-
|
109 |
textInput.value = '';
|
110 |
}
|
111 |
});
|
@@ -143,7 +143,7 @@ function handleResponse(userInput) {
|
|
143 |
} else {
|
144 |
botResponse = `Looking up details for "${userInput}"...`;
|
145 |
addMessage('bot', botResponse);
|
146 |
-
|
147 |
return;
|
148 |
}
|
149 |
|
@@ -163,9 +163,9 @@ function fetchMenuItems(dietaryPreference) {
|
|
163 |
} else if (data.menu_items.length > 0) {
|
164 |
displayItemsList(data.menu_items, 'menuItemsList', true);
|
165 |
} else {
|
166 |
-
addMessage('bot', `No ${dietaryPreference} items found
|
167 |
}
|
168 |
-
console.log(`Fetched items
|
169 |
})
|
170 |
.catch(error => {
|
171 |
addMessage('bot', `Yikes! Couldn’t reach Salesforce: ${error.message}. I’ll retry in a sec...`);
|
@@ -184,10 +184,10 @@ function suggestItems(searchTerm) {
|
|
184 |
if (data.error) {
|
185 |
addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Got another idea?`);
|
186 |
} else if (data.suggestions.length > 0) {
|
187 |
-
addMessage('bot', `Check out these suggestions for "${searchTerm}" from
|
188 |
displayItemsList(data.suggestions, 'suggestionsList', false);
|
189 |
} else {
|
190 |
-
addMessage('bot', `No matches for "${searchTerm}" in
|
191 |
}
|
192 |
console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
|
193 |
})
|
@@ -197,7 +197,8 @@ function suggestItems(searchTerm) {
|
|
197 |
});
|
198 |
}
|
199 |
|
200 |
-
function
|
|
|
201 |
fetch('/get_menu_item_details', {
|
202 |
method: 'POST',
|
203 |
headers: { 'Content-Type': 'application/json' },
|
@@ -210,20 +211,13 @@ function fetchMenuItemDetails(itemName) {
|
|
210 |
fetchSectorItemDetails(itemName);
|
211 |
} else {
|
212 |
const details = data.item_details;
|
213 |
-
|
214 |
-
|
215 |
-
} else {
|
216 |
-
details.source = 'Menu_Item__c';
|
217 |
-
selectedItems.push(details);
|
218 |
-
addMessage('bot', `Added "${details.name}" to your selection! Check the box below.`);
|
219 |
-
updateSelectionBox();
|
220 |
-
showDescriptionPopup(details); // Show detailed popup
|
221 |
-
}
|
222 |
}
|
223 |
})
|
224 |
.catch(error => {
|
225 |
-
addMessage('bot', `Trouble fetching "${itemName}": ${error.message}.
|
226 |
-
|
227 |
});
|
228 |
}
|
229 |
|
@@ -236,17 +230,11 @@ function fetchSectorItemDetails(itemName) {
|
|
236 |
.then(response => response.json())
|
237 |
.then(data => {
|
238 |
if (data.error) {
|
239 |
-
addMessage('bot', `Sorry, I couldn’t find "${itemName}" in
|
240 |
} else {
|
241 |
const details = data.item_details;
|
242 |
-
|
243 |
-
|
244 |
-
} else {
|
245 |
-
details.source = 'Sector_Detail__c';
|
246 |
-
selectedItems.push(details);
|
247 |
-
addMessage('bot', `Added "${details.name}" to your selection with image! Check the box below.`);
|
248 |
-
updateSelectionBox();
|
249 |
-
}
|
250 |
}
|
251 |
})
|
252 |
.catch(error => {
|
@@ -279,30 +267,26 @@ function showDescriptionPopup(item) {
|
|
279 |
desc.textContent = item.description;
|
280 |
content.appendChild(desc);
|
281 |
|
282 |
-
// Display additional Menu_Item__c fields if available
|
283 |
if (item.source === 'Menu_Item__c') {
|
284 |
-
const detailsList = document.createElement('ul');
|
285 |
-
detailsList.style.listStyleType = 'none';
|
286 |
-
detailsList.style.padding = '0';
|
287 |
-
|
288 |
const fields = [
|
|
|
|
|
289 |
{ label: 'Ingredients', value: item.ingredients },
|
290 |
{ label: 'Nutritional Info', value: item.nutritional_info },
|
291 |
-
{ label: 'Price', value: `$${item.price.toFixed(2)}` },
|
292 |
{ label: 'Sector', value: item.sector },
|
293 |
-
{ label: 'Spice
|
294 |
-
{ label: 'Veg/Non-Veg', value: item.veg_nonveg },
|
295 |
-
{ label: 'Category', value: item.category },
|
296 |
{ label: 'Dynamic Dish', value: item.dynamic_dish ? 'Yes' : 'No' }
|
297 |
];
|
298 |
|
299 |
fields.forEach(field => {
|
300 |
-
|
301 |
-
|
302 |
-
|
|
|
|
|
|
|
303 |
});
|
304 |
-
|
305 |
-
content.appendChild(detailsList);
|
306 |
}
|
307 |
|
308 |
const buttonContainer = document.createElement('div');
|
@@ -312,11 +296,27 @@ function showDescriptionPopup(item) {
|
|
312 |
addButton.textContent = 'Add';
|
313 |
addButton.className = 'popup-add-button';
|
314 |
addButton.onclick = () => {
|
315 |
-
|
316 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
317 |
} else {
|
318 |
-
selectedItems.push(
|
319 |
-
addMessage('bot', `Added "${
|
320 |
updateSelectionBox();
|
321 |
}
|
322 |
popup.remove();
|
@@ -334,7 +334,7 @@ function showDescriptionPopup(item) {
|
|
334 |
document.body.appendChild(popup);
|
335 |
}
|
336 |
|
337 |
-
function displayItemsList(items, containerId,
|
338 |
const container = document.getElementById(containerId);
|
339 |
if (!container) {
|
340 |
console.error(`${containerId} container not found!`);
|
@@ -359,17 +359,33 @@ function displayItemsList(items, containerId, isSectorDetail = false) {
|
|
359 |
nameDiv.className = 'ingredient-name';
|
360 |
itemDiv.appendChild(nameDiv);
|
361 |
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
373 |
|
374 |
container.appendChild(itemDiv);
|
375 |
});
|
@@ -415,10 +431,16 @@ function submitToSalesforce() {
|
|
415 |
return;
|
416 |
}
|
417 |
|
|
|
|
|
|
|
|
|
|
|
|
|
418 |
fetch('/submit_items', {
|
419 |
method: 'POST',
|
420 |
headers: { 'Content-Type': 'application/json' },
|
421 |
-
body: JSON.stringify({ items:
|
422 |
})
|
423 |
.then(response => response.json())
|
424 |
.then(data => {
|
|
|
100 |
|
101 |
const textInput = document.createElement('input');
|
102 |
textInput.type = 'text';
|
103 |
+
textInput.placeholder = 'Add item (Sector_Detail__c or Menu_Item__c)...';
|
104 |
textInput.className = 'manual-input';
|
105 |
textInput.addEventListener('keypress', (e) => {
|
106 |
if (e.key === 'Enter' && textInput.value.trim()) {
|
107 |
const itemName = textInput.value.trim();
|
108 |
+
fetchSectorOrMenuItemDetails(itemName);
|
109 |
textInput.value = '';
|
110 |
}
|
111 |
});
|
|
|
143 |
} else {
|
144 |
botResponse = `Looking up details for "${userInput}"...`;
|
145 |
addMessage('bot', botResponse);
|
146 |
+
fetchSectorOrMenuItemDetails(userInput);
|
147 |
return;
|
148 |
}
|
149 |
|
|
|
163 |
} else if (data.menu_items.length > 0) {
|
164 |
displayItemsList(data.menu_items, 'menuItemsList', true);
|
165 |
} else {
|
166 |
+
addMessage('bot', `No ${dietaryPreference} items found. Try searching for something like "paneer" or "chicken"!`);
|
167 |
}
|
168 |
+
console.log(`Fetched items for ${dietaryPreference}:`, data.menu_items);
|
169 |
})
|
170 |
.catch(error => {
|
171 |
addMessage('bot', `Yikes! Couldn’t reach Salesforce: ${error.message}. I’ll retry in a sec...`);
|
|
|
184 |
if (data.error) {
|
185 |
addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Got another idea?`);
|
186 |
} else if (data.suggestions.length > 0) {
|
187 |
+
addMessage('bot', `Check out these suggestions for "${searchTerm}" from Ingredient_Object__c:`);
|
188 |
displayItemsList(data.suggestions, 'suggestionsList', false);
|
189 |
} else {
|
190 |
+
addMessage('bot', `No matches for "${searchTerm}" in Ingredient_Object__c. Try "chicken" or "paneer"?`);
|
191 |
}
|
192 |
console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
|
193 |
})
|
|
|
197 |
});
|
198 |
}
|
199 |
|
200 |
+
function fetchSectorOrMenuItemDetails(itemName) {
|
201 |
+
// Try Menu_Item__c first
|
202 |
fetch('/get_menu_item_details', {
|
203 |
method: 'POST',
|
204 |
headers: { 'Content-Type': 'application/json' },
|
|
|
211 |
fetchSectorItemDetails(itemName);
|
212 |
} else {
|
213 |
const details = data.item_details;
|
214 |
+
addMessage('bot', `Found "${details.name}" in Menu_Item__c!`);
|
215 |
+
showDescriptionPopup(details);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
}
|
217 |
})
|
218 |
.catch(error => {
|
219 |
+
addMessage('bot', `Trouble fetching "${itemName}": ${error.message}. Trying Sector_Detail__c...`);
|
220 |
+
fetchSectorItemDetails(itemName);
|
221 |
});
|
222 |
}
|
223 |
|
|
|
230 |
.then(response => response.json())
|
231 |
.then(data => {
|
232 |
if (data.error) {
|
233 |
+
addMessage('bot', `Sorry, I couldn’t find "${itemName}" in Menu_Item__c or Sector_Detail__c. Try another item!`);
|
234 |
} else {
|
235 |
const details = data.item_details;
|
236 |
+
addMessage('bot', `Found "${details.name}" in Sector_Detail__c!`);
|
237 |
+
showDescriptionPopup(details);
|
|
|
|
|
|
|
|
|
|
|
|
|
238 |
}
|
239 |
})
|
240 |
.catch(error => {
|
|
|
267 |
desc.textContent = item.description;
|
268 |
content.appendChild(desc);
|
269 |
|
|
|
270 |
if (item.source === 'Menu_Item__c') {
|
|
|
|
|
|
|
|
|
271 |
const fields = [
|
272 |
+
{ label: 'Category', value: item.category },
|
273 |
+
{ label: 'Veg/Non-Veg', value: item.veg_nonveg },
|
274 |
{ label: 'Ingredients', value: item.ingredients },
|
275 |
{ label: 'Nutritional Info', value: item.nutritional_info },
|
276 |
+
{ label: 'Price', value: item.price ? `$${item.price.toFixed(2)}` : 'N/A' },
|
277 |
{ label: 'Sector', value: item.sector },
|
278 |
+
{ label: 'Spice Levels', value: item.spice_levels },
|
|
|
|
|
279 |
{ label: 'Dynamic Dish', value: item.dynamic_dish ? 'Yes' : 'No' }
|
280 |
];
|
281 |
|
282 |
fields.forEach(field => {
|
283 |
+
if (field.value) {
|
284 |
+
const p = document.createElement('p');
|
285 |
+
p.className = 'menu-item-field';
|
286 |
+
p.innerHTML = `<strong>${field.label}:</strong> ${field.value}`;
|
287 |
+
content.appendChild(p);
|
288 |
+
}
|
289 |
});
|
|
|
|
|
290 |
}
|
291 |
|
292 |
const buttonContainer = document.createElement('div');
|
|
|
296 |
addButton.textContent = 'Add';
|
297 |
addButton.className = 'popup-add-button';
|
298 |
addButton.onclick = () => {
|
299 |
+
const selectedItem = {
|
300 |
+
name: item.name,
|
301 |
+
image_url: item.image_url || '',
|
302 |
+
category: item.category || 'Unknown',
|
303 |
+
description: item.description || 'No description available',
|
304 |
+
source: item.source
|
305 |
+
};
|
306 |
+
if (item.source === 'Menu_Item__c') {
|
307 |
+
selectedItem.ingredients = item.ingredients;
|
308 |
+
selectedItem.nutritional_info = item.nutritional_info;
|
309 |
+
selectedItem.price = item.price;
|
310 |
+
selectedItem.sector = item.sector;
|
311 |
+
selectedItem.spice_levels = item.spice_levels;
|
312 |
+
selectedItem.veg_nonveg = item.veg_nonveg;
|
313 |
+
selectedItem.dynamic_dish = item.dynamic_dish;
|
314 |
+
}
|
315 |
+
if (selectedItems.some(existing => existing.name === selectedItem.name)) {
|
316 |
+
addMessage('bot', `"${selectedItem.name}" is already in your selection!`);
|
317 |
} else {
|
318 |
+
selectedItems.push(selectedItem);
|
319 |
+
addMessage('bot', `Added "${selectedItem.name}" to your selection! Check the box below.`);
|
320 |
updateSelectionBox();
|
321 |
}
|
322 |
popup.remove();
|
|
|
334 |
document.body.appendChild(popup);
|
335 |
}
|
336 |
|
337 |
+
function displayItemsList(items, containerId, isDetailed = false) {
|
338 |
const container = document.getElementById(containerId);
|
339 |
if (!container) {
|
340 |
console.error(`${containerId} container not found!`);
|
|
|
359 |
nameDiv.className = 'ingredient-name';
|
360 |
itemDiv.appendChild(nameDiv);
|
361 |
|
362 |
+
if (isDetailed) {
|
363 |
+
const showDescButton = document.createElement('button');
|
364 |
+
showDescButton.textContent = 'Show Details';
|
365 |
+
showDescButton.className = 'show-desc-button';
|
366 |
+
showDescButton.onclick = () => showDescriptionPopup(item);
|
367 |
+
itemDiv.appendChild(showDescButton);
|
368 |
+
} else {
|
369 |
+
const addButton = document.createElement('button');
|
370 |
+
addButton.textContent = 'Add';
|
371 |
+
addButton.onclick = () => {
|
372 |
+
const selectedItem = {
|
373 |
+
name: item.name,
|
374 |
+
image_url: item.image_url || '',
|
375 |
+
category: item.category || 'Unknown',
|
376 |
+
description: item.description || 'No description available',
|
377 |
+
source: item.source || 'Unknown'
|
378 |
+
};
|
379 |
+
if (selectedItems.some(existing => existing.name === selectedItem.name)) {
|
380 |
+
addMessage('bot', `"${selectedItem.name}" is already in your selection!`);
|
381 |
+
} else {
|
382 |
+
selectedItems.push(selectedItem);
|
383 |
+
addMessage('bot', `Added "${selectedItem.name}" to your selection!`);
|
384 |
+
updateSelectionBox();
|
385 |
+
}
|
386 |
+
};
|
387 |
+
itemDiv.appendChild(addButton);
|
388 |
+
}
|
389 |
|
390 |
container.appendChild(itemDiv);
|
391 |
});
|
|
|
431 |
return;
|
432 |
}
|
433 |
|
434 |
+
const itemsToSubmit = selectedItems.map(item => ({
|
435 |
+
name: item.name,
|
436 |
+
category: item.category || 'Unknown',
|
437 |
+
description: item.description || 'No description available'
|
438 |
+
}));
|
439 |
+
|
440 |
fetch('/submit_items', {
|
441 |
method: 'POST',
|
442 |
headers: { 'Content-Type': 'application/json' },
|
443 |
+
body: JSON.stringify({ items: itemsToSubmit })
|
444 |
})
|
445 |
.then(response => response.json())
|
446 |
.then(data => {
|