forked from nourbenamor201/lab_assignment
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.html
145 lines (129 loc) · 5.07 KB
/
app.html
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Products App</title>
<style>
body { font-family: Arial, sans-serif; margin: 20px; }
.container { max-width: 800px; margin: 0 auto; }
table { width: 100%; border-collapse: collapse; margin-bottom: 20px; }
table th, table td { padding: 8px; text-align: left; border: 1px solid #ddd; }
button { padding: 10px 15px; margin: 5px; cursor: pointer; }
input, label { margin: 5px 0; }
.form-group { margin-bottom: 15px; }
th:last-child, td:last-child {
display: none;
}
</style>
</head>
<body>
<div class="container">
<h1>Product Management</h1>
<div class="form-group">
<label for="productName">Product Name</label>
<input type="text" id="productName" placeholder="Enter product name">
</div>
<div class="form-group">
<label for="productPrice">Price</label>
<input type="number" id="productPrice" placeholder="Enter price">
</div>
<div class="form-group">
<label for="productQuantity">Quantity</label>
<input type="number" id="productQuantity" placeholder="Enter quantity">
</div>
<button onclick="addProduct()">Add Product</button>
<h2>Product List</h2>
<table id="productTable">
<thead>
<tr>
<th>Name</th>
<th>Price</th>
<th>Quantity</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<!-- Product rows will be inserted here -->
</tbody>
</table>
</div>
<script>
const apiUrl = 'http://localhost:5000/products'; // Update if different
// Fetch and display products
async function getProducts() {
try {
const response = await fetch(apiUrl);
const products = await response.json();
const tableBody = document.querySelector("#productTable tbody");
tableBody.innerHTML = '';
products.forEach(product => {
const row = document.createElement("tr");
row.innerHTML = `
<td>${product.name}</td>
<td>${product.price}</td>
<td>${product.quantity}</td>
<td>
<button onclick="deleteProduct('${product._id}')">Delete</button>
<button onclick="editProduct('${product._id}')">Edit</button>
</td>
`;
tableBody.appendChild(row);
});
} catch (error) {
console.error('Error fetching products:', error);
}
}
// Add product
async function addProduct() {
const name = document.getElementById("productName").value;
const price = document.getElementById("productPrice").value;
const quantity = document.getElementById("productQuantity").value;
if (name && price && quantity) {
try {
await fetch(apiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name, price, quantity }),
});
getProducts();
} catch (error) {
console.error('Error adding product:', error);
}
}
}
// Delete product
async function deleteProduct(productId) {
try {
await fetch(`${apiUrl}/${productId}`, { method: 'DELETE' });
getProducts();
} catch (error) {
console.error('Error deleting product:', error);
}
}
// Edit product (you can expand this to allow actual editing)
async function editProduct(productId) {
const newName = prompt('Enter new product name');
const newPrice = prompt('Enter new price');
const newQuantity = prompt('Enter new quantity');
if (newName && newPrice && newQuantity) {
try {
await fetch(`${apiUrl}/${productId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ name: newName, price: newPrice, quantity: newQuantity }),
});
getProducts();
} catch (error) {
console.error('Error updating product:', error);
}
}
}
getProducts();
</script>
</body>
</html>