geethareddy commited on
Commit
4676698
·
verified ·
1 Parent(s): 5e8f507

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +12 -16
static/script.js CHANGED
@@ -47,8 +47,8 @@ function handleResponse(userInput) {
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
 
@@ -75,6 +75,7 @@ function fetchIngredients(dietaryPreference) {
75
  addMessage('bot', 'Great choice! These are available ingredients:');
76
  displayIngredientsList(ingredients);
77
  displaySelectedIngredients();
 
78
  console.log(`Ingredients fetched for ${dietaryPreference}:`, ingredients);
79
  }
80
  })
@@ -106,7 +107,7 @@ function displayIngredientsList(ingredients) {
106
  img.src = ingredient.image_url || 'https://via.placeholder.com/80';
107
  img.alt = ingredient.name;
108
  const name = document.createElement('div');
109
- name.textContent = ingredient.name;
110
  name.style.textAlign = 'center';
111
  name.style.marginTop = '5px';
112
  name.style.fontSize = '12px';
@@ -143,7 +144,7 @@ function displaySelectedIngredients() {
143
 
144
  selectedIngredients.forEach(ingredient => {
145
  const div = document.createElement('div');
146
- div.textContent = ingredient.name;
147
  selectedArea.appendChild(div);
148
  });
149
  }
@@ -154,8 +155,6 @@ function displayOptions(options) {
154
  console.error('Chat messages container not found for options!');
155
  return;
156
  }
157
- // Clear existing options and messages
158
- chatMessages.innerHTML = '';
159
  options.forEach(opt => {
160
  const button = document.createElement('button');
161
  button.textContent = opt.text;
@@ -163,7 +162,9 @@ function displayOptions(options) {
163
  button.onclick = () => {
164
  addMessage('user', opt.text);
165
  conversation.push({ role: 'user', message: opt.text });
166
- handleResponse(opt.text); // Directly handle response without clearing
 
 
167
  };
168
  chatMessages.appendChild(button);
169
  });
@@ -172,16 +173,11 @@ function displayOptions(options) {
172
  backButton.textContent = 'Go Back';
173
  backButton.className = 'option-button';
174
  backButton.onclick = () => {
175
- conversation = [{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }]; // Reset conversation
176
  selectedIngredients = []; // Clear selected ingredients
177
- chatMessages.innerHTML = ''; // Clear messages
178
- const userName = conversation[0].message.split('!')[0].split('name?')[0].trim().split(' ')[4]; // Extract name
179
- addMessage('bot', `Nice to meet you, ${userName}! 😊 Let's create your perfect meal! What type of food would you prefer?`);
180
- displayOptions([
181
- { text: 'Vegetarian', class: 'green' },
182
- { text: 'Non-Vegetarian', class: 'red' },
183
- { text: 'Both', class: 'gray' }
184
- ]);
185
  };
186
  chatMessages.appendChild(backButton);
187
  }
 
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} (including any "Both" category):`;
51
+ fetchIngredients(lastMessage.toLowerCase());
52
  return;
53
  }
54
 
 
75
  addMessage('bot', 'Great choice! These are available ingredients:');
76
  displayIngredientsList(ingredients);
77
  displaySelectedIngredients();
78
+ // Log ingredients to console for debugging
79
  console.log(`Ingredients fetched for ${dietaryPreference}:`, ingredients);
80
  }
81
  })
 
107
  img.src = ingredient.image_url || 'https://via.placeholder.com/80';
108
  img.alt = ingredient.name;
109
  const name = document.createElement('div');
110
+ name.textContent = ingredient.name; // Removed category from display
111
  name.style.textAlign = 'center';
112
  name.style.marginTop = '5px';
113
  name.style.fontSize = '12px';
 
144
 
145
  selectedIngredients.forEach(ingredient => {
146
  const div = document.createElement('div');
147
+ div.textContent = ingredient.name; // Removed category from display
148
  selectedArea.appendChild(div);
149
  });
150
  }
 
155
  console.error('Chat messages container not found for options!');
156
  return;
157
  }
 
 
158
  options.forEach(opt => {
159
  const button = document.createElement('button');
160
  button.textContent = opt.text;
 
162
  button.onclick = () => {
163
  addMessage('user', opt.text);
164
  conversation.push({ role: 'user', message: opt.text });
165
+ chatMessages.innerHTML = ''; // Clear previous messages
166
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
167
+ setTimeout(() => handleResponse(opt.text), 500);
168
  };
169
  chatMessages.appendChild(button);
170
  });
 
173
  backButton.textContent = 'Go Back';
174
  backButton.className = 'option-button';
175
  backButton.onclick = () => {
176
+ conversation.pop(); // Remove last user input
177
  selectedIngredients = []; // Clear selected ingredients
178
+ chatMessages.innerHTML = ''; // Clear previous messages
179
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
180
+ setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
 
 
 
 
 
181
  };
182
  chatMessages.appendChild(backButton);
183
  }