Yaswanth56 commited on
Commit
3c60197
·
verified ·
1 Parent(s): 47b8baa

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +63 -135
static/script.js CHANGED
@@ -41,8 +41,20 @@ function handleResponse(userInput) {
41
  let botResponse = '';
42
  let options = [];
43
 
44
- // Handle name input (first user response after bot's greeting)
45
- if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
 
 
 
 
 
 
 
 
 
 
 
 
46
  botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
47
  options = [
48
  { text: 'Vegetarian', class: 'green' },
@@ -127,27 +139,24 @@ function fetchIngredients(foodPreference) {
127
  });
128
  }
129
 
130
- function fetchMenuItems(category) {
131
- fetch('/get_menu_items', {
132
- method: 'POST',
133
- headers: {
134
- 'Content-Type': 'application/json',
135
- },
136
- body: JSON.stringify({ category: category })
137
- })
138
- .then(response => response.json())
139
- .then(data => {
140
- if (data.error) {
141
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
142
- } else {
143
- const menuItems = data.menu_items || [];
144
- addMessage('bot', 'Here are some dishes based on your selection:');
145
- displayMenuItems(menuItems);
146
- }
147
- })
148
- .catch(error => {
149
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
150
  });
 
151
  }
152
 
153
  function displayIngredientsList(ingredients) {
@@ -192,51 +201,6 @@ function displayIngredientsList(ingredients) {
192
  });
193
  }
194
 
195
- function displayMenuItems(menuItems) {
196
- const chatMessages = document.getElementById('chatMessages');
197
- if (!chatMessages) {
198
- console.error('Chat messages container not found for menu items!');
199
- return;
200
- }
201
-
202
- let menuItemsList = document.querySelector('.menu-items-list');
203
- if (!menuItemsList) {
204
- menuItemsList = document.createElement('div');
205
- menuItemsList.className = 'menu-items-list';
206
- chatMessages.appendChild(menuItemsList);
207
- } else {
208
- menuItemsList.innerHTML = '';
209
- }
210
-
211
- menuItems.forEach(item => {
212
- const menuItem = document.createElement('div');
213
- menuItem.className = 'menu-item';
214
- const img = document.createElement('img');
215
- img.src = item.image_url || 'https://via.placeholder.com/80';
216
- img.alt = item.name;
217
- const name = document.createElement('div');
218
- name.textContent = item.name;
219
- name.style.textAlign = 'center';
220
- name.style.marginTop = '5px';
221
- name.style.fontSize = '12px';
222
- const button = document.createElement('button');
223
- button.textContent = 'Add to Cart';
224
- button.onclick = () => {
225
- selectedMenuItem = item;
226
- addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
227
- const options = [
228
- { text: 'Yes', class: 'green' },
229
- { text: 'No', class: 'red' }
230
- ];
231
- displayOptions(options);
232
- };
233
- menuItem.appendChild(img);
234
- menuItem.appendChild(name);
235
- menuItem.appendChild(button);
236
- menuItemsList.appendChild(menuItem);
237
- });
238
- }
239
-
240
  function displaySelectedIngredients() {
241
  const chatMessages = document.getElementById('chatMessages');
242
  if (!chatMessages) {
@@ -316,85 +280,49 @@ function fetchMenuItems(ingredientNames) {
316
  });
317
  }
318
 
319
- function displayCustomizationInput() {
320
  const chatMessages = document.getElementById('chatMessages');
321
  if (!chatMessages) {
322
- console.error('Chat messages container not found for customization input!');
323
  return;
324
  }
325
 
326
- let customizationArea = document.querySelector('.customization-input');
327
- if (!customizationArea) {
328
- customizationArea = document.createElement('div');
329
- customizationArea.className = 'customization-input';
330
- const textarea = document.createElement('textarea');
331
- textarea.placeholder = 'Enter any special instructions...';
332
- const submitButton = document.createElement('button');
333
- submitButton.textContent = 'Submit Instructions';
334
- submitButton.onclick = () => {
335
- const instructions = textarea.value.trim();
336
- if (instructions) {
337
- addMessage('user', instructions);
338
- conversation.push({ role: 'user', message: instructions });
339
- }
340
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
341
- addMessage('bot', 'Item added to cart! Would you like to order more?');
 
 
 
 
 
 
 
 
 
342
  const options = [
343
  { text: 'Yes', class: 'green' },
344
  { text: 'No', class: 'red' }
345
  ];
346
  displayOptions(options);
347
- selectedMenuItem = null;
348
- selectedIngredients = [];
349
- };
350
- customizationArea.appendChild(textarea);
351
- customizationArea.appendChild(submitButton);
352
- chatMessages.appendChild(customizationArea);
353
- }
354
- }
355
-
356
- function addToCart(item) {
357
- cart.push(item);
358
- console.log('Cart:', cart);
359
- }
360
-
361
- function displayOptions(options) {
362
- const chatMessages = document.getElementById('chatMessages');
363
- if (!chatMessages) {
364
- console.error('Chat messages container not found for options!');
365
- return;
366
- }
367
- options.forEach(opt => {
368
- const button = document.createElement('button');
369
- button.textContent = opt.text;
370
- button.className = `option-button ${opt.class}`;
371
- button.onclick = () => {
372
- addMessage('user', opt.text);
373
- conversation.push({ role: 'user', message: opt.text });
374
- setTimeout(() => handleResponse(opt.text), 500);
375
  };
376
- chatMessages.appendChild(button);
 
 
 
377
  });
378
- chatMessages.appendChild(document.createElement('br'));
379
- if (conversation.length > 1) { // Ensure back button appears only when there's a previous step
380
- const backButton = document.createElement('button');
381
- backButton.textContent = 'Go Back';
382
- backButton.className = 'option-button';
383
- backButton.onclick = () => {
384
- if (conversation.length > 1) {
385
- conversation.pop(); // Remove the last conversation entry
386
- selectedIngredients = []; // Clear selected ingredients if going back
387
- selectedMenuItem = null; // Clear selected menu item
388
- chatMessages.innerHTML = ''; // Clear current messages
389
- conversation.forEach(msg => addMessage(msg.role, msg.message)); // Re-render conversation
390
- // Trigger response based on the now-last message
391
- if (conversation.length > 0) {
392
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
393
- }
394
- }
395
- };
396
- chatMessages.appendChild(backButton);
397
- }
398
  }
