Spaces:
Sleeping
Sleeping
Create js/app.js
Browse files- static/js/app.js +33 -0
static/js/app.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// Add item to cart
|
| 2 |
+
function addToCart(name, price) {
|
| 3 |
+
fetch("/add_to_cart", {
|
| 4 |
+
method: "POST",
|
| 5 |
+
headers: { "Content-Type": "application/json" },
|
| 6 |
+
body: JSON.stringify({ name: name, price: price })
|
| 7 |
+
})
|
| 8 |
+
.then(response => response.json())
|
| 9 |
+
.then(data => {
|
| 10 |
+
alert(data.message); // Display message that item was added
|
| 11 |
+
})
|
| 12 |
+
.catch(error => console.error("Error:", error));
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
// Place order (if additional client-side logic is required)
|
| 16 |
+
function placeOrder() {
|
| 17 |
+
const email = document.getElementById("email").value;
|
| 18 |
+
if (!email) {
|
| 19 |
+
alert("Please enter your email to place the order.");
|
| 20 |
+
return;
|
| 21 |
+
}
|
| 22 |
+
fetch("/place_order", {
|
| 23 |
+
method: "POST",
|
| 24 |
+
headers: { "Content-Type": "application/json" },
|
| 25 |
+
body: JSON.stringify({ email: email })
|
| 26 |
+
})
|
| 27 |
+
.then(response => response.json())
|
| 28 |
+
.then(data => {
|
| 29 |
+
alert(data.message); // Display success message
|
| 30 |
+
window.location.href = "/cart"; // Redirect to the cart page
|
| 31 |
+
})
|
| 32 |
+
.catch(error => console.error("Error:", error));
|
| 33 |
+
}
|