Update templates/menu_page.html
Browse files- templates/menu_page.html +73 -105
templates/menu_page.html
CHANGED
@@ -74,16 +74,14 @@
|
|
74 |
<div id="cart-summary" style="display:none;">
|
75 |
<h2>Your Cart:</h2>
|
76 |
<div id="cart-details"></div>
|
77 |
-
<button id="place-order-btn">Place Final Order</button>
|
78 |
</div>
|
79 |
</div>
|
80 |
|
81 |
<script>
|
82 |
const listenButton = document.getElementById("listen-btn");
|
83 |
-
const placeOrderButton = document.getElementById("place-order-btn");
|
84 |
const cartSummary = document.getElementById("cart-summary");
|
85 |
const cartDetails = document.getElementById("cart-details");
|
86 |
-
let isListening = false;
|
87 |
let cart = []; // Global cart variable to store items
|
88 |
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
89 |
recognition.lang = "en-US";
|
@@ -95,74 +93,41 @@
|
|
95 |
window.speechSynthesis.speak(msg);
|
96 |
}
|
97 |
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
processOrder();
|
113 |
-
} else if (command.includes("cart details")) {
|
114 |
showCartDetails();
|
115 |
-
} else {
|
116 |
-
speak("Sorry, I couldn't understand that. Please try again.");
|
117 |
}
|
118 |
-
};
|
119 |
-
|
120 |
-
recognition.onerror = (event) => {
|
121 |
-
console.error("Speech recognition error:", event.error);
|
122 |
-
speak("Sorry, I couldn't understand that. Please try again.");
|
123 |
-
};
|
124 |
-
|
125 |
-
// Function to process order
|
126 |
-
function processOrder() {
|
127 |
-
speak("Which item would you like to order?");
|
128 |
-
recognition.start();
|
129 |
-
recognition.onresult = (event) => {
|
130 |
-
const itemName = event.results[0][0].transcript.toLowerCase();
|
131 |
-
const item = findItem(itemName);
|
132 |
-
if (item) {
|
133 |
-
speak(`You selected ${item.name}. How many would you like?`);
|
134 |
-
recognition.start();
|
135 |
-
recognition.onresult = (event) => {
|
136 |
-
const quantity = parseInt(event.results[0][0].transcript);
|
137 |
-
if (quantity) {
|
138 |
-
addToCart(item, quantity);
|
139 |
-
speak(`${quantity} ${item.name} added to your cart.`);
|
140 |
-
askMoreItems();
|
141 |
-
} else {
|
142 |
-
speak("Please say a valid quantity.");
|
143 |
-
}
|
144 |
-
};
|
145 |
-
} else {
|
146 |
-
speak("Sorry, I couldn't find that item. Please try again.");
|
147 |
-
}
|
148 |
-
};
|
149 |
-
}
|
150 |
-
|
151 |
-
// Function to ask if the user wants to order more items
|
152 |
-
function askMoreItems() {
|
153 |
-
speak("Would you like to order more items?");
|
154 |
-
recognition.start();
|
155 |
-
recognition.onresult = (event) => {
|
156 |
-
const response = event.results[0][0].transcript.toLowerCase();
|
157 |
-
if (response.includes("yes")) {
|
158 |
-
processOrder();
|
159 |
-
} else {
|
160 |
-
showCartDetails();
|
161 |
-
}
|
162 |
-
};
|
163 |
}
|
164 |
|
165 |
-
// Function to
|
166 |
function showCartDetails() {
|
167 |
if (cart.length > 0) {
|
168 |
let cartHtml = "";
|
@@ -171,54 +136,57 @@
|
|
171 |
});
|
172 |
cartDetails.innerHTML = cartHtml;
|
173 |
cartSummary.style.display = "block";
|
174 |
-
speak(`Your cart contains ${cart.length} items.`);
|
175 |
} else {
|
176 |
speak("Your cart is empty.");
|
177 |
}
|
178 |
}
|
179 |
|
180 |
-
// Function to
|
181 |
-
function
|
182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
}
|
184 |
|
185 |
-
//
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
{ name: "Mutton Biryani", price: 300 }
|
191 |
-
];
|
192 |
-
return menu.find(item => item.name.toLowerCase() === itemName.toLowerCase());
|
193 |
-
}
|
194 |
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
200 |
-
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
menuContent += `
|
205 |
-
<div class="menu-item">
|
206 |
-
<div class="details">
|
207 |
-
<h3>${item.name}</h3>
|
208 |
-
<p class="price">Price: ₹${item.price}</p>
|
209 |
-
</div>
|
210 |
-
<button class="order-btn" onclick="addToCart(${item.name}, 1)">Order</button>
|
211 |
-
</div>
|
212 |
-
`;
|
213 |
-
});
|
214 |
-
document.getElementById('menu-items').innerHTML = menuContent;
|
215 |
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
-
|
221 |
-
|
|
|
|
|
222 |
}
|
223 |
</script>
|
224 |
|
|
|
74 |
<div id="cart-summary" style="display:none;">
|
75 |
<h2>Your Cart:</h2>
|
76 |
<div id="cart-details"></div>
|
77 |
+
<button id="place-order-btn" onclick="placeOrder()">Place Final Order</button>
|
78 |
</div>
|
79 |
</div>
|
80 |
|
81 |
<script>
|
82 |
const listenButton = document.getElementById("listen-btn");
|
|
|
83 |
const cartSummary = document.getElementById("cart-summary");
|
84 |
const cartDetails = document.getElementById("cart-details");
|
|
|
85 |
let cart = []; // Global cart variable to store items
|
86 |
const recognition = new (window.SpeechRecognition || window.webkitSpeechRecognition)();
|
87 |
recognition.lang = "en-US";
|
|
|
93 |
window.speechSynthesis.speak(msg);
|
94 |
}
|
95 |
|
96 |
+
// Function to fetch and populate the menu
|
97 |
+
function fetchMenu() {
|
98 |
+
fetch("/menu")
|
99 |
+
.then(response => response.json())
|
100 |
+
.then(data => {
|
101 |
+
let menuContent = "";
|
102 |
+
data.forEach(item => {
|
103 |
+
menuContent += `
|
104 |
+
<div class="menu-item">
|
105 |
+
<div class="details">
|
106 |
+
<h3>${item.name}</h3>
|
107 |
+
<p><strong>Ingredients:</strong> ${item.ingredients}</p>
|
108 |
+
<p><strong>Category:</strong> ${item.category}</p>
|
109 |
+
<p class="price">Price: ₹${item.price}</p>
|
110 |
+
</div>
|
111 |
+
<button class="order-btn" onclick="addToCart('${item.name}', ${item.price})">Order</button>
|
112 |
+
</div>
|
113 |
+
`;
|
114 |
+
});
|
115 |
+
document.getElementById('menu-items').innerHTML = menuContent;
|
116 |
+
})
|
117 |
+
.catch(error => console.error("Error fetching menu:", error));
|
118 |
+
}
|
119 |
|
120 |
+
// Function to add item to the cart
|
121 |
+
function addToCart(itemName, itemPrice) {
|
122 |
+
const quantity = prompt(`How many ${itemName} would you like?`);
|
123 |
+
if (quantity && !isNaN(quantity) && quantity > 0) {
|
124 |
+
cart.push({ name: itemName, price: itemPrice, quantity: parseInt(quantity) });
|
125 |
+
speak(`${quantity} x ${itemName} added to your cart.`);
|
|
|
|
|
126 |
showCartDetails();
|
|
|
|
|
127 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
128 |
}
|
129 |
|
130 |
+
// Function to display cart details
|
131 |
function showCartDetails() {
|
132 |
if (cart.length > 0) {
|
133 |
let cartHtml = "";
|
|
|
136 |
});
|
137 |
cartDetails.innerHTML = cartHtml;
|
138 |
cartSummary.style.display = "block";
|
|
|
139 |
} else {
|
140 |
speak("Your cart is empty.");
|
141 |
}
|
142 |
}
|
143 |
|
144 |
+
// Function to place the final order
|
145 |
+
function placeOrder() {
|
146 |
+
fetch("/place_order", {
|
147 |
+
method: "POST",
|
148 |
+
headers: { "Content-Type": "application/json" },
|
149 |
+
body: JSON.stringify({ cart: cart })
|
150 |
+
})
|
151 |
+
.then(response => response.json())
|
152 |
+
.then(data => {
|
153 |
+
if (data.message === "Order placed successfully!") {
|
154 |
+
speak("Your order has been placed successfully.");
|
155 |
+
cart = []; // Clear cart after order
|
156 |
+
cartSummary.style.display = "none"; // Hide cart summary
|
157 |
+
} else {
|
158 |
+
speak("An error occurred while placing the order. Please try again.");
|
159 |
+
}
|
160 |
+
})
|
161 |
+
.catch(error => {
|
162 |
+
speak("An error occurred while placing the order. Please try again.");
|
163 |
+
});
|
164 |
}
|
165 |
|
166 |
+
// Event listener for listening button
|
167 |
+
listenButton.addEventListener("click", () => {
|
168 |
+
speak("Please say the item you want to order.");
|
169 |
+
recognition.start();
|
170 |
+
});
|
|
|
|
|
|
|
|
|
171 |
|
172 |
+
recognition.onresult = (event) => {
|
173 |
+
const command = event.results[0][0].transcript.toLowerCase();
|
174 |
+
console.log("User said:", command);
|
175 |
+
if (command.includes("order")) {
|
176 |
+
speak("Which item would you like to order?");
|
177 |
+
} else {
|
178 |
+
speak("Sorry, I couldn't understand that. Please try again.");
|
179 |
+
}
|
180 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
181 |
|
182 |
+
recognition.onerror = (event) => {
|
183 |
+
console.error("Speech recognition error:", event.error);
|
184 |
+
speak("Sorry, I couldn't understand that. Please try again.");
|
185 |
+
};
|
186 |
+
|
187 |
+
// Load menu on page load
|
188 |
+
window.onload = function() {
|
189 |
+
fetchMenu();
|
190 |
}
|
191 |
</script>
|
192 |
|