lokesh341 commited on
Commit
e5f575b
·
verified ·
1 Parent(s): 63fc80c

Update static/script.js

Browse files
Files changed (1) hide show
  1. static/script.js +17 -25
static/script.js CHANGED
@@ -31,7 +31,7 @@ function sendMessage() {
31
  selectionBoxVisible = true;
32
  handleResponse(message);
33
  } else {
34
- addMessage('bot', 'Please type a dish name! 😄');
35
  }
36
  userInput.value = '';
37
  updateSelectionBox();
@@ -42,7 +42,7 @@ function handleResponse(userInput) {
42
  let botResponse = '';
43
 
44
  if (conversation.length === 2) {
45
- botResponse = `Hi ${userInput}! 🍳 Search for a dish like "chicken" or "paneer"!`;
46
  displayOptions([
47
  { text: 'Vegetarian', class: 'green' },
48
  { text: 'Non-Vegetarian', class: 'red' },
@@ -50,12 +50,13 @@ function handleResponse(userInput) {
50
  ]);
51
  addMessage('bot', botResponse);
52
  } else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
53
- botResponse = `Selected ${lowerInput}. Search for a dish!`;
54
  addMessage('bot', botResponse);
 
55
  } else {
56
  botResponse = `Looking for "${userInput}"...`;
57
  addMessage('bot', botResponse);
58
- fetchMenuItems(userInput);
59
  }
60
  }
61
 
@@ -74,31 +75,19 @@ function updateSelectionBox() {
74
  const vegButton = document.createElement('button');
75
  vegButton.textContent = 'Veg';
76
  vegButton.className = 'dietary-button green';
77
- vegButton.onclick = () => {
78
- addMessage('user', 'Vegetarian');
79
- conversation.push({ role: 'user', message: 'Vegetarian' });
80
- handleResponse('vegetarian');
81
- };
82
  selectionBox.appendChild(vegButton);
83
 
84
  const nonVegButton = document.createElement('button');
85
  nonVegButton.textContent = 'Non-Veg';
86
  nonVegButton.className = 'dietary-button red';
87
- nonVegButton.onclick = () => {
88
- addMessage('user', 'Non-Vegetarian');
89
- conversation.push({ role: 'user', message: 'Non-Vegetarian' });
90
- handleResponse('non-vegetarian');
91
- };
92
  selectionBox.appendChild(nonVegButton);
93
 
94
  const bothButton = document.createElement('button');
95
  bothButton.textContent = 'Both';
96
  bothButton.className = 'dietary-button gray';
97
- bothButton.onclick = () => {
98
- addMessage('user', 'Both');
99
- conversation.push({ role: 'user', message: 'Both' });
100
- handleResponse('both');
101
- };
102
  selectionBox.appendChild(bothButton);
103
 
104
  const label = document.createElement('span');
@@ -172,11 +161,14 @@ function updateSelectionBox() {
172
  console.log('Updated selection box:', selectedItems.map(item => ({ name: item.name, category: item.category })));
173
  }
174
 
175
- function fetchMenuItems(searchTerm) {
 
 
 
176
  fetch('/get_menu_items', {
177
  method: 'POST',
178
  headers: { 'Content-Type': 'application/json' },
179
- body: JSON.stringify({ search_term: searchTerm })
180
  })
181
  .then(response => response.json())
182
  .then(data => {
@@ -186,13 +178,13 @@ function fetchMenuItems(searchTerm) {
186
  addMessage('bot', `--- Found ${data.menu_items.length} item${data.menu_items.length > 1 ? 's' : ''} ---`);
187
  displayItemsList(data.menu_items);
188
  } else {
189
- addMessage('bot', `No matches for "${searchTerm}". Try "paneer"!`);
190
  }
191
- console.log(`Fetched items for ${searchTerm}:`, data.menu_items);
192
  })
193
  .catch(error => {
194
  addMessage('bot', `Connection issue: ${error.message}. Retrying...`);
195
- setTimeout(() => fetchMenuItems(searchTerm), 2000);
196
  });
197
  }
198
 
@@ -424,7 +416,7 @@ function resetConversation() {
424
  conversation = [
425
  { role: 'bot', message: `Hello! I’m Chef Bot, your culinary assistant! What’s your name?` },
426
  { role: 'user', message: userName },
427
- { role: 'bot', message: `Hi ${userName}! 🍳 Search for a dish like "chicken" or "paneer"!` }
428
  ];
429
  selectedItems = [];
430
  selectionBoxVisible = true;
 
31
  selectionBoxVisible = true;
32
  handleResponse(message);
33
  } else {
34
+ addMessage('bot', 'Please type a dish or pick a preference! 😄');
35
  }
36
  userInput.value = '';
37
  updateSelectionBox();
 
42
  let botResponse = '';
43
 
44
  if (conversation.length === 2) {
45
+ botResponse = `Hi ${userInput}! 🍳 Search for a dish or choose a preference below!`;
46
  displayOptions([
47
  { text: 'Vegetarian', class: 'green' },
48
  { text: 'Non-Vegetarian', class: 'red' },
 
50
  ]);
51
  addMessage('bot', botResponse);
52
  } else if (lowerInput === 'vegetarian' || lowerInput === 'non-vegetarian' || lowerInput === 'both') {
53
+ botResponse = `Fetching ${lowerInput} dishes...`;
54
  addMessage('bot', botResponse);
55
+ fetchMenuItems(lowerInput);
56
  } else {
57
  botResponse = `Looking for "${userInput}"...`;
58
  addMessage('bot', botResponse);
59
+ fetchMenuItems(null, userInput);
60
  }
61
  }
