Yaswanth56 commited on
Commit
8c53fce
·
verified ·
1 Parent(s): b658d64

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +24 -23
static/script.js CHANGED
@@ -45,27 +45,20 @@ function handleResponse(userInput) {
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') || lastMessage.includes('non-vegetarian')) {
51
- if (lastMessage.includes('vegetarian')) {
52
- conversation.push({ role: 'user', message: 'Vegetarian' });
53
- console.log("Food preference selected: Vegetarian");
54
- botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
55
- fetchIngredients('vegetable');
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! 🍽️ Here are some non-vegetarian ingredients:';
60
- fetchIngredients('non-vegetarian');
61
- }
62
  return;
63
  } else if (selectedIngredients.length > 0) {
64
  fetchMenuItemsByIngredients();
65
  return;
66
  } else if (lastMessage.includes('yes') && selectedMenuItem) {
67
  botResponse = 'Here are some ingredients to customize your dish:';
68
- fetchIngredients('non-vegetarian'); // Fetch non-veg ingredients for customization
69
  return;
70
  } else if (lastMessage.includes('no') && selectedMenuItem) {
71
  addToCart(selectedMenuItem);
@@ -83,13 +76,13 @@ function handleResponse(userInput) {
83
  }
84
  }
85
 
86
- function fetchIngredients(foodPreference) {
87
  fetch('/get_ingredients', {
88
  method: 'POST',
89
  headers: {
90
  'Content-Type': 'application/json',
91
  },
92
- body: JSON.stringify({ dietary_preference: foodPreference })
93
  })
94
  .then(response => response.json())
95
  .then(data => {
@@ -97,9 +90,10 @@ function fetchIngredients(foodPreference) {
97
  addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
98
  } else {
99
  const ingredients = data.ingredients || [];
100
- addMessage('bot', 'Please select ingredients:');
101
  displayIngredientsList(ingredients);
102
  displaySelectedIngredients();
 
103
  }
104
  })
105
  .catch(error => {
@@ -303,6 +297,8 @@ function displayOptions(options) {
303
  console.error('Chat messages container not found for options!');
304
  return;
305
  }
 
 
306
  options.forEach(opt => {
307
  const button = document.createElement('button');
308
  button.textContent = opt.text;
@@ -310,9 +306,7 @@ function displayOptions(options) {
310
  button.onclick = () => {
311
  addMessage('user', opt.text);
312
  conversation.push({ role: 'user', message: opt.text });
313
- chatMessages.innerHTML = '';
314
- conversation.forEach(msg => addMessage(msg.role, msg.message));
315
- setTimeout(() => handleResponse(opt.text), 500);
316
  };
317
  chatMessages.appendChild(button);
318
  });
@@ -321,20 +315,27 @@ function displayOptions(options) {
321
  backButton.textContent = 'Go Back';
322
  backButton.className = 'option-button';
323
  backButton.onclick = () => {
324
- conversation.pop();
 
 
325
  selectedIngredients = [];
326
- selectedMenuItem = null;
327
  chatMessages.innerHTML = '';
328
  conversation.forEach(msg => addMessage(msg.role, msg.message));
329
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
 
 
 
 
330
  };
331
  chatMessages.appendChild(backButton);
332
  }
333
 
 
334
  document.getElementById('userInput').addEventListener('keypress', function(e) {
335
  if (e.key === 'Enter') {
336
  sendMessage();
337
  }
338
  });
339
 
 
340
  console.log('Script loaded successfully');
 
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
+ { 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);
 
76
  }
77
  }
78
 
79
+ function fetchIngredients(dietaryPreference) {
80
  fetch('/get_ingredients', {
81
  method: 'POST',
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 => {
 
90
  addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
91
  } else {
92
  const ingredients = data.ingredients || [];
93
+ addMessage('bot', 'Great choice! These are available ingredients:');
94
  displayIngredientsList(ingredients);
95
  displaySelectedIngredients();
96
+ console.log(`Ingredients fetched for ${dietaryPreference}:`, ingredients);
97
  }
98
  })
99
  .catch(error => {
 
297
  console.error('Chat messages container not found for options!');
298
  return;
299
  }
300
+ // Clear existing content
301
+ chatMessages.innerHTML = '';
302
  options.forEach(opt => {
303
  const button = document.createElement('button');
304
  button.textContent = opt.text;
 
306
  button.onclick = () => {
307
  addMessage('user', opt.text);
308
  conversation.push({ role: 'user', message: opt.text });
309
+ handleResponse(opt.text); // Process the selection immediately
 
 
310
  };
311
  chatMessages.appendChild(button);
312
  });
 
315
  backButton.textContent = 'Go Back';
316
  backButton.className = 'option-button';
317
  backButton.onclick = () => {
318
+ // Reset conversation to initial state
319
+ const userName = conversation.length > 1 ? conversation[1].message : 'User';
320
+ conversation = [{ role: 'bot', message: `Hi there! I'm Chef Bot! May I know your name?` }, { role: 'user', message: userName }, { role: 'bot', message: `Nice to meet you, ${userName}! 😊 Let's create your perfect meal! What type of food would you prefer?` }];
321
  selectedIngredients = [];
 
322
  chatMessages.innerHTML = '';
323
  conversation.forEach(msg => addMessage(msg.role, msg.message));
324
+ displayOptions([
325
+ { text: 'Vegetarian', class: 'green' },
326
+ { text: 'Non-Vegetarian', class: 'red' },
327
+ { text: 'Both', class: 'gray' }
328
+ ]);
329
  };
330
  chatMessages.appendChild(backButton);
331
  }
332
 
333
+ // Add event listener for Enter key
334
  document.getElementById('userInput').addEventListener('keypress', function(e) {
335
  if (e.key === 'Enter') {
336
  sendMessage();
337
  }
338
  });
339
 
340
+ // Initial load check
341
  console.log('Script loaded successfully');