lokesh341 commited on
Commit
113a89f
·
verified ·
1 Parent(s): 6bcef4b

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +363 -4
static/script.js CHANGED
@@ -147,7 +147,7 @@ function fetchMenuItems(dietaryPreference) {
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');
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
  }
@@ -171,7 +171,7 @@ function suggestItems(searchTerm) {
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');
175
  } else {
176
  addMessage('bot', `No luck with "${searchTerm}" in Ingredient_Object__c. How about "chicken" or "paneer"?`);
177
  }
@@ -209,7 +209,7 @@ function fetchItemDetails(itemName) {
209
  });
210
  }
211
 
212
- function displayItemsList(items, containerId) {
213
  const container = document.getElementById(containerId);
214
  if (!container) {
215
  console.error(`${containerId} container not found!`);
@@ -231,7 +231,366 @@ function displayItemsList(items, containerId) {
231
  name.style.fontSize = '12px';
232
  const button = document.createElement('button');
233
  button.textContent = 'Add';
234
- button.onclick = () => fetchItemDetails(item.name);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
235
  itemDiv.appendChild(img);
236
  itemDiv.appendChild(name);
237
  itemDiv.appendChild(button);
 
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
  }
 
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
  }
 
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!`);
 
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
+
390
+ // Add dietary preference buttons
391
+ const vegButton = document.createElement('button');
392
+ vegButton.textContent = 'Vegetarian';
393
+ vegButton.className = 'dietary-button green';
394
+ vegButton.onclick = () => fetchMenuItems('vegetarian');
395
+ selectionBox.appendChild(vegButton);
396
+
397
+ const nonVegButton = document.createElement('button');
398
+ nonVegButton.textContent = 'Non-Vegetarian';
399
+ nonVegButton.className = 'dietary-button red';
400
+ nonVegButton.onclick = () => fetchMenuItems('non-vegetarian');
401
+ selectionBox.appendChild(nonVegButton);
402
+
403
+ const bothButton = document.createElement('button');
404
+ bothButton.textContent = 'Both';
405
+ bothButton.className = 'dietary-button gray';
406
+ bothButton.onclick = () => fetchMenuItems('both');
407
+ selectionBox.appendChild(bothButton);
408
+
409
+ // Add selected items label and list
410
+ const label = document.createElement('span');
411
+ label.textContent = 'Selected Items:';
412
+ selectionBox.appendChild(label);
413
+
414
+ selectedItems.forEach(item => {
415
+ const itemSpan = document.createElement('span');
416
+ itemSpan.textContent = item.name;
417
+ selectionBox.appendChild(itemSpan);
418
+ });
419
+
420
+ // Add text input for manual item entry
421
+ const textInput = document.createElement('input');
422
+ textInput.type = 'text';
423
+ textInput.placeholder = 'Add item manually...';
424
+ textInput.className = 'manual-input';
425
+ textInput.addEventListener('keypress', (e) => {
426
+ if (e.key === 'Enter' && textInput.value.trim()) {
427
+ const manualItem = {
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.`);
435
+ textInput.value = '';
436
+ updateSelectionBox();
437
+ }
438
+ });
439
+ selectionBox.appendChild(textInput);
440
+
441
+ // Add Submit button if there are selected items
442
+ if (selectedItems.length > 0) {
443
+ const submitButton = document.createElement('button');
444
+ submitButton.textContent = 'Submit to Salesforce';
445
+ submitButton.className = 'submit-button';
446
+ submitButton.onclick = submitToSalesforce;
447
+ selectionBox.appendChild(submitButton);
448
+ }
449
+
450
+ chatMessages.appendChild(selectionBox);
451
+ chatMessages.scrollTop = chatMessages.scrollHeight;
452
+ console.log('Updated selection box with items:', selectedItems.map(item => item.name));
453
+ }
454
+
455
+ function handleResponse(userInput) {
456
+ const lowerInput = userInput.toLowerCase();
457
+ let botResponse = '';
458
+
459
+ if (conversation.length === 2) {
460
+ botResponse = `Great to meet you, ${userInput}! 🍳 What kind of food are you craving today?`;
461
+ displayOptions([
462
+ { text: 'Vegetarian', class: 'green' },
463
+ { text: 'Non-Vegetarian', class: 'red' },
464
+ { text: 'Both', class: 'gray' }
465
+ ]);
466
+ } else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
467
+ botResponse = `Nice choice! Let me whip up some ${lowerInput} options for you...`;
468
+ addMessage('bot', botResponse);
469
+ fetchMenuItems(lowerInput);
470
+ return;
471
+ } else {
472
+ botResponse = `Hmm, let’s see what I can find for "${userInput}"...`;
473
+ addMessage('bot', botResponse);
474
+ suggestItems(userInput);
475
+ return;
476
+ }
477
+
478
+ addMessage('bot', botResponse);
479
+ }
480
+
481
+ function fetchMenuItems(dietaryPreference) {
482
+ fetch('/get_menu_items', {
483
+ method: 'POST',
484
+ headers: { 'Content-Type': 'application/json' },
485
+ body: JSON.stringify({ dietary_preference: dietaryPreference })
486
+ })
487
+ .then(response => response.json())
488
+ .then(data => {
489
+ if (data.error) {
490
+ addMessage('bot', `Oops! Trouble fetching ${dietaryPreference} items: ${data.error}. Want to try again or search something specific?`);
491
+ } else if (data.menu_items.length > 0) {
492
+ displayItemsList(data.menu_items, 'menuItemsList', true); // Pass true to indicate Sector_Detail__c items
493
+ } else {
494
+ addMessage('bot', `Looks like we’re out of ${dietaryPreference} items in Sector_Detail__c. Try searching for something like "paneer" or "chicken"!`);
495
+ }
496
+ console.log(`Fetched items from Sector_Detail__c for ${dietaryPreference}:`, data.menu_items);
497
+ })
498
+ .catch(error => {
499
+ addMessage('bot', `Yikes! Couldn’t reach Salesforce: ${error.message}. I’ll retry in a sec...`);
500
+ setTimeout(() => fetchMenuItems(dietaryPreference), 2000);
501
+ });
502
+ }
503
+
504
+ function suggestItems(searchTerm) {
505
+ fetch('/suggest_items', {
506
+ method: 'POST',
507
+ headers: { 'Content-Type': 'application/json' },
508
+ body: JSON.stringify({ search_term: searchTerm })
509
+ })
510
+ .then(response => response.json())
511
+ .then(data => {
512
+ if (data.error) {
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); // Pass false for Ingredient_Object__c items
517
+ } else {
518
+ addMessage('bot', `No luck with "${searchTerm}" in Ingredient_Object__c. How about "chicken" or "paneer"?`);
519
+ }
520
+ console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
521
+ })
522
+ .catch(error => {
523
+ addMessage('bot', `Error fetching suggestions: ${error.message}. Retrying...`);
524
+ setTimeout(() => suggestItems(searchTerm), 2000);
525
+ });
526
+ }
527
+
528
+ function fetchItemDetails(itemName) {
529
+ fetch('/get_item_details', {
530
+ method: 'POST',
531
+ headers: { 'Content-Type': 'application/json' },
532
+ body: JSON.stringify({ item_name: itemName })
533
+ })
534
+ .then(response => response.json())
535
+ .then(data => {
536
+ if (data.error) {
537
+ addMessage('bot', `Couldn’t find "${itemName}" in Salesforce. Add it manually in the box below!`);
538
+ updateSelectionBox();
539
+ } else {
540
+ const details = data.item_details;
541
+ selectedItems.push(details);
542
+ addMessage('bot', `Added "${itemName}" to your selection! Check the box below.`);
543
+ updateSelectionBox();
544
+ console.log(`Details for ${itemName}:`, details);
545
+ }
546
+ })
547
+ .catch(error => {
548
+ addMessage('bot', `Trouble fetching details for "${itemName}": ${error.message}. Add it manually below or I’ll retry...`);
549
+ updateSelectionBox();
550
+ setTimeout(() => fetchItemDetails(itemName), 2000);
551
+ });
552
+ }
553
+
554
+ function displayItemsList(items, containerId, isSectorDetail = false) {
555
+ const container = document.getElementById(containerId);
556
+ if (!container) {
557
+ console.error(`${containerId} container not found!`);
558
+ addMessage('bot', 'Something’s off with the display. Try again?');
559
+ return;
560
+ }
561
+ container.innerHTML = '';
562
+
563
+ items.forEach(item => {
564
+ const itemDiv = document.createElement('div');
565
+ itemDiv.className = containerId === 'menuItemsList' ? 'ingredient-item' : 'suggestion-item';
566
+ const img = document.createElement('img');
567
+ img.src = item.image_url || 'https://via.placeholder.com/60';
568
+ img.alt = item.name;
569
+ const name = document.createElement('div');
570
+ name.textContent = item.name;
571
+ name.style.textAlign = 'center';
572
+ name.style.marginTop = '5px';
573
+ name.style.fontSize = '12px';
574
+ const button = document.createElement('button');
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',
582
+ ingredients: 'Not specified',
583
+ image_url: item.image_url || '',
584
+ category: item.category || 'Both'
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);
596
  itemDiv.appendChild(button);