Yaswanth56 commited on
Commit
0be21b6
·
verified ·
1 Parent(s): 3d1b9ce

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +41 -30
static/script.js CHANGED
@@ -54,10 +54,9 @@ function handleResponse(userInput) {
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'
@@ -69,6 +68,13 @@ function handleResponse(userInput) {
69
  conversation.forEach(msg => addMessage(msg.role, msg.message));
70
  handleResponse(prevMessage);
71
  return;
 
 
 
 
 
 
 
72
  }
73
  }
74
  }
@@ -79,9 +85,8 @@ function handleResponse(userInput) {
79
  displayOptions(options);
80
  } else if (currentStep === 'ingredients') {
81
  fetchIngredients(lastMessage);
82
- addSendButton();
83
  } else if (currentStep === 'recipes') {
84
- displayBackButton();
85
  }
86
  }
87
 
@@ -99,7 +104,6 @@ function fetchIngredients(dietaryPreference) {
99
  const ingredients = data.ingredients || [];
100
  addMessage('bot', 'Great choice! These are available ingredients:');
101
  displayIngredientsList(ingredients);
102
- displaySelectedIngredients();
103
  }
104
  })
105
  .catch(error => {
@@ -147,6 +151,8 @@ function displayIngredientsList(ingredients) {
147
  item.appendChild(button);
148
  ingredientsList.appendChild(item);
149
  });
 
 
150
  }
151
 
152
  function displaySelectedIngredients() {
@@ -165,11 +171,15 @@ function displaySelectedIngredients() {
165
  selectedArea.innerHTML = '';
166
  }
167
 
168
- selectedIngredients.forEach(ingredient => {
169
- const div = document.createElement('div');
170
- div.textContent = ingredient.name;
171
- selectedArea.appendChild(div);
172
- });
 
 
 
 
173
  }
174
 
175
  function addSendButton() {
@@ -207,7 +217,6 @@ function generateRecipes() {
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 => {
@@ -234,14 +243,23 @@ function displayRecipes(recipes) {
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
  }
@@ -266,21 +284,14 @@ function closePopup() {
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
 
 
54
  currentStep = 'ingredients';
55
  botResponse = 'Great choice! 🍽️ Please select your ingredients from the list below.';
56
  } else if (currentStep === 'ingredients' && selectedIngredients.length > 0) {
57
+ // Wait for "Send" button
58
  return;
59
  } else if (currentStep === 'recipes') {
 
60
  if (lastMessage.includes('back')) {
61
  if (conversation.length > 2) {
62
  conversation.pop(); // Remove 'back'
 
68
  conversation.forEach(msg => addMessage(msg.role, msg.message));
69
  handleResponse(prevMessage);
70
  return;
71
+ } else if (prevMessage.includes('send')) {
72
+ currentStep = 'ingredients';
73
+ chatMessages.innerHTML = '';
74
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
75
+ displaySelectedIngredients();
76
+ addSendButton();
77
+ return;
78
  }
79
  }
80
  }
 
85
  displayOptions(options);
86
  } else if (currentStep === 'ingredients') {
87
  fetchIngredients(lastMessage);
 
88
  } else if (currentStep === 'recipes') {
89
+ displayBackButton(); // Ensure back button is available
90
  }
91
  }
92
 
 
104
  const ingredients = data.ingredients || [];
105
  addMessage('bot', 'Great choice! These are available ingredients:');
106
  displayIngredientsList(ingredients);
 
107
  }
108
  })
109
  .catch(error => {
 
151
  item.appendChild(button);
152
  ingredientsList.appendChild(item);
153
  });
154
+ displaySelectedIngredients(); // Show selected ingredients below
155
+ addSendButton(); // Add Send button below ingredients
156
  }
157
 
158
  function displaySelectedIngredients() {
 
171
  selectedArea.innerHTML = '';
172
  }
173
 
174
+ if (selectedIngredients.length > 0) {
175
+ selectedIngredients.forEach(ingredient => {
176
+ const div = document.createElement('div');
177
+ div.textContent = ingredient.name;
178
+ selectedArea.appendChild(div);
179
+ });
180
+ } else {
181
+ selectedArea.innerHTML = '<div>No ingredients selected yet.</div>';
182
+ }
183
  }
184
 
185
  function addSendButton() {
 
217
  currentStep = 'recipes';
218
  addMessage('bot', 'Here are 5 South Indian recipes based on your ingredients:');
219
  displayRecipes(data.recipes);
 
220
  }
221
  })
222
  .catch(error => {
 
243
  const desc = document.createElement('div');
244
  desc.textContent = recipe.description;
245
  desc.style.fontSize = '12px';
246
+ const addButton = document.createElement('button');
247
+ addButton.textContent = 'Add to Cart';
248
+ addButton.className = 'option-button';
249
+ addButton.onclick = () => showPopup(recipe);
250
+ const backButton = document.createElement('button');
251
+ backButton.textContent = 'Back';
252
+ backButton.className = 'option-button';
253
+ backButton.onclick = () => {
254
+ addMessage('user', 'Back');
255
+ conversation.push({ role: 'user', message: 'Back' });
256
+ handleResponse('Back');
257
+ };
258
  item.appendChild(img);
259
  item.appendChild(name);
260
  item.appendChild(desc);
261
+ item.appendChild(addButton);
262
+ item.appendChild(backButton);
263
  chatMessages.appendChild(item);
264
  });
265
  }
 
284
  if (popup) popup.style.display = 'none';
285
  }
286
 
287
+ function resetChat() {
288
+ conversation = [{ role: 'bot', message: 'Hi there! I\'m Chef Bot! May I know your name?' }];
289
+ selectedIngredients = [];
290
+ currentStep = 'name';
291
  const chatMessages = document.getElementById('chatMessages');
292
+ if (chatMessages) {
293
+ chatMessages.innerHTML = '';
294
+ addMessage('bot', conversation[0].message);
 
 
 
 
 
 
 
 
 
 
295
  }
296
  }
297