nagasurendra commited on
Commit
d5aac24
·
verified ·
1 Parent(s): 578ff22

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +187 -187
static/script.js CHANGED
@@ -35,6 +35,193 @@ function sendMessage() {
35
  console.warn('Empty message!');
36
  }
37
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
 
40
 
@@ -252,193 +439,6 @@ function displayCustomizationInput() {
252
  }
253
  }
254
 
255
- function handleResponse(userInput) {
256
- const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
257
- let botResponse = '';
258
- let options = [];
259
-
260
- if (conversation.length === 2) { // After name input
261
- botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
262
- options = [
263
- { text: 'Vegetarian', class: 'green' },
264
- { text: 'Non-Vegetarian', class: 'red' }
265
- ];
266
- } else if (lastMessage.includes('non-vegetarian')) {
267
- conversation.push({ role: 'user', message: 'Non-Vegetarian' });
268
- botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
269
- fetchIngredients('non-vegetarian');
270
- return;
271
- } else if (lastMessage.includes('vegetarian')) {
272
- conversation.push({ role: 'user', message: 'Vegetarian' });
273
- botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
274
- fetchIngredients('vegetarian');
275
- return;
276
- } else if (lastMessage.includes('yes') && selectedMenuItem) {
277
- // New step after "Yes" is clicked: Fetch ingredients based on 'both'
278
- fetchNewStepIngredients();
279
- return;
280
- } else if (lastMessage.includes('no') && selectedMenuItem) {
281
- addToCart(selectedMenuItem);
282
- botResponse = 'Item added to cart! Would you like to order more?';
283
- options = [
284
- { text: 'Yes', class: 'green' },
285
- { text: 'No', class: 'red' }
286
- ];
287
- selectedMenuItem = null;
288
- }
289
-
290
- addMessage('bot', botResponse);
291
- if (options.length > 0) {
292
- displayOptions(options);
293
- }
294
- }
295
-
296
- // New function to fetch ingredients based on 'both' condition after clicking 'Yes'
297
- function fetchNewStepIngredients() {
298
- fetch('/get_ingredients', {
299
- method: 'POST',
300
- headers: {
301
- 'Content-Type': 'application/json',
302
- },
303
- body: JSON.stringify({ dietary_preference: 'both' })
304
- })
305
- .then(response => response.json())
306
- .then(data => {
307
- if (data.error) {
308
- addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
309
- } else {
310
- const ingredients = data.ingredients || [];
311
- addMessage('bot', 'Please select ingredients to add to your dish:');
312
- displayIngredientsList(ingredients);
313
- displaySelectedIngredients();
314
- // Show the instruction box
315
- displayCustomizationInput();
316
- }
317
- })
318
- .catch(error => {
319
- addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
320
- });
321
- }
322
-
323
- // New step to display ingredients from the 'both' condition
324
- function displayIngredientsList(ingredients) {
325
- const chatMessages = document.getElementById('chatMessages');
326
- if (!chatMessages) {
327
- console.error('Chat messages container not found for ingredients!');
328
- return;
329
- }
330
-
331
- let ingredientsList = document.querySelector('.ingredients-list');
332
- if (!ingredientsList) {
333
- ingredientsList = document.createElement('div');
334
- ingredientsList.className = 'ingredients-list';
335
- chatMessages.appendChild(ingredientsList);
336
- } else {
337
- ingredientsList.innerHTML = '';
338
- }
339
-
340
- ingredients.forEach(ingredient => {
341
- const item = document.createElement('div');
342
- item.className = 'ingredient-item';
343
- const img = document.createElement('img');
344
- img.src = ingredient.image_url || 'https://via.placeholder.com/80';
345
- img.alt = ingredient.name;
346
- const name = document.createElement('div');
347
- name.textContent = ingredient.name;
348
- name.style.textAlign = 'center';
349
- name.style.marginTop = '5px';
350
- name.style.fontSize = '12px';
351
- const button = document.createElement('button');
352
- button.textContent = 'Add';
353
- button.onclick = () => {
354
- if (!selectedIngredients.some(item => item.name === ingredient.name)) {
355
- selectedIngredients.push(ingredient);
356
- displaySelectedIngredients();
357
- }
358
- };
359
- item.appendChild(img);
360
- item.appendChild(name);
361
- item.appendChild(button);
362
- ingredientsList.appendChild(item);
363
- });
364
- }
365
-
366
- // Show the instruction box for entering custom instructions
367
- function displayCustomizationInput() {
368
- const chatMessages = document.getElementById('chatMessages');
369
- if (!chatMessages) {
370
- console.error('Chat messages container not found for customization input!');
371
- return;
372
- }
373
-
374
- let customizationArea = document.querySelector('.customization-input');
375
- if (!customizationArea) {
376
- customizationArea = document.createElement('div');
377
- customizationArea.className = 'customization-input';
378
- const textarea = document.createElement('textarea');
379
- textarea.placeholder = 'Enter any special instructions...';
380
- const submitButton = document.createElement('button');
381
- submitButton.textContent = 'Submit Instructions';
382
- submitButton.onclick = () => {
383
- const instructions = textarea.value.trim();
384
- if (instructions) {
385
- addMessage('user', instructions);
386
- conversation.push({ role: 'user', message: instructions });
387
- }
388
- addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
389
- addMessage('bot', 'Item added to cart! Would you like to order more?');
390
- const options = [
391
- { text: 'Yes', class: 'green' },
392
- { text: 'No', class: 'red' }
393
- ];
394
- displayOptions(options);
395
- selectedMenuItem = null;
396
- selectedIngredients = [];
397
- };
398
- customizationArea.appendChild(textarea);
399
- customizationArea.appendChild(submitButton);
400
- chatMessages.appendChild(customizationArea);
401
- }
402
- }
403
-
404
- function addToCart(item) {
405
- cart.push(item);
406
- console.log('Cart:', cart);
407
- }
408
-
409
- function displayOptions(options) {
410
- const chatMessages = document.getElementById('chatMessages');
411
- if (!chatMessages) {
412
- console.error('Chat messages container not found for options!');
413
- return;
414
- }
415
- options.forEach(opt => {
416
- const button = document.createElement('button');
417
- button.textContent = opt.text;
418
- button.className = `option-button ${opt.class}`;
419
- button.onclick = () => {
420
- addMessage('user', opt.text);
421
- conversation.push({ role: 'user', message: opt.text });
422
- chatMessages.innerHTML = '';
423
- conversation.forEach(msg => addMessage(msg.role, msg.message));
424
- setTimeout(() => handleResponse(opt.text), 500);
425
- };
426
- chatMessages.appendChild(button);
427
- });
428
- chatMessages.appendChild(document.createElement('br'));
429
- const backButton = document.createElement('button');
430
- backButton.textContent = 'Go Back';
431
- backButton.className = 'option-button';
432
- backButton.onclick = () => {
433
- conversation.pop();
434
- selectedIngredients = [];
435
- selectedMenuItem = null;
436
- chatMessages.innerHTML = '';
437
- conversation.forEach(msg => addMessage(msg.role, msg.message));
438
- setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
439
- };
440
- chatMessages.appendChild(backButton);
441
- }
442
 
