-
Notifications
You must be signed in to change notification settings - Fork 0
/
adminpage.js
322 lines (230 loc) · 7.08 KB
/
adminpage.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
let AllData = [];
// sort
let sort1 = document.getElementById("sort_by_price_btn_low");
let sort2=document.getElementById("sort_by_price_btn_high")
sort1.addEventListener("click", () => {
if (AllData && AllData.length) {
AllData.sort((a, b) => {
if (a.price > b.price) return 1;
if (a.price < b.price) return -1;
return 0;
});
rendercardlist(AllData);
} else {
console.log("nothing to sort");
}
});
sort2.addEventListener("click", () => {
if (AllData && AllData.length) {
AllData.sort((a, b) => {
if (a.price < b.price) return 1;
if (a.price > b.price) return -1;
return 0;
});
rendercardlist(AllData);
} else {
console.log("nothing to sort");
}
});
let url="https://testapi2-9psr.onrender.com"
let mainSection=document.getElementById("all_products")
function fetchdata(){
fetch("https://testapi2-9psr.onrender.com/products")
.then((res)=>{
return res.json();
})
.then((data)=>{
console.log(data)
let products_data =data.map(item=>({
id:item.id,
title:item.title,
brand:item.brand,
category:item.category,
imageurl:item.image,
price:item.price
}))
rendercardlist(products_data)
AllData=products_data
count1.innerText=products_data.length
})
}
fetchdata()
function rendercardlist(cardData){
let cardList=`
<div class="row">
${cardData.map(item=>getcard(item.id,item.title,item.brand,item.imageurl,item.category,item.price))
.join("")}
</div>
`
mainSection.innerHTML=cardList
}
function getcard(id,title,brand,imageurl,category,price){
let card=`
<div class="card" data-id=${id}>
<div class="card-image">
<image src=${imageurl} "alt=error"/>
</div>
<div class="card-body">
<div class="space">
<div class="card-brand">${brand}</div>
<div class="card-category">${category} </div></div>
<h3 class=" card-item card-title">${title}</h3>
<div class="card-item card-description">Rs.${price}</div>
<div id="id_number"> id- ${id} </div>
</div>
</div>
</div>`
return card
}
// count
let count=0
let count1=document.getElementById("count1")
let count2=document.getElementById("count2")
let count3=document.getElementById("count3")
let count4=document.getElementById("count4")
let count33=0
let count44=0
// add products
let title1=document.getElementById("add_products_title")
let price1=document.getElementById("add_products_price")
let category1=document.getElementById("add_products_category")
let image1=document.getElementById("add_products_image")
let description1=document.getElementById("add_products_description")
let brand1=document.getElementById("add_products_brand")
let btn1=document.getElementById("add_products_btn")
btn1.addEventListener("click",add_prodoucts)
async function add_prodoucts(){
try{
let obj={
title:title1.value,
price:price1.value,
category:category1.value,
image:image1.value,
description:description1.value,
brand:brand1.value,
}
let add_products1= await fetch("https://testapi2-9psr.onrender.com/products",{
method:'POST',
headers:{
"Content-Type": 'application/json'
},
body:JSON.stringify(obj)
})
fetchdata()
count++
count2.textContent = count
}
catch(error){
console.log(error)
}
}
// update products
let title2=document.getElementById("update_products_title")
let price2=document.getElementById("update_products_price")
let category2=document.getElementById("update_products_category")
let image2=document.getElementById("update_products_image")
let description2=document.getElementById("update_products_description")
let brand2=document.getElementById("update_products_brand")
let id2=document.getElementById("update_products_id")
let btn2=document.getElementById("update_products_btn")
btn2.addEventListener("click",update_products)
async function update_products(){
try{
let id=id2.value;
let title =title2.value;
let price=price2.value;
let category=category2.value;
let image=image2.value;
let description=description2.value;
let brand=brand2.value
let update_products1= await fetch(`${url}/products/${id}`,{
method:'PUT',
headers:{
"Content-Type": 'application/json'
},
body:JSON.stringify({title:title,price:price,category:category,image:image,description:description,brand:brand})
})
fetchdata()
count33++
count3.textContent = count33
}
catch(err){
console.log(err)
}
}
// update price
let id3=document.getElementById("update_price_id")
let price3=document.getElementById("update_price_price")
let btn3=document.getElementById("update_price_btn")
btn3.addEventListener("click",update_price)
async function update_price(){
try{
let id=id3.value;
let price=price3.value
let update_price1= await fetch(`${url}/products/${id}`,{
method:'PATCH',
headers:{
"Content-Type": 'application/json'
},
body:JSON.stringify({price:price})
})
fetchdata()
}
catch(err){
console.log(err)
}
}
// update_brand
let id4=document.getElementById("update_brand_id")
let brand4=document.getElementById("update_brand_brand")
let btn4=document.getElementById("update_brand_btn")
btn4.addEventListener("click",update_brand)
async function update_brand(){
try{
let id=id4.value;
let brand=brand4.value
let update_brand1= await fetch(`${url}/products/${id}`,{
method:'PATCH',
headers:{
"Content-Type": 'application/json'
},
body:JSON.stringify({brand:brand})
})
fetchdata()
}
catch(err){
console.log(err)
}
}
// delete products
let id5=document.getElementById("delete_products_id")
let btn5=document.getElementById("delete_products_btn")
btn5.addEventListener("click",delete_products)
async function delete_products(){
try{
let id=id5.value;
let delete1= await fetch(`${url}/products/${id}`,{
method:'DELETE',
headers:{
"Content-Type": 'application/json'
},
body:JSON.stringify()
})
fetchdata()
count44++
count4.textContent = count44
}
catch(err){
console.log(err)
}
}
// Sort by price low to high
// document.getElementById("sort_by_price_btn_low").addEventListener("click", function(){
// let sortedData = products_data.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
// rendercardlist(sortedData);
// });
// Sort by price high to low
// document.getElementById("sort_by_price_btn_high").addEventListener("click", function(){
// let sortedData = products_data.sort((a, b) => parseFloat(b.price) - parseFloat(a.price));
// rendercardlist(sortedData);
// });