399
 
400
  document.getElementById('userInput').addEventListener('keypress', function(e) {
@@ -403,4 +331,4 @@ document.getElementById('userInput').addEventListener('keypress', function(e) {
403
  }
404
  });
405
 
406
- console.log('Script loaded successfully');
 
41
  let botResponse = '';
42
  let options = [];
43
 
44
+ // FAQ handling logic
45
+ if (lastMessage.includes("how do i contact customer support")) {
46
+ botResponse = "You can email us at [email protected].";
47
+ } else if (lastMessage.includes("what are your business hours")) {
48
+ botResponse = "We are open from 9 AM to 6 PM, Monday to Friday.";
49
+ } else if (lastMessage.includes("how do i reset my password")) {
50
+ botResponse = "Click on 'Forgot Password' on the login page.";
51
+ } else if (lastMessage.includes("how do i cancel my subscription")) {
52
+ botResponse = "You can cancel your subscription by visiting the 'Account Settings' page and selecting 'Cancel Subscription'.";
53
+ } else if (lastMessage.includes("are there any discounts available")) {
54
+ botResponse = "We offer seasonal discounts and promotions. Keep an eye on our website or subscribe to our newsletter for updates.";
55
+ }
56
+ // Custom meal preference handling
57
+ else if (conversation.length === 2 && conversation[0].role === 'bot' && conversation[0].message.includes('May I know your name?')) {
58
  botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
59
  options = [
60
  { text: 'Vegetarian', class: 'green' },
 
139
  });
140
  }
141
 
142
+ function displayOptions(options) {
143
+ const chatMessages = document.getElementById('chatMessages');
144
+ if (!chatMessages) {
145
+ console.error('Chat messages container not found for options!');
146
+ return;
147
+ }
148
+ options.forEach(opt => {
149
+ const button = document.createElement('button');
150
+ button.textContent = opt.text;
151
+ button.className = `option-button ${opt.class}`;
152
+ button.onclick = () => {
153
+ addMessage('user', opt.text);
154
+ conversation.push({ role: 'user', message: opt.text });
155
+ setTimeout(() => handleResponse(opt.text), 500);
156
+ };
157
+ chatMessages.appendChild(button);
 
 
 
 
158
  });
159
+ chatMessages.appendChild(document.createElement('br'));
160
  }
161
 
162
  function displayIngredientsList(ingredients) {
 
201
  });
202
  }
203
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
204
  function displaySelectedIngredients() {
205
  const chatMessages = document.getElementById('chatMessages');
206
  if (!chatMessages) {
 
280
  });
281
  }
282
 
283
+ function displayMenuItems(menuItems) {
284
  const chatMessages = document.getElementById('chatMessages');
285
  if (!chatMessages) {
286
+ console.error('Chat messages container not found for menu items!');
287
  return;
288
  }
289
 
290
+ let menuItemsList = document.querySelector('.menu-items-list');
291
+ if (!menuItemsList) {
292
+ menuItemsList = document.createElement('div');
293
+ menuItemsList.className = 'menu-items-list';
294
+ chatMessages.appendChild(menuItemsList);
295
+ } else {
296
+ menuItemsList.innerHTML = '';
297
+ }
298
+
299
+ menuItems.forEach(item => {
300
+ const menuItem = document.createElement('div');
301
+ menuItem.className = 'menu-item';
302
+ const img = document.createElement('img');
303
+ img.src = item.image_url || 'https://via.placeholder.com/80';
304
+ img.alt = item.name;
305
+ const name = document.createElement('div');
306
+ name.textContent = item.name;
307
+ name.style.textAlign = 'center';
308
+ name.style.marginTop = '5px';
309
+ name.style.fontSize = '12px';
310
+ const button = document.createElement('button');
311
+ button.textContent = 'Add to Cart';
312
+ button.onclick = () => {
313
+ selectedMenuItem = item;
314
+ addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
315
  const options = [
316
  { text: 'Yes', class: 'green' },
317
  { text: 'No', class: 'red' }
318
  ];
319
  displayOptions(options);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
320
  };
321
+ menuItem.appendChild(img);
322
+ menuItem.appendChild(name);
323
+ menuItem.appendChild(button);
324
+ menuItemsList.appendChild(menuItem);
325
  });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
  }
327
 
328
  document.getElementById('userInput').addEventListener('keypress', function(e) {
 
331
  }
332
  });
333
 
334
+ console.log('Script loaded successfully');