nagasurendra commited on
Commit
450d774
·
verified ·
1 Parent(s): bc0ec12

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +80 -168
static/script.js CHANGED
@@ -36,7 +36,6 @@ function sendMessage() {
36
  }
37
  }
38
 
39
-
40
  function handleResponse(userInput) {
41
  const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
42
  let botResponse = '';
@@ -53,14 +52,20 @@ function handleResponse(userInput) {
53
  botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
54
  fetchIngredients('non-vegetarian');
55
  return;
 
56
  } else if (lastMessage.includes('vegetarian')) {
57
  conversation.push({ role: 'user', message: 'Vegetarian' });
58
  botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
59
  fetchIngredients('vegetarian');
60
  return;
 
 
 
 
 
61
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
62
- botResponse = 'Here are some ingredients with both options:';
63
- fetchIngredients('both'); // Show both vegetarian and non-vegetarian ingredients
64
  return;
65
  } else if (lastMessage.includes('no') && selectedMenuItem) {
66
  addToCart(selectedMenuItem);
@@ -70,6 +75,10 @@ function handleResponse(userInput) {
70
  { text: 'No', class: 'red' }
71
  ];
72
  selectedMenuItem = null;
 
 
 
 
73
  }
74
 
75
  addMessage('bot', botResponse);
@@ -102,7 +111,29 @@ function fetchIngredients(foodPreference) {
102
  });
103
  }
104
 
105
- // This will remain the same as Step 3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
106
  function displayIngredientsList(ingredients) {
107
  const chatMessages = document.getElementById('chatMessages');
108
  if (!chatMessages) {
@@ -168,149 +199,17 @@ function displaySelectedIngredients() {
168
  });
169
 
170
  if (selectedIngredients.length > 0) {
171
- // New functionality after clicking "Yes" (Step 5 onward)
172
- let instructionsArea = document.querySelector('.instructions-area');
173
- if (!instructionsArea) {
174
- instructionsArea = document.createElement('div');
175
- instructionsArea.className = 'instructions-area';
176
- const textarea = document.createElement('textarea');
177
- textarea.placeholder = 'Enter any special instructions...';
178
- const submitButton = document.createElement('button');
179
- submitButton.textContent = 'Submit';
180
- submitButton.onclick = () => {
181
- const instructions = textarea.value.trim();
182
- if (instructions) {
183
- addMessage('user', instructions);
184
- conversation.push({ role: 'user', message: instructions });
185
- }
186
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
187
- addMessage('bot', 'Item added to cart! Would you like to order more?');
188
- const options = [
189
- { text: 'Yes', class: 'green' },
190
- { text: 'No', class: 'red' }
191
- ];
192
- displayOptions(options);
193
- selectedMenuItem = null;
194
- selectedIngredients = [];
195
- };
196
- instructionsArea.appendChild(textarea);
197
- instructionsArea.appendChild(submitButton);
198
- chatMessages.appendChild(instructionsArea);
199
  }
200
  }
201
  }
202
 
