lokesh341 commited on
Commit
b5dcc88
·
verified ·
1 Parent(s): 646f759

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +17 -30
static/script.js CHANGED
@@ -100,12 +100,12 @@ function updateSelectionBox() {
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
  });
@@ -141,6 +141,7 @@ function handleResponse(userInput) {
141
  fetchMenuItems(lowerInput);
142
  return;
143
  } else {
 
144
  botResponse = `Looking up details for "${userInput}"...`;
145
  addMessage('bot', botResponse);
146
  fetchItemDetailsByName(userInput);
@@ -247,44 +248,30 @@ function fetchItemDetailsByName(itemName) {
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
 
 
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
  });
 
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);
 
248
  });
249
  }
250
 
251
+ function fetchSectorItemDetails(itemName) {
252
+ fetch('/get_sector_item_details', {
253
  method: 'POST',
254
  headers: { 'Content-Type': 'application/json' },
255
+ body: JSON.stringify({ item_name: itemName })
256
  })
257
  .then(response => response.json())
258
  .then(data => {
259
  if (data.error) {
260
+ addMessage('bot', `Sorry, I couldn’t find "${itemName}" in Sector_Detail__c. Try another item!`);
261
+ } else {
262
+ const details = data.item_details;
263
+ if (selectedItems.some(item => item.name === details.name)) {
264
+ addMessage('bot', `"${details.name}" is already in your selection!`);
 
 
 
 
 
 
 
 
 
 
 
 
 
265
  } else {
266
+ selectedItems.push(details);
267
+ addMessage('bot', `Added "${details.name}" to your selection with image! Check the box below.`);
268
+ updateSelectionBox();
269
  }
 
 
270
  }
 
271
  })
272
  .catch(error => {
273
+ addMessage('bot', `Error fetching details for "${itemName}": ${error.message}. I’ll retry in a sec...`);
274
+ setTimeout(() => fetchSectorItemDetails(itemName), 2000);
275
  });
276
  }
277