Yaswanth56 commited on
Commit
97c1eee
·
verified ·
1 Parent(s): 51d39c9

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +3 -146
static/script.js CHANGED
@@ -1,9 +1,7 @@
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');
@@ -49,25 +47,9 @@ function handleResponse(userInput) {
49
  { text: 'Both', class: 'gray' }
50
  ];
51
  } else if (lastMessage.includes('vegetarian') || lastMessage.includes('non-vegetarian') || lastMessage.includes('both')) {
52
- let dietaryPreference = lastMessage;
53
  botResponse = `Great choice! 🍽️ These are the available ingredients for ${lastMessage}:`;
54
- fetchIngredients(dietaryPreference);
55
  return;
56
- } else if (selectedIngredients.length > 0) {
57
- fetchMenuItemsByIngredients();
58
- return;
59
- } else if (lastMessage.includes('yes') && selectedMenuItem) {
60
- botResponse = 'Here are some ingredients to customize your dish:';
61
- fetchIngredients('both'); // Allow customization from all ingredients
62
- return;
63
- } else if (lastMessage.includes('no') && selectedMenuItem) {
64
- addToCart(selectedMenuItem);
65
- botResponse = 'Item added to cart! Would you like to order more?';
66
- options = [
67
- { text: 'Yes', class: 'green' },
68
- { text: 'No', class: 'red' }
69
- ];
70
- selectedMenuItem = null;
71
  }
72
 
73
  addMessage('bot', botResponse);
@@ -82,7 +64,7 @@ function fetchIngredients(dietaryPreference) {
82
  headers: {
83
  'Content-Type': 'application/json',
84
  },
85
- body: JSON.stringify({ dietary_preference: dietaryPreference.toLowerCase() })
86
  })
87
  .then(response => response.json())
88
  .then(data => {
@@ -101,33 +83,6 @@ function fetchIngredients(dietaryPreference) {
101
  });
102
  }
103
 
104
- function fetchMenuItemsByIngredients() {
105
- const ingredientNames = selectedIngredients.map(ing => ing.name).join(',');
106
- fetch('/get_menu_items_by_ingredients', {
107
- method: 'POST',
108
- headers: {
109
- 'Content-Type': 'application/json',
110
- },
111
- body: JSON.stringify({ ingredients: ingredientNames })
112
- })
113
- .then(response => response.json())
114
- .then(data => {
115
- if (data.error) {
116
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
117
- } else {
118
- const menuItems = data.menu_items || [];
119
- addMessage('bot', 'Based on your ingredients, here are some menu items:');
120
- displayMenuItems(menuItems);
121
- // Remove submit button
122
- const submitButton = document.querySelector('.submit-button');
123
- if (submitButton) submitButton.remove();
124
- }
125
- })
126
- .catch(error => {
127
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
128
- });
129
- }
130
-
131
  function displayIngredientsList(ingredients) {
132
  const chatMessages = document.getElementById('chatMessages');
133
  if (!chatMessages) {
@@ -170,51 +125,6 @@ function displayIngredientsList(ingredients) {
170
  });
171
  }
172
 
