-
Notifications
You must be signed in to change notification settings - Fork 2
/
add.js
136 lines (117 loc) · 3.56 KB
/
add.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
//Accesing all the products from HTML.
let Id=document.getElementById("id");
let Name=document.getElementById("name");
let Brand=document.getElementById("brand");
let Desc=document.getElementById("description");
let Price=document.getElementById("price");
let Type=document.getElementById("producttype");
let Image=document.getElementById("imageurl");
let AddButton=document.getElementById("Add");
let UpdateButton=document.getElementById("Update");
let GetButton=document.getElementById("Get");
let baseurl="https://63c2fb80b0c286fbe5f73c53.mockapi.io";
let Idvalue=Id.value;
let TotalProductCount=document.getElementById("totalProducts");
let updateimage=document.getElementById("updateImage");
let showbrand=document.getElementById("h3Brand");
let showname=document.getElementById("h3name");
let showprice=document.getElementById("h3price");
let showdesc=document.getElementById("desc");
//Getting the total number of products
fetch(`${baseurl}/Products`)
.then(res=>res.json())
.then(data=>{
TotalProductCount.innerText=data.length
})
//Adding the new product to API with the help of ID. This is the Create functionality of CRUD operation.
AddButton.addEventListener("click",()=>{
let obj={
Brand:Brand.value,
description:Desc.value,
price:"$"+Price.value,
image:Image.value,
name:Name.value,
id:Id.value,
Size:[8,8.5,9,10,10.5,11,13],
color:["Black"],
ProductType:Type.value
};
console.log(obj)
fetch(`${baseurl}/Products`,{
method:'POST',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(obj)
})
.then((res)=>{
return res.json
})
.then((data)=>{
console.log(data)
alert("Product Added Succesfully")
})
})
//Getting the new product from API with the help of ID. This is the Read functionality of CRUD operation.
GetButton.addEventListener("click",()=>{
let obj={
id:Id.value
};
fetch(`${baseurl}/Products/${obj.id}`)
.then(res=>res.json())
.then(data=>{
console.log(data)
Brand.value=data.Brand;
Name.value=data.name;
Desc.value=data.description;
Price.value=data.price;
Type.value=data.ProductType;
Image.value=data.image;
updateimage.src=data.image;
showbrand.innerText=data.Brand;
showname.innerText=data.name;
showdesc.innerText=data.description;
showprice.innerText=data.price;
})
})
//Updating the new product to API with the help of ID. This is the Update functionality of CRUD operation.
UpdateButton.addEventListener("click",()=>{
let obj={
Brand:Brand.value,
description:Desc.value,
price:"$"+Price.value,
image:Image.value,
name:Name.value,
id:Id.value,
Size:[8,8.5,9,10,10.5,11,13],
color:["Black"],
ProductType:Type.value
};
console.log(obj)
fetch(`${baseurl}/Products/${obj.id}`,{
method:'PATCH',
headers:{
'Content-Type':'application/json'
},
body:JSON.stringify(obj)
})
.then((res)=>{
return res.json
})
.then((data)=>{
alert("Product Updated Succesfully")
})
.catch((error)=>{
console.log(error)
})
})
// js for sidebar toogle button
var MenuItem = document.getElementById("sidebar");
MenuItem.style.display = "none";
function menutoggle() {
if (MenuItem.style.display == "none")
MenuItem.style.display = "block";
else {
MenuItem.style.display = "none";
}
}