203
- function addToCart(item) {
204
- cart.push(item);
205
- console.log('Cart:', cart);
206
- }
207
-
208
- function displayOptions(options) {
209
- const chatMessages = document.getElementById('chatMessages');
210
- if (!chatMessages) {
211
- console.error('Chat messages container not found for options!');
212
- return;
213
- }
214
- options.forEach(opt => {
215
- const button = document.createElement('button');
216
- button.textContent = opt.text;
217
- button.className = `option-button ${opt.class}`;
218
- button.onclick = () => {
219
- addMessage('user', opt.text);
220
- conversation.push({ role: 'user', message: opt.text });
221
- chatMessages.innerHTML = '';
222
- conversation.forEach(msg => addMessage(msg.role, msg.message));
223
- setTimeout(() => handleResponse(opt.text), 500);
224
- };
225
- chatMessages.appendChild(button);
226
- });
227
- chatMessages.appendChild(document.createElement('br'));
228
- const backButton = document.createElement('button');
229
- backButton.textContent = 'Go Back';
230
- backButton.className = 'option-button';
231
- backButton.onclick = () => {
232
- conversation.pop();
233
- selectedIngredients = [];
234
- selectedMenuItem = null;
235
- chatMessages.innerHTML = '';
236
- conversation.forEach(msg => addMessage(msg.role, msg.message));
237
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
238
- };
239
- chatMessages.appendChild(backButton);
240
- }
241
-
242
-
243
-
244
- function fetchMenuItems(category) {
245
- fetch('/get_menu_items', {
246
- method: 'POST',
247
- headers: {
248
- 'Content-Type': 'application/json',
249
- },
250
- body: JSON.stringify({ category: category })
251
- })
252
- .then(response => response.json())
253
- .then(data => {
254
- if (data.error) {
255
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
256
- } else {
257
- const menuItems = data.menu_items || [];
258
- addMessage('bot', 'Here are some dishes based on your selection:');
259
- displayMenuItems(menuItems);
260
- }
261
- })
262
- .catch(error => {
263
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
264
- });
265
- }
266
-
267
-
268
- function displayMenuItems(menuItems) {
269
- const chatMessages = document.getElementById('chatMessages');
270
- if (!chatMessages) {
271
- console.error('Chat messages container not found for menu items!');
272
- return;
273
- }
274
-
275
- let menuItemsList = document.querySelector('.menu-items-list');
276
- if (!menuItemsList) {
277
- menuItemsList = document.createElement('div');
278
- menuItemsList.className = 'menu-items-list';
279
- chatMessages.appendChild(menuItemsList);
280
- } else {
281
- menuItemsList.innerHTML = '';
282
- }
283
-
284
- menuItems.forEach(item => {
285
- const menuItem = document.createElement('div');
286
- menuItem.className = 'menu-item';
287
- const img = document.createElement('img');
288
- img.src = item.image_url || 'https://via.placeholder.com/80';
289
- img.alt = item.name;
290
- const name = document.createElement('div');
291
- name.textContent = item.name;
292
- name.style.textAlign = 'center';
293
- name.style.marginTop = '5px';
294
- name.style.fontSize = '12px';
295
- const button = document.createElement('button');
296
- button.textContent = 'Add to Cart';
297
- button.onclick = () => {
298
- selectedMenuItem = item;
299
- addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
300
- const options = [
301
- { text: 'Yes', class: 'green' },
302
- { text: 'No', class: 'red' }
303
- ];
304
- displayOptions(options);
305
- };
306
- menuItem.appendChild(img);
307
- menuItem.appendChild(name);
308
- menuItem.appendChild(button);
309
- menuItemsList.appendChild(menuItem);
310
- });
311
- }
312
-
313
-
314
  function submitIngredients() {
315
  fetch('/submit_ingredients', {
316
  method: 'POST',
@@ -336,30 +235,6 @@ function submitIngredients() {
336
  });
337
  }
338
 
339
- function fetchMenuItems(ingredientNames) {
340
- fetch('/get_menu_items', {
341
- method: 'POST',
342
- headers: {
343
- 'Content-Type': 'application/json',
344
- },
345
- body: JSON.stringify({ ingredient_names: ingredientNames })
346
- })
347
- .then(response => response.json())
348
- .then(data => {
349
- if (data.error) {
350
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
351
- } else {
352
- const menuItems = data.menu_items || [];
353
- addMessage('bot', 'Here are some dishes based on your selected ingredients:');
354
- displayMenuItems(menuItems);
355
- }
356
- })
357
- .catch(error => {
358
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
359
- });
360
- }
361
-
362
-
363
  function displayCustomizationInput() {
364
  const chatMessages = document.getElementById('chatMessages');
365
  if (!chatMessages) {
@@ -397,7 +272,44 @@ function displayCustomizationInput() {
397
  }
398
  }
399
 
 
 
 
 
400
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
401
 
402
  document.getElementById('userInput').addEventListener('keypress', function(e) {
403
  if (e.key === 'Enter') {
@@ -405,4 +317,4 @@ document.getElementById('userInput').addEventListener('keypress', function(e) {
405
  }
406
  });
407
 
408
- console.log('Script loaded successfully');
 
36
  }
37
  }
38
 
 
39
  function handleResponse(userInput) {
40
  const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
41
  let botResponse = '';
 
52
  botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
53
  fetchIngredients('non-vegetarian');
54
  return;
55
+
56
  } else if (lastMessage.includes('vegetarian')) {
57
  conversation.push({ role: 'user', message: 'Vegetarian' });
58
  botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
59
  fetchIngredients('vegetarian');
60
  return;
61
+ } else if (lastMessage.includes('chicken') || lastMessage.includes('beef') || lastMessage.includes('lamb')) {
62
+ conversation.push({ role: 'user', message: lastMessage });
63
+ botResponse = `Great! Here are some ${lastMessage} ingredients available:`;
64
+ fetchIngredients(lastMessage.toLowerCase());
65
+ return;
66
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
67
+ botResponse = 'Here are some ingredients to customize your dish:';
68
+ fetchIngredients('both'); // Fetch both vegetarian and non-vegetarian ingredients for customization
69
  return;
70
  } else if (lastMessage.includes('no') && selectedMenuItem) {
71
  addToCart(selectedMenuItem);
 
75
  { text: 'No', class: 'red' }
76
  ];
77
  selectedMenuItem = null;
78
+ } else if (lastMessage.includes('add') && selectedMenuItem) {
79
+ botResponse = 'Please provide any special instructions for your dish:';
80
+ displayCustomizationInput(); // Display input box for special instructions
81
+ return;
82
  }
83
 
84
  addMessage('bot', botResponse);
 
111
  });
