nagasurendra commited on
Commit
3b2bc6b
·
verified ·
1 Parent(s): dce7cd4

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +4 -265
static/script.js CHANGED
@@ -93,272 +93,11 @@ function addMessage(role, message) {
93
  }
94
 
95
 
96
- function fetchIngredients(foodPreference) {
97
- console.log(`Fetching ingredients for dietary preference: ${foodPreference}`);
98
- fetch('/get_ingredients', {
99
- method: 'POST',
100
- headers: {
101
- 'Content-Type': 'application/json',
102
- },
103
- body: JSON.stringify({ dietary_preference: foodPreference })
104
- })
105
- .then(response => {
106
- console.log(`Response status: ${response.status}`);
107
- if (!response.ok) {
108
- throw new Error(`HTTP error! Status: ${response.status}`);
109
- }
110
- return response.json();
111
- })
112
- .then(data => {
113
- console.log('Response data:', data);
114
- if (data.error) {
115
- addMessage('bot', `Error fetching ingredients: ${data.error}`);
116
- } else if (!data.ingredients || data.ingredients.length === 0) {
117
- addMessage('bot', 'No ingredients found for this selection. Please try another option or check backend data.');
118
- } else {
119
- const ingredients = data.ingredients;
120
- addMessage('bot', 'Please select ingredients:');
121
- displayIngredientsList(ingredients);
122
- displaySelectedIngredients();
123
- }
124
- })
125
- .catch(error => {
126
- console.error('Fetch error:', error);
127
- addMessage('bot', `Failed to load ingredients: ${error.message}. Check console for details.`);
128
- });
129
- }
130
-
131
- function fetchMenuItems(category) {
132
- fetch('/get_menu_items', {
133
- method: 'POST',
134
- headers: {
135
- 'Content-Type': 'application/json',
136
- },
137
- body: JSON.stringify({ category: category })
138
- })
139
- .then(response => response.json())
140
- .then(data => {
141
- if (data.error) {
142
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
143
- } else {
144
- const menuItems = data.menu_items || [];
145
- addMessage('bot', 'Here are some dishes based on your selection:');
146
- displayMenuItems(menuItems);
147
- }
148
- })
149
- .catch(error => {
150
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
151
- });
152
- }
153
-
154
- function displayIngredientsList(ingredients) {
155
- const chatMessages = document.getElementById('chatMessages');
156
- if (!chatMessages) {
157
- console.error('Chat messages container not found for ingredients!');
158
- return;
159
- }
160
-
161
- let ingredientsList = document.querySelector('.ingredients-list');
162
- if (!ingredientsList) {
163
- ingredientsList = document.createElement('div');
164
- ingredientsList.className = 'ingredients-list';
165
- chatMessages.appendChild(ingredientsList);
166
- } else {
167
- ingredientsList.innerHTML = '';
168
- }
169
-
170
- ingredients.forEach(ingredient => {
171
- const item = document.createElement('div');
172
- item.className = 'ingredient-item';
173
- const img = document.createElement('img');
174
- img.src = ingredient.image_url || 'https://via.placeholder.com/80';
175
- img.alt = ingredient.name;
176
- const name = document.createElement('div');
177
- name.textContent = ingredient.name;
178
- name.style.textAlign = 'center';
179
- name.style.marginTop = '5px';
180
- name.style.fontSize = '12px';
181
- const button = document.createElement('button');
182
- button.textContent = 'Add';
183
- button.onclick = () => {
184
- if (!selectedIngredients.some(item => item.name === ingredient.name)) {
185
- selectedIngredients.push(ingredient);
186
- displaySelectedIngredients();
187
- }
188
- };
189
- item.appendChild(img);
190
- item.appendChild(name);
191
- item.appendChild(button);
192
- ingredientsList.appendChild(item);
193
- });
194
- }
195
-
196
- function displayMenuItems(menuItems) {
197
- const chatMessages = document.getElementById('chatMessages');
198
- if (!chatMessages) {
199
- console.error('Chat messages container not found for menu items!');
200
- return;
201
- }
202
-
203
- let menuItemsList = document.querySelector('.menu-items-list');
204
- if (!menuItemsList) {
205
- menuItemsList = document.createElement('div');
206
- menuItemsList.className = 'menu-items-list';
207
- chatMessages.appendChild(menuItemsList);
208
- } else {
209
- menuItemsList.innerHTML = '';
210
- }
211
-
212
- menuItems.forEach(item => {
213
- const menuItem = document.createElement('div');
214
- menuItem.className = 'menu-item';
215
- const img = document.createElement('img');
216
- img.src = item.image_url || 'https://via.placeholder.com/80';
217
- img.alt = item.name;
218
- const name = document.createElement('div');
219
- name.textContent = item.name;
220
- name.style.textAlign = 'center';
221
- name.style.marginTop = '5px';
222
- name.style.fontSize = '12px';
223
- const button = document.createElement('button');
224
- button.textContent = 'Add to Cart';
225
- button.onclick = () => {
226
- selectedMenuItem = item;
227
- addMessage('bot', `World-class selection! Would you like to customize your dish further?`);
228
- const options = [
229
- { text: 'Yes', class: 'green' },
230
- { text: 'No', class: 'red' }
231
- ];
232
- displayOptions(options);
233
- };
234
- menuItem.appendChild(img);
235
- menuItem.appendChild(name);
236
- menuItem.appendChild(button);
237
- menuItemsList.appendChild(menuItem);
238
- });
239
- }
240
-
241
- function displaySelectedIngredients() {
242
- const chatMessages = document.getElementById('chatMessages');
243
- if (!chatMessages) {
244
- console.error('Chat messages container not found for selected ingredients!');
245
- return;
246
  }
247
-
248
- let selectedArea = document.querySelector('.selected-ingredients');
249
- if (!selectedArea) {
250
- selectedArea = document.createElement('div');
251
- selectedArea.className = 'selected-ingredients';
252
- chatMessages.appendChild(selectedArea);
253
- } else {
254
- selectedArea.innerHTML = '';
255
- }
256
-
257
- selectedIngredients.forEach(ingredient => {
258
- const div = document.createElement('div');
259
- div.textContent = ingredient.name;
260
- selectedArea.appendChild(div);
261
- });
262
-
263
- if (selectedIngredients.length > 0) {
264
- let submitButton = document.querySelector('.submit-button');
265
- if (!submitButton) {
266
- submitButton = document.createElement('button');
267
- submitButton.className = 'submit-button';
268
- submitButton.textContent = 'Submit Ingredients';
269
- submitButton.onclick = submitIngredients;
270
- chatMessages.appendChild(submitButton);
271
- }
272
- }
273
- }
274
-
275
- function submitIngredients() {
276
- fetch('/submit_ingredients', {
277
- method: 'POST',
278
- headers: {
279
- 'Content-Type': 'application/json',
280
- },
281
- body: JSON.stringify({ ingredients: selectedIngredients })
282
- })
283
- .then(response => response.json())
284
- .then(data => {
285
- if (data.success) {
286
- const ingredientNames = selectedIngredients.map(ingredient => ingredient.name.toLowerCase()).join(' ');
287
- fetchMenuItems(ingredientNames);
288
- } else {
289
- addMessage('bot', 'There was an issue submitting your ingredients. Please try again.');
290
- }
291
- })
292
- .catch(error => {
293
- addMessage('bot', `Error: ${error.message}`);
294
- });
295
- }
296
-
297
- function fetchMenuItems(ingredientNames) {
298
- fetch('/get_menu_items', {
299
- method: 'POST',
300
- headers: {
301
- 'Content-Type': 'application/json',
302
- },
303
- body: JSON.stringify({ ingredient_names: ingredientNames })
304
- })
305
- .then(response => response.json())
306
- .then(data => {
307
- if (data.error) {
308
- addMessage('bot', `Sorry, there was an error fetching menu items: ${data.error}`);
309
- } else {
310
- const menuItems = data.menu_items || [];
311
- addMessage('bot', 'Here are some dishes based on your selected ingredients:');
312
- displayMenuItems(menuItems);
313
- }
314
- })
315
- .catch(error => {
316
- addMessage('bot', `Error: Unable to connect to the menu database. ${error.message}`);
317
- });
318
- }
319
-
320
- function displayCustomizationInput() {
321
- const chatMessages = document.getElementById('chatMessages');
322
- if (!chatMessages) {
323
- console.error('Chat messages container not found for customization input!');
324
- return;
325
- }
326
-
327
- let customizationArea = document.querySelector('.customization-input');
328
- if (!customizationArea) {
329
- customizationArea = document.createElement('div');
330
- customizationArea.className = 'customization-input';
331
- const textarea = document.createElement('textarea');
332
- textarea.placeholder = 'Enter any special instructions...';
333
- const submitButton = document.createElement('button');
334
- submitButton.textContent = 'Submit Instructions';
335
- submitButton.onclick = () => {
336
- const instructions = textarea.value.trim();
337
- if (instructions) {
338
- addMessage('user', instructions);
339
- conversation.push({ role: 'user', message: instructions });
340
- }
341
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
342
- addMessage('bot', 'Item added to cart! Would you like to order more?');
343
- const options = [
344
- { text: 'Yes', class: 'green' },
345
- { text: 'No', class: 'red' }
346
- ];
347
- displayOptions(options);
348
- selectedMenuItem = null;
349
- selectedIngredients = [];
350
- };
351
- customizationArea.appendChild(textarea);
352
- customizationArea.appendChild(submitButton);
353
- chatMessages.appendChild(customizationArea);
354
- }
355
- }
356
-
357
- function addToCart(item) {
358
- cart.push(item);
359
- console.log('Cart:', cart);
360
- }
361
-
362
 
363
 
364
  console.log('Script loaded successfully');
 
93
  }
94
 
95
 
96
+ document.getElementById('userInput').addEventListener('keypress', function(e) {
97
+ if (e.key === 'Enter') {
98
+ sendMessage();
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99
  }
100
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
101
 
102
 
103
  console.log('Script loaded successfully');