lokesh341 commited on
Commit
690e9af
·
verified ·
1 Parent(s): b12eefe

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +58 -65
static/script.js CHANGED
@@ -100,12 +100,12 @@ function updateSelectionBox() {
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,10 +141,9 @@ function handleResponse(userInput) {
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);
148
  return;
149
  }
150
 
@@ -185,10 +184,10 @@ function suggestItems(searchTerm) {
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
  })
@@ -198,8 +197,8 @@ function suggestItems(searchTerm) {
198
  });
199
  }
200
 
201
- function fetchItemDetails(itemName) {
202
- fetch('/get_item_details', {
203
  method: 'POST',
204
  headers: { 'Content-Type': 'application/json' },
205
  body: JSON.stringify({ item_name: itemName })
@@ -207,44 +206,24 @@ function fetchItemDetails(itemName) {
207
  .then(response => response.json())
208
  .then(data => {
209
  if (data.error) {
210
- addMessage('bot', `Couldn’t find "${itemName}" in Ingredient_Object__c. Add it manually in the box below!`);
211
- updateSelectionBox();
212
  } else {
213
  const details = data.item_details;
214
  if (selectedItems.some(item => item.name === details.name)) {
215
  addMessage('bot', `"${details.name}" is already in your selection!`);
216
  } else {
 
217
  selectedItems.push(details);
218
- addMessage('bot', `Added "${itemName}" to your selection! Check the box below.`);
219
  updateSelectionBox();
 
220
  }
221
  }
222
  })
223
  .catch(error => {
224
- addMessage('bot', `Trouble fetching "${itemName}": ${error.message}. Add it manually or I’ll retry...`);
225
- updateSelectionBox();
226
- setTimeout(() => fetchItemDetails(itemName), 2000);
227
- });
228
- }
229
-
230
- function fetchItemDetailsByName(itemName) {
231
- fetch('/get_item_details_by_name', {
232
- method: 'POST',
233
- headers: { 'Content-Type': 'application/json' },
234
- body: JSON.stringify({ item_name: itemName })
235
- })
236
- .then(response => response.json())
237
- .then(data => {
238
- if (data.error) {
239
- addMessage('bot', `Sorry, I couldn’t find "${itemName}" in Sector_Detail__c. Try another item or use the options below!`);
240
- } else {
241
- const details = data.item_details;
242
- addMessage('bot', `Details for "${details.name}":\n- Category: ${details.category}\n- Description: ${details.description}`);
243
- }
244
- })
245
- .catch(error => {
246
- addMessage('bot', `Error fetching details for "${itemName}": ${error.message}. I’ll retry in a sec...`);
247
- setTimeout(() => fetchItemDetailsByName(itemName), 2000);
248
  });
249
  }
250
 
@@ -257,12 +236,13 @@ function fetchSectorItemDetails(itemName) {
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();
@@ -299,6 +279,32 @@ function showDescriptionPopup(item) {
299
  desc.textContent = item.description;
300
  content.appendChild(desc);
301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  const buttonContainer = document.createElement('div');
303
  buttonContainer.className = 'button-container';
304
 
@@ -306,17 +312,11 @@ function showDescriptionPopup(item) {
306
  addButton.textContent = 'Add';
307
  addButton.className = 'popup-add-button';
308
  addButton.onclick = () => {
309
- const sectorItem = {
310
- name: item.name,
311
- image_url: item.image_url || '',
312
- category: item.category || 'Unknown',
313
- description: item.description || 'No description available'
314
- };
315
- if (selectedItems.some(existing => existing.name === sectorItem.name)) {
316
- addMessage('bot', `"${sectorItem.name}" is already in your selection!`);
317
  } else {
318
- selectedItems.push(sectorItem);
319
- addMessage('bot', `Added "${sectorItem.name}" to your selection! Check the box below.`);
320
  updateSelectionBox();
321
  }
322
  popup.remove();
@@ -359,18 +359,17 @@ function displayItemsList(items, containerId, isSectorDetail = false) {
359
  nameDiv.className = 'ingredient-name';
360
  itemDiv.appendChild(nameDiv);
361
 
362
- if (isSectorDetail && item.description) {
363
- const showDescButton = document.createElement('button');
364
- showDescButton.textContent = 'Show Description';
365
- showDescButton.className = 'show-desc-button';
366
- showDescButton.onclick = () => showDescriptionPopup(item);
367
- itemDiv.appendChild(showDescButton);
368
- } else {
369
- const addButton = document.createElement('button');
370
- addButton.textContent = 'Add';
371
- addButton.onclick = () => fetchItemDetails(item.name);
372
- itemDiv.appendChild(addButton);
373
- }
374
 
375
  container.appendChild(itemDiv);
376
  });
@@ -416,16 +415,10 @@ function submitToSalesforce() {
416
  return;
417
  }
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', {
426
  method: 'POST',
427
  headers: { 'Content-Type': 'application/json' },
428
- body: JSON.stringify({ items: itemsToSubmit })
429
  })
430
  .then(response => response.json())
431
  .then(data => {
 
100
 
101
  const textInput = document.createElement('input');
102
  textInput.type = 'text';
103
+ textInput.placeholder = 'Add Menu_Item__c or 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
+ fetchMenuItemDetails(itemName); // Try Menu_Item__c first
109
  textInput.value = '';
110
  }
111
  });
 
141
  fetchMenuItems(lowerInput);
142
  return;
143
  } else {
 
144
  botResponse = `Looking up details for "${userInput}"...`;
145
  addMessage('bot', botResponse);
146
+ fetchMenuItemDetails(userInput); // Try Menu_Item__c
147
  return;
148
  }
149
 
 
184
  if (data.error) {
185
  addMessage('bot', `Couldn’t find anything for "${searchTerm}": ${data.error}. Got another idea?`);
186
  } else if (data.suggestions.length > 0) {
187
+ addMessage('bot', `Check out these suggestions for "${searchTerm}" from Salesforce:`);
188
  displayItemsList(data.suggestions, 'suggestionsList', false);
189
  } else {
190
+ addMessage('bot', `No matches for "${searchTerm}" in Salesforce. Try "chicken" or "paneer"?`);
191
  }
192
  console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
193
  })
 