62
 
 
75
  const vegButton = document.createElement('button');
76
  vegButton.textContent = 'Veg';
77
  vegButton.className = 'dietary-button green';
78
+ vegButton.onclick = () => fetchMenuItems('vegetarian');
 
 
 
 
79
  selectionBox.appendChild(vegButton);
80
 
81
  const nonVegButton = document.createElement('button');
82
  nonVegButton.textContent = 'Non-Veg';
83
  nonVegButton.className = 'dietary-button red';
84
+ nonVegButton.onclick = () => fetchMenuItems('non-vegetarian');
 
 
 
 
85
  selectionBox.appendChild(nonVegButton);
86
 
87
  const bothButton = document.createElement('button');
88
  bothButton.textContent = 'Both';
89
  bothButton.className = 'dietary-button gray';
90
+ bothButton.onclick = () => fetchMenuItems('both');
 
 
 
 
91
  selectionBox.appendChild(bothButton);
92
 
93
  const label = document.createElement('span');
 
161
  console.log('Updated selection box:', selectedItems.map(item => ({ name: item.name, category: item.category })));
162
  }
163
 
164
+ function fetchMenuItems(dietaryPreference, searchTerm = '') {
165
+ const payload = {};
166
+ if (dietaryPreference) payload.dietary_preference = dietaryPreference;
167
+ if (searchTerm) payload.search_term = searchTerm;
168
  fetch('/get_menu_items', {
169
  method: 'POST',
170
  headers: { 'Content-Type': 'application/json' },
171
+ body: JSON.stringify(payload)
172
  })
173
  .then(response => response.json())
174
  .then(data => {
 
178
  addMessage('bot', `--- Found ${data.menu_items.length} item${data.menu_items.length > 1 ? 's' : ''} ---`);
179
  displayItemsList(data.menu_items);
180
  } else {
181
+ addMessage('bot', `No matches for "${searchTerm || dietaryPreference}". Try "paneer"!`);
182
  }
183
+ console.log(`Fetched items for ${searchTerm || dietaryPreference}:`, data.menu_items);
184
  })
185
  .catch(error => {
186
  addMessage('bot', `Connection issue: ${error.message}. Retrying...`);
187
+ setTimeout(() => fetchMenuItems(dietaryPreference, searchTerm), 2000);
188
  });
189
  }
190
 
 
416
  conversation = [
417
  { role: 'bot', message: `Hello! I’m Chef Bot, your culinary assistant! What’s your name?` },
418
  { role: 'user', message: userName },
419
+ { role: 'bot', message: `Hi ${userName}! 🍳 Search for a dish or choose a preference below!` }
420
  ];
421
  selectedItems = [];
422
  selectionBoxVisible = true;