173
- function displayMenuItems(menuItems) {
174
- const chatMessages = document.getElementById('chatMessages');
175
- if (!chatMessages) {
176
- console.error('Chat messages container not found for menu items!');
177
- return;
178
- }
179
-
180
- let menuItemsList = document.querySelector('.menu-items-list');
181
- if (!menuItemsList) {
182
- menuItemsList = document.createElement('div');
183
- menuItemsList.className = 'menu-items-list';
184
- chatMessages.appendChild(menuItemsList);
185
- } else {
186
- menuItemsList.innerHTML = '';
187
- }
188
-
189
- menuItems.forEach(item => {
190
- const menuItem = document.createElement('div');
191
- menuItem.className = 'menu-item';
192
- const img = document.createElement('img');
193
- img.src = item.image_url || 'https://via.placeholder.com/80';
194
- img.alt = item.name;
195
- const name = document.createElement('div');
196
- name.textContent = item.name;
197
- name.style.textAlign = 'center';
198
- name.style.marginTop = '5px';
199
- name.style.fontSize = '12px';
200
- const button = document.createElement('button');
201
- button.textContent = 'Select Item';
202
- button.onclick = () => {
203
- selectedMenuItem = item;
204
- addMessage('bot', 'You’ve selected a delicious dish! Would you like to customize it with ingredients?');
205
- const options = [
206
- { text: 'Yes', class: 'green' },
207
- { text: 'No', class: 'red' }
208
- ];
209
- displayOptions(options);
210
- };
211
- menuItem.appendChild(img);
212
- menuItem.appendChild(name);
213
- menuItem.appendChild(button);
214
- menuItemsList.appendChild(menuItem);
215
- });
216
- }
217
-
218
  function displaySelectedIngredients() {
219
  const chatMessages = document.getElementById('chatMessages');
220
  if (!chatMessages) {
@@ -236,59 +146,6 @@ function displaySelectedIngredients() {
236
  div.textContent = ingredient.name;
237
  selectedArea.appendChild(div);
238
  });
239
-
240
- if (selectedIngredients.length > 0 && !document.querySelector('.menu-items-list')) {
241
- let submitButton = document.querySelector('.submit-button');
242
- if (!submitButton) {
243
- submitButton = document.createElement('button');
244
- submitButton.className = 'submit-button';
245
- submitButton.textContent = 'Submit Ingredients';
246
- submitButton.onclick = fetchMenuItemsByIngredients;
247
- chatMessages.appendChild(submitButton);
248
- }
249
- }
250
- }
251
-
252
- function displayCustomizationInput() {
253
- const chatMessages = document.getElementById('chatMessages');
254
- if (!chatMessages) {
255
- console.error('Chat messages container not found for customization input!');
256
- return;
257
- }
258
-
259
- let customizationArea = document.querySelector('.customization-input');
260
- if (!customizationArea) {
261
- customizationArea = document.createElement('div');
262
- customizationArea.className = 'customization-input';
263
- const textarea = document.createElement('textarea');
264
- textarea.placeholder = 'Enter any special instructions...';
265
- const submitButton = document.createElement('button');
266
- submitButton.textContent = 'Submit Instructions';
267
- submitButton.onclick = () => {
268
- const instructions = textarea.value.trim();
269
- if (instructions) {
270
- addMessage('user', instructions);
271
- conversation.push({ role: 'user', message: instructions });
272
- }
273
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
274
- addMessage('bot', 'Item added to cart! Would you like to order more?');
275
- const options = [
276
- { text: 'Yes', class: 'green' },
277
- { text: 'No', class: 'red' }
278
- ];
279
- displayOptions(options);
280
- selectedMenuItem = null;
281
- selectedIngredients = [];
282
- };
283
- customizationArea.appendChild(textarea);
284
- customizationArea.appendChild(submitButton);
285
- chatMessages.appendChild(customizationArea);
286
- }
287
- }
288
-
289
- function addToCart(item) {
290
- cart.push(item);
291
- console.log('Cart:', cart);
292
  }
293
 
294
  function displayOptions(options) {
 
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');
 
47
  { text: 'Both', class: 'gray' }
48
  ];
49
  } else if (lastMessage.includes('vegetarian') || lastMessage.includes('non-vegetarian') || lastMessage.includes('both')) {
 
50
  botResponse = `Great choice! 🍽️ These are the available ingredients for ${lastMessage}:`;
51
+ fetchIngredients(lastMessage);
52
  return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  }
54
 
55
  addMessage('bot', botResponse);
 
64
  headers: {
65
  'Content-Type': 'application/json',
66
  },
67
+ body: JSON.stringify({ dietary_preference: dietaryPreference })
68
  })
69
  .then(response => response.json())
70
  .then(data => {
 
83
  });
84
  }
85
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
86
  function displayIngredientsList(ingredients) {
87
  const chatMessages = document.getElementById('chatMessages');
88
  if (!chatMessages) {
 
125
  });
126
  }
127
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
128
  function displaySelectedIngredients() {
129
  const chatMessages = document.getElementById('chatMessages');
130
  if (!chatMessages) {
 
146
  div.textContent = ingredient.name;
147
  selectedArea.appendChild(div);
148
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
149
  }
150
 
151
  function displayOptions(options) {