112
  }
113
 
114
+ function fetchMenuItems(category) {
115
+ fetch('/get_menu_items', {
116
+ method: 'POST',
117
+ headers: {
118
+ 'Content-Type': 'application/json',
119
+ },
120
+ body: JSON.stringify({ category: category })
121
+ })
122
+ .then(response => response.json())
123
+ .then(data => {
124
+ if (data.error) {
125
+ addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
126
+ } else {
127
+ const menuItems = data.menu_items || [];
128
+ addMessage('bot', 'Here are some dishes based on your selection:');
129
+ displayMenuItems(menuItems);
130
+ }
131
+ })
132
+ .catch(error => {
133
+ addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
134
+ });
135
+ }
136
+
137
  function displayIngredientsList(ingredients) {
138
  const chatMessages = document.getElementById('chatMessages');
139
  if (!chatMessages) {
 
199
  });
200
 
201
  if (selectedIngredients.length > 0) {
202
+ let submitButton = document.querySelector('.submit-button');
203
+ if (!submitButton) {
204
+ submitButton = document.createElement('button');
205
+ submitButton.className = 'submit-button';
206
+ submitButton.textContent = 'Submit Ingredients';
207
+ submitButton.onclick = submitIngredients;
208
+ chatMessages.appendChild(submitButton);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
209
  }
210
  }
211
  }
212
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
213
  function submitIngredients() {
214
  fetch('/submit_ingredients', {
215
  method: 'POST',
 
235
  });
236
  }
237
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
238
  function displayCustomizationInput() {
239
  const chatMessages = document.getElementById('chatMessages');
240
  if (!chatMessages) {
 
272
  }
273
  }
274
 
275
+ function addToCart(item) {
276
+ cart.push(item);
277
+ console.log('Cart:', cart);
278
+ }
279
 
280
+ function displayOptions(options) {
281
+ const chatMessages = document.getElementById('chatMessages');
282
+ if (!chatMessages) {
283
+ console.error('Chat messages container not found for options!');
284
+ return;
285
+ }
286
+ options.forEach(opt => {
287
+ const button = document.createElement('button');
288
+ button.textContent = opt.text;
289
+ button.className = `option-button ${opt.class}`;
290
+ button.onclick = () => {
291
+ addMessage('user', opt.text);
292
+ conversation.push({ role: 'user', message: opt.text });
293
+ chatMessages.innerHTML = '';
294
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
295
+ setTimeout(() => handleResponse(opt.text), 500);
296
+ };
297
+ chatMessages.appendChild(button);
298
+ });
299
+ chatMessages.appendChild(document.createElement('br'));
300
+ const backButton = document.createElement('button');
301
+ backButton.textContent = 'Go Back';
302
+ backButton.className = 'option-button';
303
+ backButton.onclick = () => {
304
+ conversation.pop();
305
+ selectedIngredients = [];
306
+ selectedMenuItem = null;
307
+ chatMessages.innerHTML = '';
308
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
309
+ setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
310
+ };
311
+ chatMessages.appendChild(backButton);
312
+ }
313
 
314
  document.getElementById('userInput').addEventListener('keypress', function(e) {
315
  if (e.key === 'Enter') {
 
317
  }
318
  });
319
 
320
+ console.log('Script loaded successfully');