Spaces:
Sleeping
Sleeping
Update static/script.js
Browse files- static/script.js +21 -357
static/script.js
CHANGED
@@ -2,6 +2,7 @@ let conversation = [
|
|
2 |
{ role: 'bot', message: 'Hello! I’m Chef Bot, your culinary assistant! What’s your name?' }
|
3 |
];
|
4 |
let selectedItems = [];
|
|
|
5 |
|
6 |
function addMessage(role, message) {
|
7 |
const chatMessages = document.getElementById('chatMessages');
|
@@ -27,363 +28,24 @@ function sendMessage() {
|
|
27 |
if (message) {
|
28 |
addMessage('user', message);
|
29 |
conversation.push({ role: 'user', message: message });
|
|
|
30 |
setTimeout(() => handleResponse(message), 500);
|
31 |
} else {
|
32 |
addMessage('bot', 'Hey, don’t be shy! Type something or add some items to get started! 😄');
|
33 |
}
|
34 |
-
userInput.value = '';
|
35 |
-
}
|
36 |
-
|
37 |
-
function updateSelectionBox() {
|
38 |
-
const chatMessages = document.getElementById('chatMessages');
|
39 |
-
if (!chatMessages) return;
|
40 |
-
|
41 |
-
// Remove existing selection box if it exists
|
42 |
-
const existingBox = document.querySelector('.selection-box');
|
43 |
-
if (existingBox) existingBox.remove();
|
44 |
-
|
45 |
-
const selectionBox = document.createElement('div');
|
46 |
-
selectionBox.className = 'selection-box';
|
47 |
-
|
48 |
-
// Add dietary preference buttons
|
49 |
-
const vegButton = document.createElement('button');
|
50 |
-
vegButton.textContent = 'Vegetarian';
|
51 |
-
vegButton.className = 'dietary-button green';
|
52 |
-
vegButton.onclick = () => fetchMenuItems('vegetarian');
|
53 |
-
selectionBox.appendChild(vegButton);
|
54 |
-
|
55 |
-
const nonVegButton = document.createElement('button');
|
56 |
-
nonVegButton.textContent = 'Non-Vegetarian';
|
57 |
-
nonVegButton.className = 'dietary-button red';
|
58 |
-
nonVegButton.onclick = () => fetchMenuItems('non-vegetarian');
|
59 |
-
selectionBox.appendChild(nonVegButton);
|
60 |
-
|
61 |
-
const bothButton = document.createElement('button');
|
62 |
-
bothButton.textContent = 'Both';
|
63 |
-
bothButton.className = 'dietary-button gray';
|
64 |
-
bothButton.onclick = () => fetchMenuItems('both');
|
65 |
-
selectionBox.appendChild(bothButton);
|
66 |
-
|
67 |
-
// Add selected items label and list
|
68 |
-
const label = document.createElement('span');
|
69 |
-
label.textContent = 'Selected Items:';
|
70 |
-
selectionBox.appendChild(label);
|
71 |
-
|
72 |
-
selectedItems.forEach(item => {
|
73 |
-
const itemSpan = document.createElement('span');
|
74 |
-
itemSpan.textContent = item.name;
|
75 |
-
selectionBox.appendChild(itemSpan);
|
76 |
-
});
|
77 |
-
|
78 |
-
// Add text input for manual item entry
|
79 |
-
const textInput = document.createElement('input');
|
80 |
-
textInput.type = 'text';
|
81 |
-
textInput.placeholder = 'Add item manually...';
|
82 |
-
textInput.className = 'manual-input';
|
83 |
-
textInput.addEventListener('keypress', (e) => {
|
84 |
-
if (e.key === 'Enter' && textInput.value.trim()) {
|
85 |
-
const manualItem = {
|
86 |
-
name: textInput.value.trim(),
|
87 |
-
description: 'Manually added',
|
88 |
-
ingredients: 'Not specified',
|
89 |
-
image_url: ''
|
90 |
-
};
|
91 |
-
selectedItems.push(manualItem);
|
92 |
-
addMessage('bot', `Added "${manualItem.name}" to your selection! Check the box below.`);
|
93 |
-
textInput.value = '';
|
94 |
-
updateSelectionBox();
|
95 |
-
}
|
96 |
-
});
|
97 |
-
selectionBox.appendChild(textInput);
|
98 |
-
|
99 |
-
// Add Submit button if there are selected items
|
100 |
-
if (selectedItems.length > 0) {
|
101 |
-
const submitButton = document.createElement('button');
|
102 |
-
submitButton.textContent = 'Submit to Salesforce';
|
103 |
-
submitButton.className = 'submit-button';
|
104 |
-
submitButton.onclick = submitToSalesforce;
|
105 |
-
selectionBox.appendChild(submitButton);
|
106 |
-
}
|
107 |
-
|
108 |
-
chatMessages.appendChild(selectionBox);
|
109 |
-
chatMessages.scrollTop = chatMessages.scrollHeight;
|
110 |
-
console.log('Updated selection box with items:', selectedItems.map(item => item.name));
|
111 |
-
}
|
112 |
-
|
113 |
-
function handleResponse(userInput) {
|
114 |
-
const lowerInput = userInput.toLowerCase();
|
115 |
-
let botResponse = '';
|
116 |
-
|
117 |
-
if (conversation.length === 2) {
|
118 |
-
botResponse = `Great to meet you, ${userInput}! 🍳 What kind of food are you craving today?`;
|
119 |
-
displayOptions([
|
120 |
-
{ text: 'Vegetarian', class: 'green' },
|
121 |
-
{ text: 'Non-Vegetarian', class: 'red' },
|
122 |
-
{ text: 'Both', class: 'gray' }
|
123 |
-
]);
|
124 |
-
} else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
|
125 |
-
botResponse = `Nice choice! Let me whip up some ${lowerInput} options for you...`;
|
126 |
-
addMessage('bot', botResponse);
|
127 |
-
fetchMenuItems(lowerInput);
|
128 |
-
return;
|
129 |
-
} else {
|
130 |
-
botResponse = `Hmm, let’s see what I can find for "${userInput}"...`;
|
131 |
-
addMessage('bot', botResponse);
|
132 |
-
suggestItems(userInput);
|
133 |
-
return;
|
134 |
-
}
|
135 |
-
|
136 |
-
addMessage('bot', botResponse);
|
137 |
-
}
|
138 |
-
|
139 |
-
function fetchMenuItems(dietaryPreference) {
|
140 |
-
fetch('/get_menu_items', {
|
141 |
-
method: 'POST',
|
142 |
-
headers: { 'Content-Type': 'application/json' },
|
143 |
-
body: JSON.stringify({ dietary_preference: dietaryPreference })
|
144 |
-
})
|
145 |
-
.then(response => response.json())
|
146 |
-
.then(data => {
|
147 |
-
if (data.error) {
|
148 |
-
addMessage('bot', `Oops! Trouble fetching ${dietaryPreference} items: ${data.error}. Want to try again or search something specific?`);
|
149 |
-
} else if (data.menu_items.length > 0) {
|
150 |
-
displayItemsList(data.menu_items, 'menuItemsList', true); // Pass true to indicate Sector_Detail__c items
|
151 |
-
} else {
|
152 |
-
addMessage('bot', `Looks like we’re out of ${dietaryPreference} items in Sector_Detail__c. Try searching for something like "paneer" or "chicken"!`);
|
153 |
-
}
|
154 |
-
console.log(`Fetched items from Sector_Detail__c for ${dietaryPreference}:`, data.menu_items);
|
155 |
-
})
|
156 |
-
.catch(error => {
|
157 |
-
addMessage('bot', `Yikes! Couldn’t reach Salesforce: ${error.message}. I’ll retry in a sec...`);
|
158 |
-
setTimeout(() => fetchMenuItems(dietaryPreference), 2000);
|
159 |
-
});
|
160 |
-
}
|
161 |
-
|
162 |
-
function suggestItems(searchTerm) {
|
163 |
-
fetch('/suggest_items', {
|
164 |
-
method: 'POST',
|
165 |
-
headers: { 'Content-Type': 'application/json' },
|
166 |
-
body: JSON.stringify({ search_term: searchTerm })
|
167 |
-
})
|
168 |
-
.then(response => response.json())
|
169 |
-
.then(data => {
|
170 |
-
if (data.error) {
|
171 |
-
addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Got another idea?`);
|
172 |
-
} else if (data.suggestions.length > 0) {
|
173 |
-
addMessage('bot', `Check out these suggestions for "${searchTerm}" from Ingredient_Object__c:`);
|
174 |
-
displayItemsList(data.suggestions, 'suggestionsList', false); // Pass false for Ingredient_Object__c items
|
175 |
-
} else {
|
176 |
-
addMessage('bot', `No luck with "${searchTerm}" in Ingredient_Object__c. How about "chicken" or "paneer"?`);
|
177 |
-
}
|
178 |
-
console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
|
179 |
-
})
|
180 |
-
.catch(error => {
|
181 |
-
addMessage('bot', `Error fetching suggestions: ${error.message}. Retrying...`);
|
182 |
-
setTimeout(() => suggestItems(searchTerm), 2000);
|
183 |
-
});
|
184 |
-
}
|
185 |
-
|
186 |
-
function fetchItemDetails(itemName) {
|
187 |
-
fetch('/get_item_details', {
|
188 |
-
method: 'POST',
|
189 |
-
headers: { 'Content-Type': 'application/json' },
|
190 |
-
body: JSON.stringify({ item_name: itemName })
|
191 |
-
})
|
192 |
-
.then(response => response.json())
|
193 |
-
.then(data => {
|
194 |
-
if (data.error) {
|
195 |
-
addMessage('bot', `Couldn’t find "${itemName}" in Salesforce. Add it manually in the box below!`);
|
196 |
-
updateSelectionBox();
|
197 |
-
} else {
|
198 |
-
const details = data.item_details;
|
199 |
-
selectedItems.push(details);
|
200 |
-
addMessage('bot', `Added "${itemName}" to your selection! Check the box below.`);
|
201 |
-
updateSelectionBox();
|
202 |
-
console.log(`Details for ${itemName}:`, details);
|
203 |
-
}
|
204 |
-
})
|
205 |
-
.catch(error => {
|
206 |
-
addMessage('bot', `Trouble fetching details for "${itemName}": ${error.message}. Add it manually below or I’ll retry...`);
|
207 |
-
updateSelectionBox();
|
208 |
-
setTimeout(() => fetchItemDetails(itemName), 2000);
|
209 |
-
});
|
210 |
-
}
|
211 |
-
|
212 |
-
function displayItemsList(items, containerId, isSectorDetail = false) {
|
213 |
-
const container = document.getElementById(containerId);
|
214 |
-
if (!container) {
|
215 |
-
console.error(`${containerId} container not found!`);
|
216 |
-
addMessage('bot', 'Something’s off with the display. Try again?');
|
217 |
-
return;
|
218 |
-
}
|
219 |
-
container.innerHTML = '';
|
220 |
-
|
221 |
-
items.forEach(item => {
|
222 |
-
const itemDiv = document.createElement('div');
|
223 |
-
itemDiv.className = containerId === 'menuItemsList' ? 'ingredient-item' : 'suggestion-item';
|
224 |
-
const img = document.createElement('img');
|
225 |
-
img.src = item.image_url || 'https://via.placeholder.com/60';
|
226 |
-
img.alt = item.name;
|
227 |
-
const name = document.createElement('div');
|
228 |
-
name.textContent = item.name;
|
229 |
-
name.style.textAlign = 'center';
|
230 |
-
name.style.marginTop = '5px';
|
231 |
-
name.style.fontSize = '12px';
|
232 |
-
const button = document.createElement('button');
|
233 |
-
button.textContent = 'Add';
|
234 |
-
button.onclick = () => {
|
235 |
-
if (isSectorDetail) {
|
236 |
-
// Directly use item data from Sector_Detail__c
|
237 |
-
const sectorItem = {
|
238 |
-
name: item.name,
|
239 |
-
description: 'From Sector_Detail__c',
|
240 |
-
ingredients: 'Not specified',
|
241 |
-
image_url: item.image_url || '',
|
242 |
-
category: item.category || 'Both'
|
243 |
-
};
|
244 |
-
selectedItems.push(sectorItem);
|
245 |
-
addMessage('bot', `Added "${item.name}" to your selection! Check the box below.`);
|
246 |
-
updateSelectionBox();
|
247 |
-
} else {
|
248 |
-
// Fetch details from Ingredient_Object__c for suggestions
|
249 |
-
fetchItemDetails(item.name);
|
250 |
-
}
|
251 |
-
};
|
252 |
-
itemDiv.appendChild(img);
|
253 |
-
itemDiv.appendChild(name);
|
254 |
-
itemDiv.appendChild(button);
|
255 |
-
container.appendChild(itemDiv);
|
256 |
-
});
|
257 |
-
}
|
258 |
-
|
259 |
-
function displayOptions(options) {
|
260 |
-
const chatMessages = document.getElementById('chatMessages');
|
261 |
-
if (!chatMessages) {
|
262 |
-
console.error('Chat messages container not found for options!');
|
263 |
-
return;
|
264 |
-
}
|
265 |
-
const optionsDiv = document.createElement('div');
|
266 |
-
optionsDiv.style.marginTop = '10px';
|
267 |
-
|
268 |
-
options.forEach(opt => {
|
269 |
-
const button = document.createElement('button');
|
270 |
-
button.textContent = opt.text;
|
271 |
-
button.className = `option-button ${opt.class}`;
|
272 |
-
button.onclick = () => {
|
273 |
-
addMessage('user', opt.text);
|
274 |
-
conversation.push({ role: 'user', message: opt.text });
|
275 |
-
handleResponse(opt.text);
|
276 |
-
};
|
277 |
-
optionsDiv.appendChild(button);
|
278 |
-
});
|
279 |
-
|
280 |
-
const backButton = document.createElement('button');
|
281 |
-
backButton.textContent = 'Go Back';
|
282 |
-
backButton.className = 'option-button';
|
283 |
-
backButton.style.marginTop = '10px';
|
284 |
-
backButton.onclick = () => resetConversation();
|
285 |
-
optionsDiv.appendChild(document.createElement('br'));
|
286 |
-
optionsDiv.appendChild(backButton);
|
287 |
-
|
288 |
-
chatMessages.appendChild(optionsDiv);
|
289 |
-
}
|
290 |
-
|
291 |
-
function submitToSalesforce() {
|
292 |
-
if (selectedItems.length === 0) {
|
293 |
-
addMessage('bot', 'No items to submit yet! Add some tasty picks first! 😊');
|
294 |
-
return;
|
295 |
-
}
|
296 |
-
|
297 |
-
fetch('/submit_items', {
|
298 |
-
method: 'POST',
|
299 |
-
headers: { 'Content-Type': 'application/json' },
|
300 |
-
body: JSON.stringify({ items: selectedItems })
|
301 |
-
})
|
302 |
-
.then(response => response.json())
|
303 |
-
.then(data => {
|
304 |
-
if (data.error) {
|
305 |
-
addMessage('bot', `Uh-oh! Failed to submit items: ${data.error}. Want to try again?`);
|
306 |
-
} else {
|
307 |
-
addMessage('bot', `${data.success}! Your culinary choices are now saved. What’s next on the menu?`);
|
308 |
-
selectedItems = [];
|
309 |
-
updateSelectionBox();
|
310 |
-
}
|
311 |
-
})
|
312 |
-
.catch(error => {
|
313 |
-
addMessage('bot', `Error submitting items: ${error.message}. I’ll retry shortly...`);
|
314 |
-
setTimeout(submitToSalesforce, 2000);
|
315 |
-
});
|
316 |
-
}
|
317 |
-
|
318 |
-
function resetConversation() {
|
319 |
-
const userName = conversation.length > 1 ? conversation[1].message : 'Friend';
|
320 |
-
conversation = [
|
321 |
-
{ role: 'bot', message: `Hello! I’m Chef Bot, your culinary assistant! What’s your name?` },
|
322 |
-
{ role: 'user', message: userName },
|
323 |
-
{ role: 'bot', message: `Great to meet you, ${userName}! 🍳 What kind of food are you craving today?` }
|
324 |
-
];
|
325 |
-
selectedItems = [];
|
326 |
-
const chatMessages = document.getElementById('chatMessages');
|
327 |
-
chatMessages.innerHTML = '';
|
328 |
-
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
329 |
-
displayOptions([
|
330 |
-
{ text: 'Vegetarian', class: 'green' },
|
331 |
-
{ text: 'Non-Vegetarian', class: 'red' },
|
332 |
-
{ text: 'Both', class: 'gray' }
|
333 |
-
]);
|
334 |
-
document.getElementById('suggestionsList').innerHTML = '';
|
335 |
-
document.getElementById('menuItemsList').innerHTML = '';
|
336 |
updateSelectionBox();
|
337 |
}
|
338 |
|
339 |
-
document.getElementById('userInput').addEventListener('keypress', (e) => {
|
340 |
-
if (e.key === 'Enter') sendMessage();
|
341 |
-
});
|
342 |
-
|
343 |
-
console.log('Chef Bot script loaded successfully!');let conversation = [
|
344 |
-
{ role: 'bot', message: 'Hello! I’m Chef Bot, your culinary assistant! What’s your name?' }
|
345 |
-
];
|
346 |
-
let selectedItems = [];
|
347 |
-
|
348 |
-
function addMessage(role, message) {
|
349 |
-
const chatMessages = document.getElementById('chatMessages');
|
350 |
-
if (!chatMessages) {
|
351 |
-
console.error('Chat messages container not found!');
|
352 |
-
return;
|
353 |
-
}
|
354 |
-
const messageDiv = document.createElement('div');
|
355 |
-
messageDiv.className = role === 'bot' ? 'bot-message' : 'user-message';
|
356 |
-
messageDiv.textContent = message;
|
357 |
-
chatMessages.appendChild(messageDiv);
|
358 |
-
chatMessages.scrollTop = chatMessages.scrollHeight;
|
359 |
-
console.log(`Added ${role} message: ${message}`);
|
360 |
-
}
|
361 |
-
|
362 |
-
function sendMessage() {
|
363 |
-
const userInput = document.getElementById('userInput');
|
364 |
-
if (!userInput) {
|
365 |
-
console.error('User input field not found!');
|
366 |
-
return;
|
367 |
-
}
|
368 |
-
const message = userInput.value.trim();
|
369 |
-
if (message) {
|
370 |
-
addMessage('user', message);
|
371 |
-
conversation.push({ role: 'user', message: message });
|
372 |
-
setTimeout(() => handleResponse(message), 500);
|
373 |
-
} else {
|
374 |
-
addMessage('bot', 'Hey, don’t be shy! Type something or add some items to get started! 😄');
|
375 |
-
}
|
376 |
-
userInput.value = ''; // Clear input after sending
|
377 |
-
}
|
378 |
-
|
379 |
function updateSelectionBox() {
|
380 |
const chatMessages = document.getElementById('chatMessages');
|
381 |
if (!chatMessages) return;
|
382 |
|
383 |
-
// Remove existing selection box if it exists
|
384 |
const existingBox = document.querySelector('.selection-box');
|
385 |
if (existingBox) existingBox.remove();
|
386 |
|
|
|
|
|
387 |
const selectionBox = document.createElement('div');
|
388 |
selectionBox.className = 'selection-box';
|
389 |
|
@@ -406,7 +68,7 @@ function updateSelectionBox() {
|
|
406 |
bothButton.onclick = () => fetchMenuItems('both');
|
407 |
selectionBox.appendChild(bothButton);
|
408 |
|
409 |
-
// Add selected items
|
410 |
const label = document.createElement('span');
|
411 |
label.textContent = 'Selected Items:';
|
412 |
selectionBox.appendChild(label);
|
@@ -417,7 +79,7 @@ function updateSelectionBox() {
|
|
417 |
selectionBox.appendChild(itemSpan);
|
418 |
});
|
419 |
|
420 |
-
// Add
|
421 |
const textInput = document.createElement('input');
|
422 |
textInput.type = 'text';
|
423 |
textInput.placeholder = 'Add item manually...';
|
@@ -428,7 +90,8 @@ function updateSelectionBox() {
|
|
428 |
name: textInput.value.trim(),
|
429 |
description: 'Manually added',
|
430 |
ingredients: 'Not specified',
|
431 |
-
image_url: ''
|
|
|
432 |
};
|
433 |
selectedItems.push(manualItem);
|
434 |
addMessage('bot', `Added "${manualItem.name}" to your selection! Check the box below.`);
|
@@ -438,7 +101,7 @@ function updateSelectionBox() {
|
|
438 |
});
|
439 |
selectionBox.appendChild(textInput);
|
440 |
|
441 |
-
// Add Submit button if
|
442 |
if (selectedItems.length > 0) {
|
443 |
const submitButton = document.createElement('button');
|
444 |
submitButton.textContent = 'Submit to Salesforce';
|
@@ -487,11 +150,11 @@ function fetchMenuItems(dietaryPreference) {
|
|
487 |
.then(response => response.json())
|
488 |
.then(data => {
|
489 |
if (data.error) {
|
490 |
-
addMessage('bot', `Oops! Trouble fetching ${dietaryPreference} items: ${data.error}.
|
491 |
} else if (data.menu_items.length > 0) {
|
492 |
-
displayItemsList(data.menu_items, 'menuItemsList', true);
|
493 |
} else {
|
494 |
-
addMessage('bot', `
|
495 |
}
|
496 |
console.log(`Fetched items from Sector_Detail__c for ${dietaryPreference}:`, data.menu_items);
|
497 |
})
|
@@ -513,9 +176,9 @@ function suggestItems(searchTerm) {
|
|
513 |
addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Got another idea?`);
|
514 |
} else if (data.suggestions.length > 0) {
|
515 |
addMessage('bot', `Check out these suggestions for "${searchTerm}" from Ingredient_Object__c:`);
|
516 |
-
displayItemsList(data.suggestions, 'suggestionsList', false);
|
517 |
} else {
|
518 |
-
addMessage('bot', `No
|
519 |
}
|
520 |
console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
|
521 |
})
|
@@ -534,7 +197,7 @@ function fetchItemDetails(itemName) {
|
|
534 |
.then(response => response.json())
|
535 |
.then(data => {
|
536 |
if (data.error) {
|
537 |
-
addMessage('bot', `Couldn’t find "${itemName}" in
|
538 |
updateSelectionBox();
|
539 |
} else {
|
540 |
const details = data.item_details;
|
@@ -545,7 +208,7 @@ function fetchItemDetails(itemName) {
|
|
545 |
}
|
546 |
})
|
547 |
.catch(error => {
|
548 |
-
addMessage('bot', `Trouble fetching
|
549 |
updateSelectionBox();
|
550 |
setTimeout(() => fetchItemDetails(itemName), 2000);
|
551 |
});
|
@@ -575,7 +238,6 @@ function displayItemsList(items, containerId, isSectorDetail = false) {
|
|
575 |
button.textContent = 'Add';
|
576 |
button.onclick = () => {
|
577 |
if (isSectorDetail) {
|
578 |
-
// Directly use item data from Sector_Detail__c
|
579 |
const sectorItem = {
|
580 |
name: item.name,
|
581 |
description: 'From Sector_Detail__c',
|
@@ -585,11 +247,10 @@ function displayItemsList(items, containerId, isSectorDetail = false) {
|
|
585 |
};
|
586 |
selectedItems.push(sectorItem);
|
587 |
addMessage('bot', `Added "${item.name}" to your selection! Check the box below.`);
|
588 |
-
updateSelectionBox();
|
589 |
} else {
|
590 |
-
// Fetch details from Ingredient_Object__c for suggestions
|
591 |
fetchItemDetails(item.name);
|
592 |
}
|
|
|
593 |
};
|
594 |
itemDiv.appendChild(img);
|
595 |
itemDiv.appendChild(name);
|
@@ -614,7 +275,9 @@ function displayOptions(options) {
|
|
614 |
button.onclick = () => {
|
615 |
addMessage('user', opt.text);
|
616 |
conversation.push({ role: 'user', message: opt.text });
|
|
|
617 |
handleResponse(opt.text);
|
|
|
618 |
};
|
619 |
optionsDiv.appendChild(button);
|
620 |
});
|
@@ -665,6 +328,7 @@ function resetConversation() {
|
|
665 |
{ role: 'bot', message: `Great to meet you, ${userName}! 🍳 What kind of food are you craving today?` }
|
666 |
];
|
667 |
selectedItems = [];
|
|
|
668 |
const chatMessages = document.getElementById('chatMessages');
|
669 |
chatMessages.innerHTML = '';
|
670 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|
|
|
2 |
{ role: 'bot', message: 'Hello! I’m Chef Bot, your culinary assistant! What’s your name?' }
|
3 |
];
|
4 |
let selectedItems = [];
|
5 |
+
let selectionBoxVisible = false;
|
6 |
|
7 |
function addMessage(role, message) {
|
8 |
const chatMessages = document.getElementById('chatMessages');
|
|
|
28 |
if (message) {
|
29 |
addMessage('user', message);
|
30 |
conversation.push({ role: 'user', message: message });
|
31 |
+
selectionBoxVisible = true; // Show selection box after first user interaction
|
32 |
setTimeout(() => handleResponse(message), 500);
|
33 |
} else {
|
34 |
addMessage('bot', 'Hey, don’t be shy! Type something or add some items to get started! 😄');
|
35 |
}
|
36 |
+
userInput.value = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
37 |
updateSelectionBox();
|
38 |
}
|
39 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
function updateSelectionBox() {
|
41 |
const chatMessages = document.getElementById('chatMessages');
|
42 |
if (!chatMessages) return;
|
43 |
|
|
|
44 |
const existingBox = document.querySelector('.selection-box');
|
45 |
if (existingBox) existingBox.remove();
|
46 |
|
47 |
+
if (!selectionBoxVisible && selectedItems.length === 0) return;
|
48 |
+
|
49 |
const selectionBox = document.createElement('div');
|
50 |
selectionBox.className = 'selection-box';
|
51 |
|
|
|
68 |
bothButton.onclick = () => fetchMenuItems('both');
|
69 |
selectionBox.appendChild(bothButton);
|
70 |
|
71 |
+
// Add selected items
|
72 |
const label = document.createElement('span');
|
73 |
label.textContent = 'Selected Items:';
|
74 |
selectionBox.appendChild(label);
|
|
|
79 |
selectionBox.appendChild(itemSpan);
|
80 |
});
|
81 |
|
82 |
+
// Add manual input
|
83 |
const textInput = document.createElement('input');
|
84 |
textInput.type = 'text';
|
85 |
textInput.placeholder = 'Add item manually...';
|
|
|
90 |
name: textInput.value.trim(),
|
91 |
description: 'Manually added',
|
92 |
ingredients: 'Not specified',
|
93 |
+
image_url: '',
|
94 |
+
category: 'Both'
|
95 |
};
|
96 |
selectedItems.push(manualItem);
|
97 |
addMessage('bot', `Added "${manualItem.name}" to your selection! Check the box below.`);
|
|
|
101 |
});
|
102 |
selectionBox.appendChild(textInput);
|
103 |
|
104 |
+
// Add Submit button if items exist
|
105 |
if (selectedItems.length > 0) {
|
106 |
const submitButton = document.createElement('button');
|
107 |
submitButton.textContent = 'Submit to Salesforce';
|
|
|
150 |
.then(response => response.json())
|
151 |
.then(data => {
|
152 |
if (data.error) {
|
153 |
+
addMessage('bot', `Oops! Trouble fetching ${dietaryPreference} items: ${data.error}. Try again or search something specific.`);
|
154 |
} else if (data.menu_items.length > 0) {
|
155 |
+
displayItemsList(data.menu_items, 'menuItemsList', true);
|
156 |
} else {
|
157 |
+
addMessage('bot', `No ${dietaryPreference} items found in Sector_Detail__c. Try searching for something like "paneer" or "chicken"!`);
|
158 |
}
|
159 |
console.log(`Fetched items from Sector_Detail__c for ${dietaryPreference}:`, data.menu_items);
|
160 |
})
|
|
|
176 |
addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Got another idea?`);
|
177 |
} else if (data.suggestions.length > 0) {
|
178 |
addMessage('bot', `Check out these suggestions for "${searchTerm}" from Ingredient_Object__c:`);
|
179 |
+
displayItemsList(data.suggestions, 'suggestionsList', false);
|
180 |
} else {
|
181 |
+
addMessage('bot', `No matches for "${searchTerm}" in Ingredient_Object__c. Try "chicken" or "paneer"?`);
|
182 |
}
|
183 |
console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
|
184 |
})
|
|
|
197 |
.then(response => response.json())
|
198 |
.then(data => {
|
199 |
if (data.error) {
|
200 |
+
addMessage('bot', `Couldn’t find "${itemName}" in Ingredient_Object__c. Add it manually in the box below!`);
|
201 |
updateSelectionBox();
|
202 |
} else {
|
203 |
const details = data.item_details;
|
|
|
208 |
}
|
209 |
})
|
210 |
.catch(error => {
|
211 |
+
addMessage('bot', `Trouble fetching "${itemName}": ${error.message}. Add it manually or I’ll retry...`);
|
212 |
updateSelectionBox();
|
213 |
setTimeout(() => fetchItemDetails(itemName), 2000);
|
214 |
});
|
|
|
238 |
button.textContent = 'Add';
|
239 |
button.onclick = () => {
|
240 |
if (isSectorDetail) {
|
|
|
241 |
const sectorItem = {
|
242 |
name: item.name,
|
243 |
description: 'From Sector_Detail__c',
|
|
|
247 |
};
|
248 |
selectedItems.push(sectorItem);
|
249 |
addMessage('bot', `Added "${item.name}" to your selection! Check the box below.`);
|
|
|
250 |
} else {
|
|
|
251 |
fetchItemDetails(item.name);
|
252 |
}
|
253 |
+
updateSelectionBox();
|
254 |
};
|
255 |
itemDiv.appendChild(img);
|
256 |
itemDiv.appendChild(name);
|
|
|
275 |
button.onclick = () => {
|
276 |
addMessage('user', opt.text);
|
277 |
conversation.push({ role: 'user', message: opt.text });
|
278 |
+
selectionBoxVisible = true;
|
279 |
handleResponse(opt.text);
|
280 |
+
updateSelectionBox();
|
281 |
};
|
282 |
optionsDiv.appendChild(button);
|
283 |
});
|
|
|
328 |
{ role: 'bot', message: `Great to meet you, ${userName}! 🍳 What kind of food are you craving today?` }
|
329 |
];
|
330 |
selectedItems = [];
|
331 |
+
selectionBoxVisible = true;
|
332 |
const chatMessages = document.getElementById('chatMessages');
|
333 |
chatMessages.innerHTML = '';
|
334 |
conversation.forEach(msg => addMessage(msg.role, msg.message));
|