197
  });
198
  }
199
 
200
+ function fetchMenuItemDetails(itemName) {
201
+ fetch('/get_menu_item_details', {
202
  method: 'POST',
203
  headers: { 'Content-Type': 'application/json' },
204
  body: JSON.stringify({ item_name: itemName })
 
206
  .then(response => response.json())
207
  .then(data => {
208
  if (data.error) {
209
+ // If not found in Menu_Item__c, try Sector_Detail__c
210
+ fetchSectorItemDetails(itemName);
211
  } else {
212
  const details = data.item_details;
213
  if (selectedItems.some(item => item.name === details.name)) {
214
  addMessage('bot', `"${details.name}" is already in your selection!`);
215
  } else {
216
+ details.source = 'Menu_Item__c';
217
  selectedItems.push(details);
218
+ addMessage('bot', `Added "${details.name}" to your selection! Check the box below.`);
219
  updateSelectionBox();
220
+ showDescriptionPopup(details); // Show detailed popup
221
  }
222
  }
223
  })
224
  .catch(error => {
225
+ addMessage('bot', `Trouble fetching "${itemName}": ${error.message}. I’ll retry...`);
226
+ setTimeout(() => fetchMenuItemDetails(itemName), 2000);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
227
  });
228
  }
229
 
 
236
  .then(response => response.json())
237
  .then(data => {
238
  if (data.error) {
239
+ addMessage('bot', `Sorry, I couldn’t find "${itemName}" in Salesforce. Try another item!`);
240
  } else {
241
  const details = data.item_details;
242
  if (selectedItems.some(item => item.name === details.name)) {
243
  addMessage('bot', `"${details.name}" is already in your selection!`);
244
  } else {
245
+ details.source = 'Sector_Detail__c';
246
  selectedItems.push(details);
247
  addMessage('bot', `Added "${details.name}" to your selection with image! Check the box below.`);
248
  updateSelectionBox();
 
279
  desc.textContent = item.description;
280
  content.appendChild(desc);
281
 
282
+ // Display additional Menu_Item__c fields if available
283
+ if (item.source === 'Menu_Item__c') {
284
+ const detailsList = document.createElement('ul');
285
+ detailsList.style.listStyleType = 'none';
286
+ detailsList.style.padding = '0';
287
+
288
+ const fields = [
289
+ { label: 'Ingredients', value: item.ingredients },
290
+ { label: 'Nutritional Info', value: item.nutritional_info },
291
+ { label: 'Price', value: `$${item.price.toFixed(2)}` },
292
+ { label: 'Sector', value: item.sector },
293
+ { label: 'Spice Level', value: item.spice_levels },
294
+ { label: 'Veg/Non-Veg', value: item.veg_nonveg },
295
+ { label: 'Category', value: item.category },
296
+ { label: 'Dynamic Dish', value: item.dynamic_dish ? 'Yes' : 'No' }
297
+ ];
298
+
299
+ fields.forEach(field => {
300
+ const li = document.createElement('li');
301
+ li.innerHTML = `<strong>${field.label}:</strong> ${field.value}`;
302
+ detailsList.appendChild(li);
303
+ });
304
+
305
+ content.appendChild(detailsList);
306
+ }
307
+
308
  const buttonContainer = document.createElement('div');
309
  buttonContainer.className = 'button-container';
310
 
 
312
  addButton.textContent = 'Add';
313
  addButton.className = 'popup-add-button';
314
  addButton.onclick = () => {
315
+ if (selectedItems.some(existing => existing.name === item.name)) {
316
+ addMessage('bot', `"${item.name}" is already in your selection!`);
 
 
 
 
 
 
317
  } else {
318
+ selectedItems.push(item);
319
+ addMessage('bot', `Added "${item.name}" to your selection! Check the box below.`);
320
  updateSelectionBox();
321
  }
322
  popup.remove();
 
359
  nameDiv.className = 'ingredient-name';
360
  itemDiv.appendChild(nameDiv);
361
 
362
+ const actionButton = document.createElement('button');
363
+ actionButton.textContent = isSectorDetail ? 'Show Description' : 'Add';
364
+ actionButton.className = isSectorDetail ? 'show-desc-button' : 'add-button';
365
+ actionButton.onclick = () => {
366
+ if (isSectorDetail) {
367
+ showDescriptionPopup(item);
368
+ } else {
369
+ fetchMenuItemDetails(item.name);
370
+ }
371
+ };
372
+ itemDiv.appendChild(actionButton);
 
373
 
374
  container.appendChild(itemDiv);
375
  });
 
415
  return;
416
  }
417
 
 
 
 
 
 
 
418
  fetch('/submit_items', {
419
  method: 'POST',
420
  headers: { 'Content-Type': 'application/json' },
421
+ body: JSON.stringify({ items: selectedItems })
422
  })
423
  .then(response => response.json())
424
  .then(data => {