Yaswanth56 commited on
Commit
e21c304
·
verified ·
1 Parent(s): 726c45e

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +146 -45
static/script.js CHANGED
@@ -2,6 +2,7 @@ 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');
@@ -42,48 +43,52 @@ function handleResponse(userInput) {
42
  let botResponse = '';
43
  let options = [];
44
 
45
- if (conversation.length === 2) { // After name input
 
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' },
49
  { text: 'Non-Vegetarian', class: 'red' }
50
  ];
51
- } else if (lastMessage.includes('vegetarian')) {
52
- botResponse = 'Great choice! 🍽️ We have some amazing vegetarian options! What\'s your dietary preference?';
53
- options = [
54
- { text: 'Vegan', class: '' },
55
- { text: 'Gluten-Free', class: '' },
56
- { text: 'Vegetarian', class: '' },
57
- { text: 'Low Carb', class: '' },
58
- { text: 'Dairy-Free', class: '' },
59
- { text: 'Keto', class: '' },
60
- { text: 'Halal', class: '' }
61
- ];
62
- } else if (lastMessage.includes('non-vegetarian')) {
63
- botResponse = 'Great choice! 🍽️ We have some amazing non-vegetarian options! What\'s your dietary preference?';
64
- options = [
65
- { text: 'Low Carb', class: '' },
66
- { text: 'Dairy-Free', class: '' },
67
- { text: 'Keto', class: '' },
68
- { text: 'Halal', class: '' }
69
- ];
70
- } else if (lastMessage.includes('low carb') || lastMessage.includes('dairy-free') || lastMessage.includes('keto') || lastMessage.includes('halal') || lastMessage.includes('gluten-free') || lastMessage.includes('vegan') || lastMessage.includes('vegetarian')) {
71
- fetchIngredients(lastMessage);
72
- return; // Exit early to handle ingredients fetch
73
  }
74
 
75
  addMessage('bot', botResponse);
76
  if (options.length > 0) {
77
  displayOptions(options);
 
 
 
 
 
78
  }
79
  }
80
 
