-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-cart.html
More file actions
74 lines (65 loc) · 2.04 KB
/
Copy pathtest-cart.html
File metadata and controls
74 lines (65 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<!doctype html>
<html>
<head>
<title>Cart API Test</title>
</head>
<body>
<h1>Cart API Test</h1>
<button onclick="getCart()">Get Cart</button>
<button onclick="addItem1()">Add Item 1</button>
<button onclick="addItem2()">Add Item 2</button>
<button onclick="addItem3()">Add Item 3</button>
<pre id="output"></pre>
<script>
const API_BASE = "https://api.eatwella.ng/api/api";
const output = document.getElementById("output");
async function getCart() {
try {
const res = await fetch(`${API_BASE}/cart`, {
credentials: "include",
headers: { Accept: "application/json" },
});
const data = await res.json();
output.textContent = "GET CART:\n" + JSON.stringify(data, null, 2);
// Calculate count
const count =
data?.items?.reduce((sum, item) => sum + item.quantity, 0) || 0;
} catch (err) {
output.textContent = "Error: " + err.message;
}
}
async function addItem1() {
await addToCart(1);
}
async function addItem2() {
await addToCart(2);
}
async function addItem3() {
await addToCart(3);
}
async function addToCart(menuId) {
try {
const res = await fetch(`${API_BASE}/cart`, {
method: "POST",
credentials: "include",
headers: {
Accept: "application/json",
"Content-Type": "application/json",
},
body: JSON.stringify({ menu_id: menuId, quantity: 1 }),
});
const data = await res.json();
output.textContent =
`ADD ITEM ${menuId}:\n` + JSON.stringify(data, null, 2);
// Calculate count
const count =
data?.items?.reduce((sum, item) => sum + item.quantity, 0) || 0;
} catch (err) {
output.textContent = "Error: " + err.message;
}
}
// Auto-fetch cart on load
getCart();
</script>
</body>
</html>