Spaces:
Running
Running
Update static/script.js
Browse files- static/script.js +44 -14
static/script.js
CHANGED
@@ -100,23 +100,13 @@ function updateSelectionBox() {
|
|
100 |
|
101 |
const textInput = document.createElement('input');
|
102 |
textInput.type = 'text';
|
103 |
-
textInput.placeholder = '
|
104 |
textInput.className = 'manual-input';
|
105 |
textInput.addEventListener('keypress', (e) => {
|
106 |
if (e.key === 'Enter' && textInput.value.trim()) {
|
107 |
-
const
|
108 |
-
|
109 |
-
image_url: '',
|
110 |
-
description: 'Manually added item'
|
111 |
-
};
|
112 |
-
if (selectedItems.some(item => item.name === manualItem.name)) {
|
113 |
-
addMessage('bot', `"${manualItem.name}" is already in your selection!`);
|
114 |
-
} else {
|
115 |
-
selectedItems.push(manualItem);
|
116 |
-
addMessage('bot', `Added "${manualItem.name}" to your selection! Check the box below.`);
|
117 |
-
}
|
118 |
textInput.value = '';
|
119 |
-
updateSelectionBox();
|
120 |
}
|
121 |
});
|
122 |
selectionBox.appendChild(textInput);
|
@@ -151,7 +141,6 @@ function handleResponse(userInput) {
|
|
151 |
fetchMenuItems(lowerInput);
|
152 |
return;
|
153 |
} else {
|
154 |
-
// Treat input as an item name and fetch details
|
155 |
botResponse = `Looking up details for "${userInput}"...`;
|
156 |
addMessage('bot', botResponse);
|
157 |
fetchItemDetailsByName(userInput);
|
@@ -258,6 +247,47 @@ function fetchItemDetailsByName(itemName) {
|
|
258 |
});
|
259 |
}
|
260 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
261 |
function showDescriptionPopup(item) {
|
262 |
const existingPopup = document.querySelector('.description-popup');
|
263 |
if (existingPopup) existingPopup.remove();
|
|
|
100 |
|
101 |
const textInput = document.createElement('input');
|
102 |
textInput.type = 'text';
|
103 |
+
textInput.placeholder = 'Search related items...';
|
104 |
textInput.className = 'manual-input';
|
105 |
textInput.addEventListener('keypress', (e) => {
|
106 |
if (e.key === 'Enter' && textInput.value.trim()) {
|
107 |
+
const searchTerm = textInput.value.trim();
|
108 |
+
fetchRelatedImages(searchTerm);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
109 |
textInput.value = '';
|
|
|
110 |
}
|
111 |
});
|
112 |
selectionBox.appendChild(textInput);
|
|
|
141 |
fetchMenuItems(lowerInput);
|
142 |
return;
|
143 |
} else {
|
|
|
144 |
botResponse = `Looking up details for "${userInput}"...`;
|
145 |
addMessage('bot', botResponse);
|
146 |
fetchItemDetailsByName(userInput);
|
|
|
247 |
});
|
248 |
}
|
249 |
|
250 |
+
function fetchRelatedImages(searchTerm) {
|
251 |
+
fetch('/get_menu_items', {
|
252 |
+
method: 'POST',
|
253 |
+
headers: { 'Content-Type': 'application/json' },
|
254 |
+
body: JSON.stringify({ search_term: searchTerm })
|
255 |
+
})
|
256 |
+
.then(response => response.json())
|
257 |
+
.then(data => {
|
258 |
+
if (data.error) {
|
259 |
+
addMessage('bot', `Oops! Trouble finding related items for "${searchTerm}": ${data.error}. Try again!`);
|
260 |
+
} else if (data.menu_items.length > 0) {
|
261 |
+
const imageContainer = document.createElement('div');
|
262 |
+
imageContainer.className = 'image-container';
|
263 |
+
data.menu_items.forEach(item => {
|
264 |
+
if (item.image_url) {
|
265 |
+
const img = document.createElement('img');
|
266 |
+
img.src = item.image_url;
|
267 |
+
img.alt = item.name;
|
268 |
+
img.className = 'related-image';
|
269 |
+
imageContainer.appendChild(img);
|
270 |
+
}
|
271 |
+
});
|
272 |
+
if (imageContainer.children.length > 0) {
|
273 |
+
const chatMessages = document.getElementById('chatMessages');
|
274 |
+
chatMessages.appendChild(imageContainer);
|
275 |
+
chatMessages.scrollTop = chatMessages.scrollHeight;
|
276 |
+
addMessage('bot', `Here are images for items related to "${searchTerm}":`);
|
277 |
+
} else {
|
278 |
+
addMessage('bot', `No images found for items related to "${searchTerm}".`);
|
279 |
+
}
|
280 |
+
} else {
|
281 |
+
addMessage('bot', `No items found related to "${searchTerm}" in Sector_Detail__c.`);
|
282 |
+
}
|
283 |
+
console.log(`Fetched related images for "${searchTerm}":`, data.menu_items);
|
284 |
+
})
|
285 |
+
.catch(error => {
|
286 |
+
addMessage('bot', `Error fetching related images for "${searchTerm}": ${error.message}. I’ll retry in a sec...`);
|
287 |
+
setTimeout(() => fetchRelatedImages(searchTerm), 2000);
|
288 |
+
});
|
289 |
+
}
|
290 |
+
|
291 |
function showDescriptionPopup(item) {
|
292 |
const existingPopup = document.querySelector('.description-popup');
|
293 |
if (existingPopup) existingPopup.remove();
|