Yaswanth56 commited on
Commit
8170b7e
·
verified ·
1 Parent(s): c75a07d

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +145 -85
static/script.js CHANGED
@@ -1,7 +1,9 @@
1
  let conversation = [
2
- { role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }
3
  ];
4
  let selectedIngredients = [];
 
 
5
 
6
  function addMessage(role, message) {
7
  const chatMessages = document.getElementById('chatMessages');
@@ -28,10 +30,7 @@ function sendMessage() {
28
  addMessage('user', message);
29
  conversation.push({ role: 'user', message: message });
30
  userInput.value = '';
31
-
32
- setTimeout(() => {
33
- handleResponse(message);
34
- }, 500);
35
  } else {
36
  console.warn('Empty message!');
37
  }
@@ -42,52 +41,45 @@ function handleResponse(userInput) {
42
  let botResponse = '';
43
  let options = [];
44
 
45
- // Store food preference (Vegetarian or Non-Vegetarian)
46
  if (conversation.length === 2) { // After name input
47
  botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
48
  options = [
49
  { text: 'Vegetarian', class: 'green' },
50
  { text: 'Non-Vegetarian', class: 'red' }
51
  ];
52
- } else if (lastMessage.includes('non-vegetarian') || lastMessage.includes('non veg') || lastMessage.includes('nonveg')) {
53
- conversation.push({ role: 'user', message: 'Non-Vegetarian' }); // Store Non-Vegetarian preference
 
 
 
 
 
 
54
  console.log("Food preference selected: Non-Vegetarian");
55
- console.log("Conversation Array after storing Non-Vegetarian:", conversation); // Debugging: Log conversation
56
- botResponse = 'Great choice! 🍽️ We have some amazing non-vegetarian options! What\'s your dietary preference?';
57
  options = [
58
- { text: 'Low Carb', class: '' },
59
- { text: 'Dairy-Free', class: '' },
60
- { text: 'Keto', class: '' },
61
- { text: 'Halal', class: '' }
62
  ];
63
- } else if (lastMessage.includes('vegetarian')) {
64
- conversation.push({ role: 'user', message: 'Vegetarian' }); // Store Vegetarian preference
65
- console.log("Food preference selected: Vegetarian");
66
- console.log("Conversation Array after storing Vegetarian:", conversation); // Debugging: Log conversation
67
- botResponse = 'Great choice! 🍽️ We have some amazing vegetarian options! What\'s your dietary preference?';
 
 
 
 
 
 
 
 
 
68
  options = [
69
- { text: 'Vegan', class: '' },
70
- { text: 'Gluten-Free', class: '' },
71
- { text: 'Low Carb', class: '' },
72
- { text: 'Dairy-Free', class: '' },
73
- { text: 'Keto', class: '' },
74
- { text: 'Halal', class: '' }
75
  ];
76
- } else if (lastMessage.includes('low carb') || lastMessage.includes('dairy-free') || lastMessage.includes('keto') || lastMessage.includes('halal') || lastMessage.includes('gluten-free') || lastMessage.includes('vegan') || lastMessage.includes('vegetarian')) {
77
- // Fetch ingredients based on previous food preference (Vegetarian or Non-Vegetarian)
78
- console.log("Conversation Array before finding food preference:", conversation); // Debugging
79
- const foodPreference = conversation.find(msg => msg.role === 'user' && (msg.message.toLowerCase().includes('vegetarian') || msg.message.toLowerCase().includes('non-vegetarian')))?.message.toLowerCase();
80
-
81
- console.log("Previous food preference: " + foodPreference); // Debug which food preference is selected
82
-
83
- if (foodPreference === 'vegetarian') {
84
- console.log("Fetching Vegetarian ingredients..."); // Debug when fetching vegetarian ingredients
85
- fetchIngredients('veg'); // Fetch Vegetarian ingredients
86
- } else if (foodPreference === 'non-vegetarian') {
87
- console.log("Fetching Non-Vegetarian ingredients..."); // Debug when fetching non-vegetarian ingredients
88
- fetchIngredients('non-vegetarian'); // Fetch Non-Vegetarian ingredients
89
- }
90
- return; // Exit early to fetch ingredients
91
  }
92
 
93
  addMessage('bot', botResponse);
@@ -96,16 +88,13 @@ function handleResponse(userInput) {
96
  }
97
  }
98
 
99
-
100
-
101
-
102
  function fetchIngredients(foodPreference) {
103
  fetch('/get_ingredients', {
104
  method: 'POST',
105
  headers: {
106
  'Content-Type': 'application/json',
107
  },
108
- body: JSON.stringify({ dietary_preference: foodPreference }) // Send the food preference to the backend
109
  })
110
  .then(response => response.json())
111
  .then(data => {
@@ -113,7 +102,7 @@ function fetchIngredients(foodPreference) {
113
  addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
114
  } else {
115
  const ingredients = data.ingredients || [];
116
- addMessage('bot', 'Here are some available ingredients:');
117
  displayIngredientsList(ingredients);
118
  displaySelectedIngredients();
119
  }
@@ -123,6 +112,28 @@ function fetchIngredients(foodPreference) {
123
  });
124
  }
125
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
126
 
127
  function displayIngredientsList(ingredients) {
128
  const chatMessages = document.getElementById('chatMessages');
@@ -131,7 +142,6 @@ function displayIngredientsList(ingredients) {
131
  return;
132
  }
133
 
134
- // Create or update ingredients list
135
  let ingredientsList = document.querySelector('.ingredients-list');
136
  if (!ingredientsList) {
137
  ingredientsList = document.createElement('div');
@@ -145,7 +155,7 @@ function displayIngredientsList(ingredients) {
145
  const item = document.createElement('div');
146
  item.className = 'ingredient-item';
147
  const img = document.createElement('img');
148
- img.src = ingredient.image_url || 'https://via.placeholder.com/80'; // Fallback image
149
  img.alt = ingredient.name;
150
  const name = document.createElement('div');
151
  name.textContent = ingredient.name;
@@ -167,6 +177,50 @@ function displayIngredientsList(ingredients) {
167
  });
168
  }
169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
170
 
171
  function displaySelectedIngredients() {
172
  const chatMessages = document.getElementById('chatMessages');
@@ -175,7 +229,6 @@ function displaySelectedIngredients() {
175
  return;
176
  }
177
 
178
- // Create or update selected ingredients area
179
  let selectedArea = document.querySelector('.selected-ingredients');
180
  if (!selectedArea) {
181
  selectedArea = document.createElement('div');
@@ -191,23 +244,19 @@ function displaySelectedIngredients() {
191
  selectedArea.appendChild(div);
192
  });
193
 
194
- // Check if there are selected ingredients
195
  if (selectedIngredients.length > 0) {
196
- // Create and display the Submit button
197
  let submitButton = document.querySelector('.submit-button');
198
  if (!submitButton) {
199
  submitButton = document.createElement('button');
200
  submitButton.className = 'submit-button';
201
  submitButton.textContent = 'Submit Ingredients';
202
- submitButton.onclick = () => {
203
- submitIngredients(selectedIngredients);
204
- };
205
  chatMessages.appendChild(submitButton);
206
  }
207
  }
208
  }
209
 
210
- function submitIngredients(selectedIngredients) {
211
  fetch('/submit_ingredients', {
212
  method: 'POST',
213
  headers: {
@@ -218,21 +267,8 @@ function submitIngredients(selectedIngredients) {
218
  .then(response => response.json())
219
  .then(data => {
220
  if (data.success) {
221
- // Now fetch dish suggestions
222
- fetch('/get_dish_suggestions', {
223
- method: 'POST',
224
- headers: {
225
- 'Content-Type': 'application/json',
226
- },
227
- body: JSON.stringify({ ingredients: selectedIngredients })
228
- })
229
- .then(response => response.json())
230
- .then(suggestionData => {
231
- if (suggestionData.suggestions) {
232
- // Display dish suggestions
233
- displayDishSuggestions(suggestionData.suggestions);
234
- }
235
- });
236
  } else {
237
  addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
238
  }
@@ -242,22 +278,47 @@ function submitIngredients(selectedIngredients) {
242
  });
243
  }
244
 
245
- function displayDishSuggestions(suggestions) {
246
  const chatMessages = document.getElementById('chatMessages');
247
  if (!chatMessages) {
248
- console.error('Chat messages container not found for dish suggestions!');
249
  return;
250
  }
251
- addMessage('bot', `Based on your ingredients, here are some dish suggestions:`);
252
-
253
- // Create and display the suggestions
254
- const suggestionDiv = document.createElement('div');
255
- suggestionDiv.className = 'suggestions-list';
256
- suggestionDiv.textContent = suggestions;
257
- chatMessages.appendChild(suggestionDiv);
258
- }
259
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
260
 
 
 
 
 
261
 
262
  function displayOptions(options) {
263
  const chatMessages = document.getElementById('chatMessages');
@@ -272,7 +333,7 @@ function displayOptions(options) {
272
  button.onclick = () => {
273
  addMessage('user', opt.text);
274
  conversation.push({ role: 'user', message: opt.text });
275
- chatMessages.innerHTML = ''; // Clear previous messages
276
  conversation.forEach(msg => addMessage(msg.role, msg.message));
277
  setTimeout(() => handleResponse(opt.text), 500);
278
  };
@@ -283,21 +344,20 @@ function displayOptions(options) {
283
  backButton.textContent = 'Go Back';
284
  backButton.className = 'option-button';
285
  backButton.onclick = () => {
286
- conversation.pop(); // Remove last user input
287
- selectedIngredients = []; // Clear selected ingredients
288
- chatMessages.innerHTML = ''; // Clear previous messages
 
289
  conversation.forEach(msg => addMessage(msg.role, msg.message));
290
  setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
291
  };
292
  chatMessages.appendChild(backButton);
293
  }
294
 
295
- // Add event listener for Enter key
296
  document.getElementById('userInput').addEventListener('keypress', function(e) {
297
  if (e.key === 'Enter') {
298
  sendMessage();
299
  }
300
  });
301
 
302
- // Initial load check
303
  console.log('Script loaded successfully');
 
1
  let conversation = [
2
+ { role: 'bot', message: "Hi there! I'm Chef Bot! May I know your name?" }
3
  ];
4
  let selectedIngredients = [];
5
+ let selectedMenuItem = null;
6
+ let cart = [];
7
 
8
  function addMessage(role, message) {
9
  const chatMessages = document.getElementById('chatMessages');
 
30
  addMessage('user', message);
31
  conversation.push({ role: 'user', message: message });
32
  userInput.value = '';
33
+ setTimeout(() => handleResponse(message), 500);
 
 
 
34
  } else {
35
  console.warn('Empty message!');
36
  }
 
41
  let botResponse = '';
42
  let options = [];
43
 
 
44
  if (conversation.length === 2) { // After name input
45
  botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
46
  options = [
47
  { text: 'Vegetarian', class: 'green' },
48
  { text: 'Non-Vegetarian', class: 'red' }
49
  ];
50
+ } else if (lastMessage.includes('vegetarian')) {
51
+ conversation.push({ role: 'user', message: 'Vegetarian' });
52
+ console.log("Food preference selected: Vegetarian");
53
+ botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
54
+ fetchIngredients('veg');
55
+ return;
56
+ } else if (lastMessage.includes('non-vegetarian')) {
57
+ conversation.push({ role: 'user', message: 'Non-Vegetarian' });
58
  console.log("Food preference selected: Non-Vegetarian");
59
+ botResponse = 'Great choice! 🍽️ Would you like fish-based dishes?';
 
60
  options = [
61
+ { text: 'Yes', class: 'green' },
62
+ { text: 'No', class: 'red' }
 
 
63
  ];
64
+ } else if (lastMessage.includes('yes') && conversation.some(msg => msg.message.toLowerCase().includes('non-vegetarian'))) {
65
+ fetchMenuItems('fish');
66
+ return;
67
+ } else if (lastMessage.includes('no') && conversation.some(msg => msg.message.toLowerCase().includes('non-vegetarian'))) {
68
+ botResponse = 'Alright, here are some non-vegetarian ingredients:';
69
+ fetchIngredients('non-vegetarian');
70
+ return;
71
+ } else if (lastMessage.includes('yes') && selectedMenuItem) {
72
+ botResponse = 'Here are some ingredients to customize your dish:';
73
+ fetchIngredients('non-vegetarian');
74
+ return;
75
+ } else if (lastMessage.includes('no') && selectedMenuItem) {
76
+ addToCart(selectedMenuItem);
77
+ botResponse = 'Item added to cart! Would you like to order more?';
78
  options = [
79
+ { text: 'Yes', class: 'green' },
80
+ { text: 'No', class: 'red' }
 
 
 
 
81
  ];
82
+ selectedMenuItem = null;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
83
  }
84
 
85
  addMessage('bot', botResponse);
 
88
  }
89
  }
90
 
 
 
 
91
  function fetchIngredients(foodPreference) {
92
  fetch('/get_ingredients', {
93
  method: 'POST',
94
  headers: {
95
  'Content-Type': 'application/json',
96
  },
97
+ body: JSON.stringify({ dietary_preference: foodPreference })
98
  })
99
  .then(response => response.json())
100
  .then(data => {
 
102
  addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
103
  } else {
104
  const ingredients = data.ingredients || [];
105
+ addMessage('bot', 'Please select ingredients:');
106
  displayIngredientsList(ingredients);
107
  displaySelectedIngredients();
108
  }
 
112
  });
113
  }
114
 
115
+ function fetchMenuItems(category) {
116
+ fetch('/get_menu_items', {
117
+ method: 'POST',
118
+ headers: {
119
+ 'Content-Type': 'application/json',
120
+ },
121
+ body: JSON.stringify({ category: category })
122
+ })
123
+ .then(response => response.json())
124
+ .then(data => {
125
+ if (data.error) {
126
+ addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
127
+ } else {
128
+ const menuItems = data.menu_items || [];
129
+ addMessage('bot', 'Here are some fish-based dishes:');
130
+ displayMenuItems(menuItems);
131
+ }
132
+ })
133
+ .catch(error => {
134
+ addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
135
+ });
136
+ }
137
 
138
  function displayIngredientsList(ingredients) {
139
  const chatMessages = document.getElementById('chatMessages');
 
142
  return;
143
  }
144
 
 
145
  let ingredientsList = document.querySelector('.ingredients-list');
146
  if (!ingredientsList) {
147
  ingredientsList = document.createElement('div');
 
155
  const item = document.createElement('div');
156
  item.className = 'ingredient-item';
157
  const img = document.createElement('img');
158
+ img.src = ingredient.image_url || 'https://via.placeholder.com/80';
159
  img.alt = ingredient.name;
160
  const name = document.createElement('div');
161
  name.textContent = ingredient.name;
 
177
  });
178
  }
179
 
180
+ function displayMenuItems(menuItems) {
181
+ const chatMessages = document.getElementById('chatMessages');
182
+ if (!chatMessages) {
183
+ console.error('Chat messages container not found for menu items!');
184
+ return;
185
+ }
186
+
187
+ let menuItemsList = document.querySelector('.menu-items-list');
188
+ if (!menuItemsList) {
189
+ menuItemsList = document.createElement('div');
190
+ menuItemsList.className = 'menu-items-list';
191
+ chatMessages.appendChild(menuItemsList);
192
+ } else {
193
+ menuItemsList.innerHTML = '';
194
+ }
195
+
196
+ menuItems.forEach(item => {
197
+ const menuItem = document.createElement('div');
198
+ menuItem.className = 'menu-item';
199
+ const img = document.createElement('img');
200
+ img.src = item.image_url || 'https://via.placeholder.com/80';
201
+ img.alt = item.name;
202
+ const name = document.createElement('div');
203
+ name.textContent = item.name;
204
+ name.style.textAlign = 'center';
205
+ name.style.marginTop = '5px';
206
+ name.style.fontSize = '12px';
207
+ const button = document.createElement('button');
208
+ button.textContent = 'Add to Cart';
209
+ button.onclick = () => {
210
+ selectedMenuItem = item;
211
+ addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
212
+ const options = [
213
+ { text: 'Yes', class: 'green' },
214
+ { text: 'No', class: 'red' }
215
+ ];
216
+ displayOptions(options);
217
+ };
218
+ menuItem.appendChild(img);
219
+ menuItem.appendChild(name);
220
+ menuItem.appendChild(button);
221
+ menuItemsList.appendChild(menuItem);
222
+ });
223
+ }
224
 
225
  function displaySelectedIngredients() {
226
  const chatMessages = document.getElementById('chatMessages');
 
229
  return;
230
  }
231
 
 
232
  let selectedArea = document.querySelector('.selected-ingredients');
233
  if (!selectedArea) {
234
  selectedArea = document.createElement('div');
 
244
  selectedArea.appendChild(div);
245
  });
246
 
 
247
  if (selectedIngredients.length > 0) {
 
248
  let submitButton = document.querySelector('.submit-button');
249
  if (!submitButton) {
250
  submitButton = document.createElement('button');
251
  submitButton.className = 'submit-button';
252
  submitButton.textContent = 'Submit Ingredients';
253
+ submitButton.onclick = submitIngredients;
 
 
254
  chatMessages.appendChild(submitButton);
255
  }
256
  }
257
  }
258
 
259
+ function submitIngredients() {
260
  fetch('/submit_ingredients', {
261
  method: 'POST',
262
  headers: {
 
267
  .then(response => response.json())
268
  .then(data => {
269
  if (data.success) {
270
+ addMessage('bot', 'Great choice! Would you like to add any special instructions for your dish?');
271
+ displayCustomizationInput();
 
 
 
 
 
 
 
 
 
 
 
 
 
272
  } else {
273
  addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
274
  }
 
278
  });
279
  }
280
 
281
+ function displayCustomizationInput() {
282
  const chatMessages = document.getElementById('chatMessages');
283
  if (!chatMessages) {
284
+ console.error('Chat messages container not found for customization input!');
285
  return;
286
  }
 
 
 
 
 
 
 
 
287
 
288
+ let customizationArea = document.querySelector('.customization-input');
289
+ if (!customizationArea) {
290
+ customizationArea = document.createElement('div');
291
+ customizationArea.className = 'customization-input';
292
+ const textarea = document.createElement('textarea');
293
+ textarea.placeholder = 'Enter any special instructions...';
294
+ const submitButton = document.createElement('button');
295
+ submitButton.textContent = 'Submit Instructions';
296
+ submitButton.onclick = () => {
297
+ const instructions = textarea.value.trim();
298
+ if (instructions) {
299
+ addMessage('user', instructions);
300
+ conversation.push({ role: 'user', message: instructions });
301
+ }
302
+ addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
303
+ addMessage('bot', 'Item added to cart! Would you like to order more?');
304
+ const options = [
305
+ { text: 'Yes', class: 'green' },
306
+ { text: 'No', class: 'red' }
307
+ ];
308
+ displayOptions(options);
309
+ selectedMenuItem = null;
310
+ selectedIngredients = [];
311
+ };
312
+ customizationArea.appendChild(textarea);
313
+ customizationArea.appendChild(submitButton);
314
+ chatMessages.appendChild(customizationArea);
315
+ }
316
+ }
317
 
318
+ function addToCart(item) {
319
+ cart.push(item);
320
+ console.log('Cart:', cart);
321
+ }
322
 
323
  function displayOptions(options) {
324
  const chatMessages = document.getElementById('chatMessages');
 
333
  button.onclick = () => {
334
  addMessage('user', opt.text);
335
  conversation.push({ role: 'user', message: opt.text });
336
+ chatMessages.innerHTML = '';
337
  conversation.forEach(msg => addMessage(msg.role, msg.message));
338
  setTimeout(() => handleResponse(opt.text), 500);
339
  };
 
344
  backButton.textContent = 'Go Back';
345
  backButton.className = 'option-button';
346
  backButton.onclick = () => {
347
+ conversation.pop();
348
+ selectedIngredients = [];
349
+ selectedMenuItem = null;
350
+ chatMessages.innerHTML = '';
351
  conversation.forEach(msg => addMessage(msg.role, msg.message));
352
  setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
353
  };
354
  chatMessages.appendChild(backButton);
355
  }
356
 
 
357
  document.getElementById('userInput').addEventListener('keypress', function(e) {
358
  if (e.key === 'Enter') {
359
  sendMessage();
360
  }
361
  });
362
 
 
363
  console.log('Script loaded successfully');