-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
98 lines (89 loc) · 3.54 KB
/
index.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>ALHAMDULILLAH hey folks look my mongo database is gonna done to learn</h1>
<!-- POST DATA FROM UI AND SAVE DATA TO DATABASE -->
<form action="/addProduct" method="post">
<input type="text" placeholder="Product Name" name="name">
<input type="number" placeholder="Price" name="price">
<input type="number" placeholder="Quantity" name="quantity">
<input type="submit" value="Add Product">
</form>
<div id="products">
</div>
<div id="updateProductInfo">
</div>
<script>
// GET DATA ON UI FROM DATABASE
function loadAllProductFromDB () {
fetch("/products")
.then(res => res.json())
.then(pd => {
const container = document.getElementById("products");
container.innerHTML = "";
pd.forEach(element => {
const p = document.createElement("p");
p.innerHTML = `<strong>Product Name: ${element.name}</strong>, Price: ${element.price}, Quantity: ${element.quantity}
<button onclick = "loadSingleProduct('${element._id}')">Update</button>
<button onclick = "deleteProduct(event, '${element._id}')">Delete</button>`;
container.appendChild(p);
});
})
}
loadAllProductFromDB();
// LOAD SINGLE PRODUCT TO UPDATE
function loadSingleProduct(id) {
fetch(`product/${id}`)
.then(res => res.json())
.then(documents => { console.log(documents)
const updateProductInfo = document.getElementById("updateProductInfo");
updateProductInfo.innerHTML = `<h3>Product Name: ${documents.name}</h3>
Price: <input type = "number" value = "${documents.price}" id = "price">
<br>
Quantity: <input type = "number" value = "${documents.quantity}" id = "quantity">
<br>
<button onclick = "updateSingleProductInfo('${documents._id}')">Submit</button>
`;
})
}
// NOW UPDATE SINGLE PRODUCT SPECIFIC INFO
function updateSingleProductInfo(id) {
const price = document.getElementById("price").value;
const quantity = document.getElementById("quantity").value;
const product = {id, price, quantity};
fetch(`/updateProductInfo/${id}`, {
method: "PATCH",
headers: { 'Content-type': `application/json; charset=UTF-8` },
body: JSON.stringify(product),
})
.then(res => res.json())
.then(result => {
if(result){
loadAllProductFromDB();
document.getElementById("updateProductInfo").innerHTML = "";
}
})
}
// DELETE DATA FROM DATABASE BY CLICKING ON UI
function deleteProduct(event, id) {
fetch(`/delete/${id}`, {
method: "DELETE"
})
.then(res => res.json())
.then(result => {
if(result){
loadAllProductFromDB();
// OR OR OR OR OR
// event.target.parentNode.style.display = "none";
}
})
}
</script>
</body>
</html>