lokesh341 commited on
Commit
56df0a0
·
verified ·
1 Parent(s): 7efbd6f

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +17 -33
static/script.js CHANGED
@@ -38,7 +38,6 @@ function handleResponse(userInput) {
38
  const lowerInput = userInput.toLowerCase();
39
  let botResponse = '';
40
 
41
- // Step 1: After name input
42
  if (conversation.length === 2) {
43
  botResponse = `Nice to meet you, ${userInput}! 😊 What type of food would you like to explore today?`;
44
  displayOptions([
@@ -46,17 +45,13 @@ function handleResponse(userInput) {
46
  { text: 'Non-Vegetarian', class: 'red' },
47
  { text: 'Both', class: 'gray' }
48
  ]);
49
- }
50
- // Step 2: After dietary preference
51
- else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
52
- botResponse = `Awesome! Let me fetch some ${lowerInput} menu items for you...`;
53
  addMessage('bot', botResponse);
54
  fetchMenuItems(lowerInput);
55
  return;
56
- }
57
- // Step 3: Search for specific items
58
- else {
59
- botResponse = `Looking for something with "${userInput}"...`;
60
  addMessage('bot', botResponse);
61
  suggestItems(userInput);
62
  return;
@@ -65,7 +60,6 @@ function handleResponse(userInput) {
65
  addMessage('bot', botResponse);
66
  }
67
 
68
- // Fetch menu items from Salesforce
69
  function fetchMenuItems(dietaryPreference) {
70
  fetch('/get_menu_items', { method: 'GET' })
71
  .then(response => response.json())
@@ -75,10 +69,9 @@ function fetchMenuItems(dietaryPreference) {
75
  } else {
76
  const filteredItems = data.menu_items.filter(item => {
77
  if (dietaryPreference === 'both') return true;
78
- // Client-side filtering (update if Salesforce supports this)
79
  return dietaryPreference === 'vegetarian'
80
- ? item.name.toLowerCase().includes('veg')
81
- : !item.name.toLowerCase().includes('veg');
82
  });
83
  if (filteredItems.length > 0) {
84
  addMessage('bot', `Here are some ${dietaryPreference} menu items:`);
@@ -90,12 +83,11 @@ function fetchMenuItems(dietaryPreference) {
90
  }
91
  })
92
  .catch(error => {
93
- addMessage('bot', `Error connecting to the database: ${error.message}. I’ll try again soon!`);
94
- setTimeout(() => fetchMenuItems(dietaryPreference), 2000); // Retry after 2 seconds
95
  });
96
  }
97
 
98
- // Fetch suggestions from Salesforce
99
  function suggestItems(searchTerm) {
100
  fetch('/suggest_items', {
101
  method: 'POST',
@@ -110,17 +102,16 @@ function suggestItems(searchTerm) {
110
  addMessage('bot', `Found these suggestions for "${searchTerm}":`);
111
  displayItemsList(data.suggestions, 'suggestionsList');
112
  } else {
113
- addMessage('bot', `No matches for "${searchTerm}". Maybe try "chicken" or "rice"?`);
114
  }
115
  console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
116
  })
117
  .catch(error => {
118
  addMessage('bot', `Error fetching suggestions: ${error.message}. Retrying...`);
119
- setTimeout(() => suggestItems(searchTerm), 2000); // Retry after 2 seconds
120
  });
121
  }
122
 
123
- // Fetch item details from Salesforce with improved error handling
124
  function fetchItemDetails(itemName) {
125
  fetch('/get_item_details', {
126
  method: 'POST',
@@ -130,27 +121,26 @@ function fetchItemDetails(itemName) {
130
  .then(response => response.json())
131
  .then(data => {
132
  if (data.error) {
133
- addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Does it exist in our menu?`);
134
  } else {
135
  const details = data.item_details;
136
  selectedItems.push(details);
137
- addMessage('bot', `Here’s what I found for "${itemName}":`);
138
  displaySelectedItems();
139
- console.log(`Details fetched for ${itemName}:`, details);
140
  }
141
  })
142
  .catch(error => {
143
- addMessage('bot', `Error fetching details for "${itemName}": ${error.message}. I’ll retry...`);
144
- setTimeout(() => fetchItemDetails(itemName), 2000); // Retry after 2 seconds
145
  });
146
  }
147
 
148
- // Display items (menu items or suggestions)
149
  function displayItemsList(items, containerId) {
150
  const container = document.getElementById(containerId);
151
  if (!container) {
152
  console.error(`${containerId} container not found!`);
153
- addMessage('bot', 'Something went wrong with the display. Please try again!');
154
  return;
155
  }
156
  container.innerHTML = '';
@@ -176,12 +166,10 @@ function displayItemsList(items, containerId) {
176
  });
177
  }
178
 
179
- // Display selected items with details
180
  function displaySelectedItems() {
181
  const selectedArea = document.getElementById('itemDetails');
182
  if (!selectedArea) {
183
  console.error('Item details container not found!');
184
- addMessage('bot', 'Can’t show details right now. Try again later!');
185
  return;
186
  }
187
  selectedArea.innerHTML = '';
@@ -197,9 +185,8 @@ function displaySelectedItems() {
197
  }
198
  }
199
 
200
- // Display options (Vegetarian, Non-Vegetarian, Both)
201
  function displayOptions(options) {
202
- const chatMessages = document.getElementId('chatMessages');
203
  if (!chatMessages) {
204
  console.error('Chat messages container not found for options!');
205
  return;
@@ -230,7 +217,6 @@ function displayOptions(options) {
230
  chatMessages.appendChild(optionsDiv);
231
  }
232
 
233
- // Reset conversation
234
  function resetConversation() {
235
  const userName = conversation.length > 1 ? conversation[1].message : 'Friend';
236
  conversation = [
@@ -252,10 +238,8 @@ function resetConversation() {
252
  displaySelectedItems();
253
  }
254
 
255
- // Event listeners
256
  document.getElementById('userInput').addEventListener('keypress', (e) => {
257
  if (e.key === 'Enter') sendMessage();
258
  });
259
 
260
- // Initial load
261
  console.log('Chef Bot script loaded successfully!');
 
38
  const lowerInput = userInput.toLowerCase();
39
  let botResponse = '';
40
 
 
41
  if (conversation.length === 2) {
42
  botResponse = `Nice to meet you, ${userInput}! 😊 What type of food would you like to explore today?`;
43
  displayOptions([
 
45
  { text: 'Non-Vegetarian', class: 'red' },
46
  { text: 'Both', class: 'gray' }
47
  ]);
48
+ } else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
49
+ botResponse = `Awesome! Fetching some ${lowerInput} menu items for you...`;
 
 
50
  addMessage('bot', botResponse);
51
  fetchMenuItems(lowerInput);
52
  return;
53
+ } else {
54
+ botResponse = `Searching for "${userInput}"...`;
 
 
55
  addMessage('bot', botResponse);
56
  suggestItems(userInput);
57
  return;
 
60
  addMessage('bot', botResponse);
61
  }
62
 
 
63
  function fetchMenuItems(dietaryPreference) {
64
  fetch('/get_menu_items', { method: 'GET' })
65
  .then(response => response.json())
 
69
  } else {
70
  const filteredItems = data.menu_items.filter(item => {
71
  if (dietaryPreference === 'both') return true;
 
72
  return dietaryPreference === 'vegetarian'
73
+ ? item.category === 'Veg'
74
+ : item.category === 'Non-Veg';
75
  });
76
  if (filteredItems.length > 0) {
77
  addMessage('bot', `Here are some ${dietaryPreference} menu items:`);
 
83
  }
84
  })
85
  .catch(error => {
86
+ addMessage('bot', `Error connecting to Salesforce: ${error.message}. Retrying...`);
87
+ setTimeout(() => fetchMenuItems(dietaryPreference), 2000);
88
  });
89
  }
90
 
 
91
  function suggestItems(searchTerm) {
92
  fetch('/suggest_items', {
93
  method: 'POST',
 
102
  addMessage('bot', `Found these suggestions for "${searchTerm}":`);
103
  displayItemsList(data.suggestions, 'suggestionsList');
104
  } else {
105
+ addMessage('bot', `No matches for "${searchTerm}". Try "chicken" or "rice"?`);
106
  }
107
  console.log(`Suggestions for ${searchTerm}:`, data.suggestions);
108
  })
109
  .catch(error => {
110
  addMessage('bot', `Error fetching suggestions: ${error.message}. Retrying...`);
111
+ setTimeout(() => suggestItems(searchTerm), 2000);
112
  });
113
  }
114
 
 
115
  function fetchItemDetails(itemName) {
116
  fetch('/get_item_details', {
117
  method: 'POST',
 
121
  .then(response => response.json())
122
  .then(data => {
123
  if (data.error) {
124
+ addMessage('bot', `Sorry, couldn’t get details for "${itemName}": ${data.error}. Check the name?`);
125
  } else {
126
  const details = data.item_details;
127
  selectedItems.push(details);
128
+ addMessage('bot', `Added "${itemName}"! Here are the details:`);
129
  displaySelectedItems();
130
+ console.log(`Details for ${itemName}:`, details);
131
  }
132
  })
133
  .catch(error => {
134
+ addMessage('bot', `Error fetching details for "${itemName}": ${error.message}. Retrying...`);
135
+ setTimeout(() => fetchItemDetails(itemName), 2000);
136
  });
137
  }
138
 
 
139
  function displayItemsList(items, containerId) {
140
  const container = document.getElementById(containerId);
141
  if (!container) {
142
  console.error(`${containerId} container not found!`);
143
+ addMessage('bot', 'Display issue. Please try again!');
144
  return;
145
  }
146
  container.innerHTML = '';
 
166
  });
167
  }
168
 
 
169
  function displaySelectedItems() {
170
  const selectedArea = document.getElementById('itemDetails');
171
  if (!selectedArea) {
172
  console.error('Item details container not found!');
 
173
  return;
174
  }
175
  selectedArea.innerHTML = '';
 
185
  }
186
  }
187
 
 
188
  function displayOptions(options) {
189
+ const chatMessages = document.getElementById('chatMessages');
190
  if (!chatMessages) {
191
  console.error('Chat messages container not found for options!');
192
  return;
 
217
  chatMessages.appendChild(optionsDiv);
218
  }
219
 
 
220
  function resetConversation() {
221
  const userName = conversation.length > 1 ? conversation[1].message : 'Friend';
222
  conversation = [
 
238
  displaySelectedItems();
239
  }
240
 
 
241
  document.getElementById('userInput').addEventListener('keypress', (e) => {
242
  if (e.key === 'Enter') sendMessage();
243
  });
244
 
 
245
  console.log('Chef Bot script loaded successfully!');