443
 
444
  document.getElementById('userInput').addEventListener('keypress', function(e) {
 
35
  console.warn('Empty message!');
36
  }
37
  }
38
+ function handleResponse(userInput) {
39
+ const lastMessage = conversation[conversation.length - 1].message.toLowerCase();
40
+ let botResponse = '';
41
+ let options = [];
42
+
43
+ if (conversation.length === 2) { // After name input
44
+ botResponse = `Nice to meet you, ${userInput}! 😊 Let's create your perfect meal! What type of food would you prefer?`;
45
+ options = [
46
+ { text: 'Vegetarian', class: 'green' },
47
+ { text: 'Non-Vegetarian', class: 'red' }
48
+ ];
49
+ } else if (lastMessage.includes('non-vegetarian')) {
50
+ conversation.push({ role: 'user', message: 'Non-Vegetarian' });
51
+ botResponse = 'Great choice! 🍽️ Please select a non-vegetarian option:';
52
+ fetchIngredients('non-vegetarian');
53
+ return;
54
+ } else if (lastMessage.includes('vegetarian')) {
55
+ conversation.push({ role: 'user', message: 'Vegetarian' });
56
+ botResponse = 'Great choice! 🍽️ Here are some vegetarian ingredients:';
57
+ fetchIngredients('vegetarian');
58
+ return;
59
+ } else if (lastMessage.includes('yes') && selectedMenuItem) {
60
+ // New step after "Yes" is clicked: Fetch ingredients based on 'both'
61
+ fetchNewStepIngredients();
62
+ return;
63
+ } else if (lastMessage.includes('no') && selectedMenuItem) {
64
+ addToCart(selectedMenuItem);
65
+ botResponse = 'Item added to cart! Would you like to order more?';
66
+ options = [
67
+ { text: 'Yes', class: 'green' },
68
+ { text: 'No', class: 'red' }
69
+ ];
70
+ selectedMenuItem = null;
71
+ }
72
+
73
+ addMessage('bot', botResponse);
74
+ if (options.length > 0) {
75
+ displayOptions(options);
76
+ }
77
+ }
78
+
79
+ // New function to fetch ingredients based on 'both' condition after clicking 'Yes'
80
+ function fetchNewStepIngredients() {
81
+ fetch('/get_ingredients', {
82
+ method: 'POST',
83
+ headers: {
84
+ 'Content-Type': 'application/json',
85
+ },
86
+ body: JSON.stringify({ dietary_preference: 'both' })
87
+ })
88
+ .then(response => response.json())
89
+ .then(data => {
90
+ if (data.error) {
91
+ addMessage('bot', `Sorry, there was an error fetching ingredients: ${data.error}`);
92
+ } else {
93
+ const ingredients = data.ingredients || [];
94
+ addMessage('bot', 'Please select ingredients to add to your dish:');
95
+ displayIngredientsList(ingredients);
96
+ displaySelectedIngredients();
97
+ // Show the instruction box
98
+ displayCustomizationInput();
99
+ }
100
+ })
101
+ .catch(error => {
102
+ addMessage('bot', `Error: Unable to connect to the ingredient database. ${error.message}`);
103
+ });
104
+ }
105
+
106
+ // New step to display ingredients from the 'both' condition
107
+ function displayIngredientsList(ingredients) {
108
+ const chatMessages = document.getElementById('chatMessages');
109
+ if (!chatMessages) {
110
+ console.error('Chat messages container not found for ingredients!');
111
+ return;
112
+ }
113
+
114
+ let ingredientsList = document.querySelector('.ingredients-list');
115
+ if (!ingredientsList) {
116
+ ingredientsList = document.createElement('div');
117
+ ingredientsList.className = 'ingredients-list';
118
+ chatMessages.appendChild(ingredientsList);
119
+ } else {
120
+ ingredientsList.innerHTML = '';
121
+ }
122
+
123
+ ingredients.forEach(ingredient => {
124
+ const item = document.createElement('div');
125
+ item.className = 'ingredient-item';
126
+ const img = document.createElement('img');
127
+ img.src = ingredient.image_url || 'https://via.placeholder.com/80';
128
+ img.alt = ingredient.name;
129
+ const name = document.createElement('div');
130
+ name.textContent = ingredient.name;
131
+ name.style.textAlign = 'center';
132
+ name.style.marginTop = '5px';
133
+ name.style.fontSize = '12px';
134
+ const button = document.createElement('button');
135
+ button.textContent = 'Add';
136
+ button.onclick = () => {
137
+ if (!selectedIngredients.some(item => item.name === ingredient.name)) {
138
+ selectedIngredients.push(ingredient);
139
+ displaySelectedIngredients();
140
+ }
141
+ };
142
+ item.appendChild(img);
143
+ item.appendChild(name);
144
+ item.appendChild(button);
145
+ ingredientsList.appendChild(item);
146
+ });
147
+ }
148
+
149
+ // Show the instruction box for entering custom instructions
150
+ function displayCustomizationInput() {
151
+ const chatMessages = document.getElementById('chatMessages');
152
+ if (!chatMessages) {
153
+ console.error('Chat messages container not found for customization input!');
154
+ return;
155
+ }
156
+
157
+ let customizationArea = document.querySelector('.customization-input');
158
+ if (!customizationArea) {
159
+ customizationArea = document.createElement('div');
160
+ customizationArea.className = 'customization-input';
161
+ const textarea = document.createElement('textarea');
162
+ textarea.placeholder = 'Enter any special instructions...';
163
+ const submitButton = document.createElement('button');
164
+ submitButton.textContent = 'Submit Instructions';
165
+ submitButton.onclick = () => {
166
+ const instructions = textarea.value.trim();
167
+ if (instructions) {
168
+ addMessage('user', instructions);
169
+ conversation.push({ role: 'user', message: instructions });
170
+ }
171
+ addToCart({ ...selectedMenuItem, instructions, ingredients: selectedIngredients });
172
+ addMessage('bot', 'Item added to cart! Would you like to order more?');
173
+ const options = [
174
+ { text: 'Yes', class: 'green' },
175
+ { text: 'No', class: 'red' }
176
+ ];
177
+ displayOptions(options);
178
+ selectedMenuItem = null;
179
+ selectedIngredients = [];
180
+ };
181
+ customizationArea.appendChild(textarea);
182
+ customizationArea.appendChild(submitButton);
183
+ chatMessages.appendChild(customizationArea);
184
+ }
185
+ }
186
+
187
+ function addToCart(item) {
188
+ cart.push(item);
189
+ console.log('Cart:', cart);
190
+ }
191
+
192
+ function displayOptions(options) {
193
+ const chatMessages = document.getElementById('chatMessages');
194
+ if (!chatMessages) {
195
+ console.error('Chat messages container not found for options!');
196
+ return;
197
+ }
198
+ options.forEach(opt => {
199
+ const button = document.createElement('button');
200
+ button.textContent = opt.text;
201
+ button.className = `option-button ${opt.class}`;
202
+ button.onclick = () => {
203
+ addMessage('user', opt.text);
204
+ conversation.push({ role: 'user', message: opt.text });
205
+ chatMessages.innerHTML = '';
206
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
207
+ setTimeout(() => handleResponse(opt.text), 500);
208
+ };
209
+ chatMessages.appendChild(button);
210
+ });
211
+ chatMessages.appendChild(document.createElement('br'));
212
+ const backButton = document.createElement('button');
213
+ backButton.textContent = 'Go Back';
214
+ backButton.className = 'option-button';
215
+ backButton.onclick = () => {
216
+ conversation.pop();
217
+ selectedIngredients = [];
218
+ selectedMenuItem = null;
219
+ chatMessages.innerHTML = '';
220
+ conversation.forEach(msg => addMessage(msg.role, msg.message));
221
+ setTimeout(() => handleResponse(conversation[conversation.length - 1].message), 500);
222
+ };
223
+ chatMessages.appendChild(backButton);
224
+ }
225
 
226
 
227
 
 
439
  }
440
  }
441
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
442
 
443
 
444
  document.getElementById('userInput').addEventListener('keypress', function(e) {