81
  function fetchIngredients(dietaryPreference) {
82
  fetch('/get_ingredients', {
83
  method: 'POST',
84
- headers: {
85
- 'Content-Type': 'application/json',
86
- },
87
  body: JSON.stringify({ dietary_preference: dietaryPreference })
88
  })
89
  .then(response => response.json())
@@ -109,7 +114,6 @@ function displayIngredientsList(ingredients) {
109
  return;
110
  }
111
 
112
- // Create or update ingredients list
113
  let ingredientsList = document.querySelector('.ingredients-list');
114
  if (!ingredientsList) {
115
  ingredientsList = document.createElement('div');
@@ -123,7 +127,7 @@ function displayIngredientsList(ingredients) {
123
  const item = document.createElement('div');
124
  item.className = 'ingredient-item';
125
  const img = document.createElement('img');
126
- img.src = ingredient.image_url || 'https://via.placeholder.com/80'; // Fallback image
127
  img.alt = ingredient.name;
128
  const name = document.createElement('div');
129
  name.textContent = ingredient.name;
@@ -152,7 +156,6 @@ function displaySelectedIngredients() {
152
  return;
153
  }
154
 
155
- // Create or update selected ingredients area
156
  let selectedArea = document.querySelector('.selected-ingredients');
157
  if (!selectedArea) {
158
  selectedArea = document.createElement('div');
@@ -169,12 +172,121 @@ function displaySelectedIngredients() {
169
  });
170
  }
171
 
172
- function displayOptions(options) {
173
  const chatMessages = document.getElementById('chatMessages');
174
- if (!chatMessages) {
175
- console.error('Chat messages container not found for options!');
176
- return;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  }
 
 
 
 
 
178
  options.forEach(opt => {
179
  const button = document.createElement('button');
180
  button.textContent = opt.text;
@@ -182,24 +294,13 @@ function displayOptions(options) {
182
  button.onclick = () => {
183
  addMessage('user', opt.text);
184
  conversation.push({ role: 'user', message: opt.text });
185
- chatMessages.innerHTML = ''; // Clear previous messages
186
  conversation.forEach(msg => addMessage(msg.role, msg.message));
187
  setTimeout(() => handleResponse(opt.text), 500);
188
  };
189
  chatMessages.appendChild(button);
190
  });
191
  chatMessages.appendChild(document.createElement('br'));
192
- const backButton = document.createElement('button');
193
- backButton.textContent = 'Go Back';
194
- backButton.className = 'option-button';
195
- backButton.onclick = () => {
196
- conversation.pop(); // Remove last user input
197
- selectedIngredients = []; // Clear selected ingredients
198
- chatMessages.innerHTML = ''; // Clear previous messages
199
- conversation.forEach(msg => addMessage(msg.role, msg.message));
200
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
201
- };
202
- chatMessages.appendChild(backButton);
203
  }
204
 
205
  // Add event listener for Enter key
 
2
  { role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }
3
  ];
4
  let selectedIngredients = [];
5
+ let currentStep = 'name'; // Track conversation step: 'name', 'dietary', 'ingredients', 'recipes'
6
 
7
  function addMessage(role, message) {
8
  const chatMessages = document.getElementById('chatMessages');
 
43
  let botResponse = '';
44
  let options = [];
45
 
46
+ if (currentStep === 'name' && conversation.length === 2) {
47
+ currentStep = 'dietary';
48
  botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
49
  options = [
50
  { text: 'Vegetarian', class: 'green' },
51
  { text: 'Non-Vegetarian', class: 'red' }
52
  ];
53
+ } else if (currentStep === 'dietary' && (lastMessage.includes('vegetarian') || lastMessage.includes('non-vegetarian'))) {
54
+ currentStep = 'ingredients';
55
+ botResponse = 'Great choice! 🍽️ Please select your ingredients from the list below.';
56
+ } else if (currentStep === 'ingredients' && selectedIngredients.length > 0) {
57
+ // No automatic transition; wait for "Send" button
58
+ return;
59
+ } else if (currentStep === 'recipes') {
60
+ // Handle back button logic if needed
61
+ if (lastMessage.includes('back')) {
62
+ if (conversation.length > 2) {
63
+ conversation.pop(); // Remove 'back'
64
+ const prevMessage = conversation[conversation.length - 1].message.toLowerCase();
65
+ if (prevMessage.includes('vegetarian') || prevMessage.includes('non-vegetarian')) {
66
+ currentStep = 'dietary';
67
+ selectedIngredients = [];
68
+ chatMessages.innerHTML = '';
69
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
70
+ handleResponse(prevMessage);
71
+ return;
72
+ }
73
+ }
74
+ }
75
  }
76
 
77
  addMessage('bot', botResponse);
78
  if (options.length > 0) {
79
  displayOptions(options);
80
+ } else if (currentStep === 'ingredients') {
81
+ fetchIngredients(lastMessage);
82
+ addSendButton();
83
+ } else if (currentStep === 'recipes') {
84
+ displayBackButton();
85
  }
86
  }
87
 
88
  function fetchIngredients(dietaryPreference) {
89
  fetch('/get_ingredients', {
90
  method: 'POST',
91
+ headers: {'Content-Type': 'application/json'},
 
 
92
  body: JSON.stringify({ dietary_preference: dietaryPreference })
93
  })
94
  .then(response => response.json())
 
114
  return;
115
  }
116
 
 
117
  let ingredientsList = document.querySelector('.ingredients-list');
118
  if (!ingredientsList) {
119
  ingredientsList = document.createElement('div');
 
127
  const item = document.createElement('div');
128
  item.className = 'ingredient-item';
129
  const img = document.createElement('img');
130
+ img.src = ingredient.image_url || 'https://via.placeholder.com/80';
131
  img.alt = ingredient.name;
132
  const name = document.createElement('div');
133
  name.textContent = ingredient.name;
 
156
  return;
157
  }
158
 
 
159
  let selectedArea = document.querySelector('.selected-ingredients');
160
  if (!selectedArea) {
161
  selectedArea = document.createElement('div');
 
172
  });
173
  }
174
 
