forked from neelmani0/internal-mark-2946
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admindashboard.js
250 lines (193 loc) · 6.92 KB
/
admindashboard.js
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//const { data } = require("cypress/types/jquery");
//const { data } = require("cypress/types/jquery")
let cartdata = JSON.parse(localStorage.getItem("cartdata")) || [];
let checkoutData = JSON.parse(localStorage.getItem("checkoutdata"))||[];
const baseurl = `http://127.0.0.1:9090/`
var x = "CHARGING-SOLUTIONS";
let lowstocks = 0;
let pending = document.getElementById("pending");
let checkout = document.getElementById("checkout")
pending.innerText = cartdata.length;
checkout.innerText = checkoutData.length;
var items = document.getElementById("items")
let totalstock = document.getElementById("zero stocks");
let lowstockcount = document.getElementById("low-stocks")
var category = document.getElementById("category")
let pcat = document.getElementById("pcat");
let productlist = document.getElementsByClassName("product-list");
let update = document.getElementById("update-product");
/////////////////////////////////////////////////
let id = document.getElementById("productu-id");
let pname1 = document.getElementById("productu-name");
let pdesc1 = document.getElementById("productu-description");
let price1 = document.getElementById("productu-price");
let quantity1 = document.getElementById("productu-quantity")
let image11 = document.getElementById("productu-image1")
let image21 = document.getElementById("productu-image2")
let image31 = document.getElementById("productu-image3")
let image41 = document.getElementById("productu-image4")
let cat1 = document.getElementById("category1")
/////////////////////////////////////////////////////////////
let pname = document.getElementById("product-name");
let pdesc = document.getElementById("product-description");
let price = document.getElementById("product-price");
let quantity = document.getElementById("product-quantity")
let image1 = document.getElementById("product-image1")
let image2 = document.getElementById("product-image2")
let image3 = document.getElementById("product-image3")
let image4 = document.getElementById("product-image4")
let form1 = document.querySelector("form")
category.addEventListener("change", () => {
x = category.value;
lowstockcount.innerText = lowstocks.length;
pcat.innerText = `:-${x}`
fetchdata(x)
})
//console.log(x)
fetchdata(x)
function fetchdata() {
fetch(`${baseurl}${category.value}`)
.then((res) => res.json())
.then((data) => {
// console.log(data)
lowstocks = data.filter((el) => el.stock <= 50)
lowstockcount.innerText = lowstocks.length;
console.log(lowstocks)
totalstock.innerText = data.length;
append(data)
})
.catch((er) => {
console.log(er)
})
}
function append(data) {
items.innerHTML = ""
console.log(data)
data.forEach(element => {
var li = document.createElement("li");
var div = document.createElement("div");
var image = document.createElement("img");
var div2 = document.createElement("div");
var name = document.createElement("h3");
var p1 = document.createElement("p")
var p2 = document.createElement("p")
var p3 = document.createElement("p")
var edit = document.createElement("button");
var del = document.createElement("button");
div.setAttribute("class", "product")
image.src = element.images[0];
div2.setAttribute("class", "product-details");
name.innerText = element.title;
p1.innerText = `stock available :- ${element.stock}`;
p2.innerText = `price:${element.price}`
p3.innerText = element.category;
edit.innerText = "EDIT";
del.innerText = "DELETE"
edit.addEventListener("click", () => {
editdata(element)
})
del.addEventListener("click", () => {
deleteProduct(element.id)
fetchdata();
})
if (element.stock <= 50) {
div.style.backgroundColor = "lightpink";
} else if (element.stock > 50 && element.stock < 100) {
div.style.backgroundColor = "#eced93";
} else {
div.style.backgroundColor = "lightgreen";
}
div2.append(name, p1, p2, p3, edit, del)
div.append(image, div2);
li.append(div)
items.append(li)
});
}
form1.addEventListener("submit", (e) => {
e.preventDefault();
let obj = {
title: pname.value,
description: pdesc.value,
stock: Number(quantity.value),
price: price.value,
images: [image1.value, image2.value, image3.value, image4.value],
category: category.value
}
console.log(obj)
addProduct(obj)
fetchdata()
})
async function addProduct(obj) {
try {
let productDetails = await fetch(`${baseurl}${category.value}`, {
method: "POST",
body: JSON.stringify(obj),
headers: {
"Content-Type": "application/json",
},
});
if (productDetails.ok) {
alert("Data Appended");
}
} catch (error) {
alert("Bad Request");
}
}
function editdata(obj1) {
id.value = obj1.id;
pname1.value = obj1.title;
pdesc1.value = obj1.description;
price1.value = obj1.price;
quantity1.value = obj1.stock;
image11.value = obj1.images[0];
image21.value = obj1.images[1];
image31.value = obj1.images[2];
image41.value = obj1.images[3];
// cat1.value = obj1.category;
}
update.addEventListener("submit", (e) => {
e.preventDefault();
let obj = {
title: pname1.value,
description: pdesc.value,
stock: quantity1.value,
price: price1.value,
images: [image11.value, image21.value, image31.value, image41.value],
category: cat1.value
}
console.log(obj)
//append(x)
editProduct(id.value, obj)
fetchdata()
})
async function editProduct(id, data) {
try {
let x = await fetch(
`${baseurl}${cat1.value}/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(data),
}
);
model.style.display = "none";
window.location.reload();
//displayTable();
} catch (error) {
console.log("Bad request");
}
}
async function deleteProduct(id) {
try {
let x = await fetch(`${baseurl}${category.value}/${id}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
});
window.location.reload();
} catch (error) {
console.log("Bad request for deleting");
}
}