Spaces:
Running
Running
Update static/script.js
Browse files- static/script.js +15 -54
static/script.js
CHANGED
@@ -3,7 +3,6 @@ let conversation = [
|
|
3 |
];
|
4 |
let selectedItems = [];
|
5 |
let selectionBoxVisible = false;
|
6 |
-
let suggestionDropdown = null;
|
7 |
|
8 |
function addMessage(role, message) {
|
9 |
const chatMessages = document.getElementById('chatMessages');
|
@@ -99,30 +98,18 @@ function updateSelectionBox() {
|
|
99 |
selectionBox.appendChild(itemContainer);
|
100 |
});
|
101 |
|
102 |
-
const textInputContainer = document.createElement('div');
|
103 |
-
textInputContainer.className = 'text-input-container';
|
104 |
-
|
105 |
const textInput = document.createElement('input');
|
106 |
textInput.type = 'text';
|
107 |
-
textInput.placeholder = '
|
108 |
textInput.className = 'manual-input';
|
109 |
-
textInput.addEventListener('input', () => searchSectorItems(textInput.value));
|
110 |
textInput.addEventListener('keypress', (e) => {
|
111 |
if (e.key === 'Enter' && textInput.value.trim()) {
|
112 |
const itemName = textInput.value.trim();
|
113 |
fetchSectorItemDetails(itemName);
|
114 |
textInput.value = '';
|
115 |
-
removeSuggestions();
|
116 |
}
|
117 |
});
|
118 |
-
|
119 |
-
|
120 |
-
const suggestionBox = document.createElement('div');
|
121 |
-
suggestionBox.className = 'suggestion-box';
|
122 |
-
suggestionBox.id = 'suggestionBox';
|
123 |
-
textInputContainer.appendChild(suggestionBox);
|
124 |
-
|
125 |
-
selectionBox.appendChild(textInputContainer);
|
126 |
|
127 |
if (selectedItems.length > 0) {
|
128 |
const submitButton = document.createElement('button');
|
@@ -154,6 +141,7 @@ function handleResponse(userInput) {
|
|
154 |
fetchMenuItems(lowerInput);
|
155 |
return;
|
156 |
} else {
|
|
|
157 |
botResponse = `Looking up details for "${userInput}"...`;
|
158 |
addMessage('bot', botResponse);
|
159 |
fetchItemDetailsByName(userInput);
|
@@ -186,13 +174,8 @@ function fetchMenuItems(dietaryPreference) {
|
|
186 |
});
|
187 |
}
|
188 |
|
189 |
-
function
|
190 |
-
|
191 |
-
removeSuggestions();
|
192 |
-
return;
|
193 |
-
}
|
194 |
-
|
195 |
-
fetch('/get_menu_items', {
|
196 |
method: 'POST',
|
197 |
headers: { 'Content-Type': 'application/json' },
|
198 |
body: JSON.stringify({ search_term: searchTerm })
|
@@ -200,43 +183,21 @@ function searchSectorItems(searchTerm) {
|
|
200 |
.then(response => response.json())
|
201 |
.then(data => {
|
202 |
if (data.error) {
|
203 |
-
|
204 |
-
|
205 |
-
|
206 |
-
|
207 |
} else {
|
208 |
-
|
209 |
}
|
|
|
210 |
})
|
211 |
.catch(error => {
|
212 |
-
|
213 |
-
|
214 |
});
|
215 |
}
|
216 |
|
217 |
-
function displaySuggestions(items) {
|
218 |
-
const suggestionBox = document.getElementById('suggestionBox');
|
219 |
-
if (!suggestionBox) return;
|
220 |
-
|
221 |
-
suggestionBox.innerHTML = '';
|
222 |
-
items.forEach(item => {
|
223 |
-
const suggestionItem = document.createElement('div');
|
224 |
-
suggestionItem.className = 'suggestion-item';
|
225 |
-
suggestionItem.textContent = item.name;
|
226 |
-
suggestionItem.onclick = () => {
|
227 |
-
fetchSectorItemDetails(item.name);
|
228 |
-
document.querySelector('.manual-input').value = '';
|
229 |
-
removeSuggestions();
|
230 |
-
};
|
231 |
-
suggestionBox.appendChild(suggestionItem);
|
232 |
-
});
|
233 |
-
}
|
234 |
-
|
235 |
-
function removeSuggestions() {
|
236 |
-
const suggestionBox = document.getElementById('suggestionBox');
|
237 |
-
if (suggestionBox) suggestionBox.innerHTML = '';
|
238 |
-
}
|
239 |
-
|
240 |
function fetchItemDetails(itemName) {
|
241 |
fetch('/get_item_details', {
|
242 |
method: 'POST',
|
@@ -457,8 +418,8 @@ function submitToSalesforce() {
|
|
457 |
|
458 |
const itemsToSubmit = selectedItems.map(item => ({
|
459 |
name: item.name,
|
460 |
-
category: item.category || 'Unknown',
|
461 |
-
description: item.description || 'No description available'
|
462 |
}));
|
463 |
|
464 |
fetch('/submit_items', {
|
|
|
3 |
];
|
4 |
let selectedItems = [];
|
5 |
let selectionBoxVisible = false;
|
|
|
6 |
|
7 |
function addMessage(role, message) {
|
8 |
const chatMessages = document.getElementById('chatMessages');
|
|
|
98 |
selectionBox.appendChild(itemContainer);
|
99 |
});
|
100 |
|
|
|
|
|
|
|
101 |
const textInput = document.createElement('input');
|
102 |
textInput.type = 'text';
|
103 |
+
textInput.placeholder = 'Add Sector_Detail__c item...';
|
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 |
fetchSectorItemDetails(itemName);
|
109 |
textInput.value = '';
|
|
|
110 |
}
|
111 |
});
|
112 |
+
selectionBox.appendChild(textInput);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
113 |
|
114 |
if (selectedItems.length > 0) {
|
115 |
const submitButton = document.createElement('button');
|
|
|
141 |
fetchMenuItems(lowerInput);
|
142 |
return;
|
143 |
} else {
|
144 |
+
// Treat input as an item name and fetch details
|
145 |
botResponse = `Looking up details for "${userInput}"...`;
|
146 |
addMessage('bot', botResponse);
|
147 |
fetchItemDetailsByName(userInput);
|
|
|
174 |
});
|
175 |
}
|
176 |
|
177 |
+
function suggestItems(searchTerm) {
|
178 |
+
fetch('/suggest_items', {
|
|
|
|
|
|
|
|
|
|
|
179 |
method: 'POST',
|
180 |
headers: { 'Content-Type': 'application/json' },
|
181 |
body: JSON.stringify({ search_term: searchTerm })
|
|
|
183 |
.then(response => response.json())
|
184 |
.then(data => {
|
185 |
if (data.error) {
|
186 |
+
addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Got another idea?`);
|
187 |
+
} else if (data.suggestions.length > 0) {
|
188 |
+
addMessage('bot', `Check out these suggestions for "${searchTerm}" from Ingredient_Object__c:`);
|
189 |
+
displayItemsList(data.suggestions, 'suggestionsList', false);
|
190 |
} else {
|
191 |
+
addMessage('bot', `No matches for "${searchTerm}" in Ingredient_Object__c. Try "chicken" or "paneer"?`);
|
192 |
}
|
193 |
+
console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
|
194 |
})
|
195 |
.catch(error => {
|
196 |
+
addMessage('bot', `Error fetching suggestions: ${error.message}. Retrying...`);
|
197 |
+
setTimeout(() => suggestItems(searchTerm), 2000);
|
198 |
});
|
199 |
}
|
200 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
201 |
function fetchItemDetails(itemName) {
|
202 |
fetch('/get_item_details', {
|
203 |
method: 'POST',
|
|
|
418 |
|
419 |
const itemsToSubmit = selectedItems.map(item => ({
|
420 |
name: item.name,
|
421 |
+
category: item.category || 'Unknown', // Include Category__c
|
422 |
+
description: item.description || 'No description available' // Ensure description
|
423 |
}));
|
424 |
|
425 |
fetch('/submit_items', {
|