175
+ function addSendButton() {
176
  const chatMessages = document.getElementById('chatMessages');
177
+ if (!chatMessages) return;
178
+
179
+ let sendButton = document.querySelector('.send-button');
180
+ if (!sendButton) {
181
+ sendButton = document.createElement('button');
182
+ sendButton.textContent = 'Send';
183
+ sendButton.className = 'option-button send-button';
184
+ sendButton.onclick = () => {
185
+ if (selectedIngredients.length > 0) {
186
+ generateRecipes();
187
+ } else {
188
+ addMessage('bot', 'Please select at least one ingredient first!');
189
+ }
190
+ };
191
+ chatMessages.appendChild(sendButton);
192
+ }
193
+ }
194
+
195
+ function generateRecipes() {
196
+ const ingredients = selectedIngredients.map(item => item.name);
197
+ fetch('/generate_recipes', {
198
+ method: 'POST',
199
+ headers: {'Content-Type': 'application/json'},
200
+ body: JSON.stringify({ ingredients: ingredients })
201
+ })
202
+ .then(response => response.json())
203
+ .then(data => {
204
+ if (data.error) {
205
+ addMessage('bot', `Error generating recipes: ${data.error}`);
206
+ } else {
207
+ currentStep = 'recipes';
208
+ addMessage('bot', 'Here are 5 South Indian recipes based on your ingredients:');
209
+ displayRecipes(data.recipes);
210
+ displayBackButton();
211
+ }
212
+ })
213
+ .catch(error => {
214
+ addMessage('bot', `Error: Unable to generate recipes. ${error.message}`);
215
+ });
216
+ }
217
+
218
+ function displayRecipes(recipes) {
219
+ const chatMessages = document.getElementById('chatMessages');
220
+ if (!chatMessages) return;
221
+
222
+ recipes.forEach(recipe => {
223
+ const item = document.createElement('div');
224
+ item.className = 'recipe-item';
225
+ const img = document.createElement('img');
226
+ img.src = recipe.image_url;
227
+ img.alt = recipe.name;
228
+ img.style.width = '100px';
229
+ img.style.height = '100px';
230
+ img.style.objectFit = 'cover';
231
+ const name = document.createElement('div');
232
+ name.textContent = recipe.name;
233
+ name.style.margin = '5px 0';
234
+ const desc = document.createElement('div');
235
+ desc.textContent = recipe.description;
236
+ desc.style.fontSize = '12px';
237
+ const button = document.createElement('button');
238
+ button.textContent = 'Add to Cart';
239
+ button.className = 'option-button';
240
+ button.onclick = () => showPopup(recipe);
241
+ item.appendChild(img);
242
+ item.appendChild(name);
243
+ item.appendChild(desc);
244
+ item.appendChild(button);
245
+ chatMessages.appendChild(item);
246
+ });
247
+ }
248
+
249
+ function showPopup(recipe) {
250
+ const popup = document.getElementById('popup');
251
+ const popupContent = document.getElementById('popupContent');
252
+ if (!popup || !popupContent) return;
253
+
254
+ popupContent.innerHTML = `
255
+ <h3>${recipe.name}</h3>
256
+ <img src="${recipe.image_url}" alt="${recipe.name}" style="width: 100%; max-width: 250px;">
257
+ <p><strong>Description:</strong> ${recipe.description}</p>
258
+ <p><strong>Preparation Steps:</strong> ${recipe.details.preparation}</p>
259
+ <p><strong>Key Ingredients:</strong> ${recipe.details.key_ingredients.join(', ')}</p>
260
+ `;
261
+ popup.style.display = 'block';
262
+ }
263
+
264
+ function closePopup() {
265
+ const popup = document.getElementById('popup');
266
+ if (popup) popup.style.display = 'none';
267
+ }
268
+
269
+ function displayBackButton() {
270
+ const chatMessages = document.getElementById('chatMessages');
271
+ if (!chatMessages) return;
272
+
273
+ let backButton = document.querySelector('.back-button');
274
+ if (!backButton) {
275
+ backButton = document.createElement('button');
276
+ backButton.textContent = 'Back';
277
+ backButton.className = 'option-button back-button';
278
+ backButton.onclick = () => {
279
+ addMessage('user', 'Back');
280
+ conversation.push({ role: 'user', message: 'Back' });
281
+ handleResponse('Back');
282
+ };
283
+ chatMessages.appendChild(backButton);
284
  }
285
+ }
286
+
287
+ function displayOptions(options) {
288
+ const chatMessages = document.getElementById('chatMessages');
289
+ if (!chatMessages) return;
290
  options.forEach(opt => {
291
  const button = document.createElement('button');
292
  button.textContent = opt.text;
 
294
  button.onclick = () => {
295
  addMessage('user', opt.text);
296
  conversation.push({ role: 'user', message: opt.text });
297
+ chatMessages.innerHTML = '';
298
  conversation.forEach(msg => addMessage(msg.role, msg.message));
299
  setTimeout(() => handleResponse(opt.text), 500);
300
  };
301
  chatMessages.appendChild(button);
302
  });
303
  chatMessages.appendChild(document.createElement('br'));
 
 
 
 
 
 
 
 
 
 
 
304
  }
305
 
306
  // Add event listener for Enter key