-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.html
More file actions
131 lines (118 loc) · 3.4 KB
/
admin.html
File metadata and controls
131 lines (118 loc) · 3.4 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Inventory Admin</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
background: #f9f9f9;
}
h1 {
text-align: center;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
background: white;
}
th,
td {
border: 1px solid #ccc;
padding: 8px;
text-align: center;
}
th {
background: #444;
color: white;
}
button {
padding: 8px 15px;
margin: 5px;
border: none;
border-radius: 4px;
cursor: pointer;
}
.btn-add {
background: #007bff;
color: white;
}
.btn-update {
background: #ffc107;
color: black;
}
.btn-delete {
background: #dc3545;
color: white;
}
</style>
</head>
<body>
<h1>⚙️ Admin Panel</h1>
<div style="text-align: center">
<button class="btn-add" id="btnAddRandom">Add Random Product</button>
</div>
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Stock</th>
<th>Max Threshold</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="adminTable"></tbody>
</table>
<script>
async function fetchProducts() {
const res = await fetch("/api/products");
const data = await res.json();
const tb = document.getElementById("adminTable");
tb.innerHTML = "";
data.forEach((p) => {
const row = `
<tr>
<td>${p.id}</td>
<td><input value="${p.name}" onchange="updateProduct(${p.id}, 'name', this.value)" /></td>
<td><input type="number" value="${p.stock}" onchange="updateProduct(${p.id}, 'stock', this.value)" /></td>
<td><input type="number" value="${p.maxThreshold}" onchange="updateProduct(${p.id}, 'maxThreshold', this.value)" /></td>
<td><button class="btn-delete" onclick="deleteProduct(${p.id})">Delete</button></td>
</tr>`;
tb.insertAdjacentHTML("beforeend", row);
});
}
async function addRandomProduct() {
const names = ["Keyboard", "Mouse", "Monitor", "Tablet", "Camera"];
const name = names[Math.floor(Math.random() * names.length)];
const stock = Math.floor(Math.random() * 10) + 1;
const maxThreshold = stock + Math.floor(Math.random() * 20) + 5;
await fetch("/api/products", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ name, stock, maxThreshold }),
});
fetchProducts();
}
async function updateProduct(id, field, value) {
const body = {};
body[field] = field === "name" ? value : Number(value);
await fetch(`/api/products/${id}`, {
method: "PUT",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body),
});
fetchProducts();
}
async function deleteProduct(id) {
await fetch(`/api/products/${id}`, { method: "DELETE" });
fetchProducts();
}
document.getElementById("btnAddRandom").onclick = addRandomProduct;
// initial load
fetchProducts();
</script>
</body>
</html>