-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathadminAddProducts.js
131 lines (110 loc) · 2.97 KB
/
adminAddProducts.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
let url = "https://63c6d291dcdc478e15ca4df5.mockapi.io/appliances";
let dashboard = document.getElementById("dashboard-page");
let products = document.getElementById("products-page");
let orders = document.getElementById("orders-page");
let customers = document.getElementById("customers-page");
let addProduct = document.getElementById("adding-product");
let delProduct = document.getElementById("deleting-product");
let add = document.getElementById("add");
let del = document.getElementById("delete-btn");
let home = document.getElementById("home");
home.addEventListener("click",() => {
window.location = "./index.html";
})
dashboard.addEventListener("click",() => {
window.location = "./dashboard.html";
})
products.addEventListener("click",() => {
window.location = "./adminProducts.html";
})
orders.addEventListener("click",() => {
window.location = "./adminOrders.html";
})
customers.addEventListener("click",() => {
window.location = "./adminCustomer.html";
})
add.addEventListener("click",(e) => {
e.preventDefault();
let addData = {
title : addProduct.title.value,
description : addProduct.description.value,
category : addProduct.category.value,
price : addProduct.price.value,
image : addProduct.image.value,
rating : "4.5",
inStock : true
};
let num = formValidation(addData);
if(num == 5){
fetch(url,{
method : "POST",
headers : {
"content-type" : "application/json"
},
body : JSON.stringify(addData)
});
alert("Product Added Successfully !!");
}
});
del.addEventListener("click",(e) => {
e.preventDefault();
let productId = delProduct.deleteId.value;
let x = deleteFormValidation();
if(x == 1){
// fetch(`${url}/${productId}`)
// .then((res) => {
// return res.json();
// })
// .then((data) => {
// arr.push(data);
// })
fetch(`${url}/${productId}`,{
method : "DELETE"
})
alert("Product Deleted Successfully !!");
}
});
function formValidation(data) {
let check = 0;
if(data.title == ""){
alert("Please Enter Title");
}
else{
check++;
}
if(data.description == ""){
alert("Please Enter Description about Product");
}
else{
check++;
}
if(data.category == ""){
alert("Please Select a Category");
}
else{
check++;
}
if(data.price == ""){
alert("Please Enter a Price");
}
else{
check++;
}
if(data.image == ""){
alert("Please Enter a Valid URL for Image");
}
else{
check++;
}
return check;
}
function deleteFormValidation(data){
let check = 0;
if(delProduct.deleteId.value == ""){
alert("Please Enter a ID");
}
else{
check++;
}
return check;
}