Spaces:
Running
Running
Update static/script.js
Browse files- static/script.js +19 -26
static/script.js
CHANGED
@@ -73,7 +73,7 @@ function fetchMenuItems(dietaryPreference) {
|
|
73 |
} else if (data.menu_items.length > 0) {
|
74 |
addMessage('bot', `Here are some ${dietaryPreference} menu items:`);
|
75 |
displayItemsList(data.menu_items, 'menuItemsList');
|
76 |
-
displaySubmitButton();
|
77 |
} else {
|
78 |
addMessage('bot', `No ${dietaryPreference} items found. Try searching for something specific!`);
|
79 |
}
|
@@ -98,7 +98,7 @@ function suggestItems(searchTerm) {
|
|
98 |
} else if (data.suggestions.length > 0) {
|
99 |
addMessage('bot', `Found these suggestions for "${searchTerm}":`);
|
100 |
displayItemsList(data.suggestions, 'suggestionsList');
|
101 |
-
displaySubmitButton();
|
102 |
} else {
|
103 |
addMessage('bot', `No matches for "${searchTerm}". Try "chicken" or "paneer"?`);
|
104 |
}
|
@@ -122,7 +122,7 @@ function fetchItemDetails(itemName) {
|
|
122 |
addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Check the name!`);
|
123 |
} else {
|
124 |
const details = data.item_details;
|
125 |
-
selectedItems.push(details
|
126 |
addMessage('bot', `Added "${itemName}" to your selection!`);
|
127 |
displaySelectedItems();
|
128 |
displaySubmitButton();
|
@@ -178,7 +178,8 @@ function displaySelectedItems() {
|
|
178 |
} else {
|
179 |
selectedItems.forEach(item => {
|
180 |
const div = document.createElement('div');
|
181 |
-
div.textContent = item; // Display
|
|
|
182 |
selectedArea.appendChild(div);
|
183 |
});
|
184 |
}
|
@@ -221,49 +222,41 @@ function displaySubmitButton() {
|
|
221 |
if (!chatMessages) return;
|
222 |
|
223 |
// Remove existing submit button if any
|
224 |
-
const existingButton = document.
|
225 |
if (existingButton) existingButton.remove();
|
226 |
|
227 |
-
|
228 |
-
|
229 |
-
|
230 |
-
|
231 |
-
|
232 |
-
submitButton.style.marginTop = '10px';
|
233 |
-
submitButton.onclick = submitIngredients;
|
234 |
-
chatMessages.appendChild(submitButton);
|
235 |
-
}
|
236 |
}
|
237 |
|
238 |
-
function
|
239 |
if (selectedItems.length === 0) {
|
240 |
addMessage('bot', 'No items selected to submit!');
|
241 |
return;
|
242 |
}
|
243 |
|
244 |
-
fetch('/
|
245 |
method: 'POST',
|
246 |
headers: { 'Content-Type': 'application/json' },
|
247 |
-
body: JSON.stringify({
|
248 |
})
|
249 |
.then(response => response.json())
|
250 |
.then(data => {
|
251 |
if (data.error) {
|
252 |
-
addMessage('bot', `Failed to submit: ${data.error}`);
|
253 |
} else {
|
254 |
-
addMessage('bot',
|
255 |
selectedItems = [];
|
256 |
displaySelectedItems();
|
257 |
-
|
258 |
-
{ text: 'Vegetarian', class: 'green' },
|
259 |
-
{ text: 'Non-Vegetarian', class: 'red' },
|
260 |
-
{ text: 'Both', class: 'gray' }
|
261 |
-
]);
|
262 |
}
|
263 |
})
|
264 |
.catch(error => {
|
265 |
-
addMessage('bot', `Error submitting
|
266 |
-
setTimeout(
|
267 |
});
|
268 |
}
|
269 |
|
|
|
73 |
} else if (data.menu_items.length > 0) {
|
74 |
addMessage('bot', `Here are some ${dietaryPreference} menu items:`);
|
75 |
displayItemsList(data.menu_items, 'menuItemsList');
|
76 |
+
if (selectedItems.length > 0) displaySubmitButton();
|
77 |
} else {
|
78 |
addMessage('bot', `No ${dietaryPreference} items found. Try searching for something specific!`);
|
79 |
}
|
|
|
98 |
} else if (data.suggestions.length > 0) {
|
99 |
addMessage('bot', `Found these suggestions for "${searchTerm}":`);
|
100 |
displayItemsList(data.suggestions, 'suggestionsList');
|
101 |
+
if (selectedItems.length > 0) displaySubmitButton();
|
102 |
} else {
|
103 |
addMessage('bot', `No matches for "${searchTerm}". Try "chicken" or "paneer"?`);
|
104 |
}
|
|
|
122 |
addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Check the name!`);
|
123 |
} else {
|
124 |
const details = data.item_details;
|
125 |
+
selectedItems.push(details);
|
126 |
addMessage('bot', `Added "${itemName}" to your selection!`);
|
127 |
displaySelectedItems();
|
128 |
displaySubmitButton();
|
|
|
178 |
} else {
|
179 |
selectedItems.forEach(item => {
|
180 |
const div = document.createElement('div');
|
181 |
+
div.textContent = item.name; // Display as text label
|
182 |
+
div.style.margin = '5px 0';
|
183 |
selectedArea.appendChild(div);
|
184 |
});
|
185 |
}
|
|
|
222 |
if (!chatMessages) return;
|
223 |
|
224 |
// Remove existing submit button if any
|
225 |
+
const existingButton = document.querySelector('.submit-button');
|
226 |
if (existingButton) existingButton.remove();
|
227 |
|
228 |
+
const submitButton = document.createElement('button');
|
229 |
+
submitButton.textContent = 'Submit to Salesforce';
|
230 |
+
submitButton.className = 'submit-button';
|
231 |
+
submitButton.onclick = submitToSalesforce;
|
232 |
+
chatMessages.appendChild(submitButton);
|
|
|
|
|
|
|
|
|
233 |
}
|
234 |
|
235 |
+
function submitToSalesforce() {
|
236 |
if (selectedItems.length === 0) {
|
237 |
addMessage('bot', 'No items selected to submit!');
|
238 |
return;
|
239 |
}
|
240 |
|
241 |
+
fetch('/submit_items', {
|
242 |
method: 'POST',
|
243 |
headers: { 'Content-Type': 'application/json' },
|
244 |
+
body: JSON.stringify({ items: selectedItems })
|
245 |
})
|
246 |
.then(response => response.json())
|
247 |
.then(data => {
|
248 |
if (data.error) {
|
249 |
+
addMessage('bot', `Failed to submit items: ${data.error}`);
|
250 |
} else {
|
251 |
+
addMessage('bot', data.success);
|
252 |
selectedItems = [];
|
253 |
displaySelectedItems();
|
254 |
+
document.querySelector('.submit-button').remove();
|
|
|
|
|
|
|
|
|
255 |
}
|
256 |
})
|
257 |
.catch(error => {
|
258 |
+
addMessage('bot', `Error submitting items: ${error.message}. Retrying...`);
|
259 |
+
setTimeout(submitToSalesforce, 2000);
|
260 |
});
|
261 |
}
|
262 |
|