-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.js
213 lines (180 loc) · 7.01 KB
/
admin.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
let url = "https://63c812db075b3f3a91d99323.mockapi.io/Clothes";
//catching the main product container
let mainContainer = document.getElementById("productInnerContainer")
//catching edit produc button
let editProductButton = document.getElementById("editProductButton");
//catching the product update form
let updateid = document.getElementById("update-product-id");
let updateProductImage = document.getElementById("update-product-image");
let updateProductTitle = document.getElementById("update-product-title");
let updateProductDescription = document.getElementById("update-product-description");
let updateProductRating = document.getElementById("update-product-rating");
let updateProductPrice = document.getElementById("update-product-price");
let updateProductUpdateBtn = document.getElementById("update-product");
//catching the add new product form
let addProductImage = document.getElementById("add-product-image");
let addProductTitle = document.getElementById("add-product-title");
let addProductDescription = document.getElementById("add-product-description");
let addProductRating = document.getElementById("add-product-rating");
let addProductPrice = document.getElementById("add-product-price");
let addProductBtn = document.getElementById("add-product");
// catching delete product form
let deleteid = document.getElementById("delete-product-id");
let deleteProductBtn = document.getElementById("delete-product");
addEventListener("load", (event) => {
fetchProductsFunction();
});
async function fetchProductsFunction() {
try {
// by default no need to pass object to fetch function bcz by default request if get only
let getProducts = await fetch(`${url}`);
let productData = await getProducts.json();
console.log(productData);
// employeesDataMainArray = employeeData;
// now will call a function that will render the employee data
renderCardList(productData);
} catch (error) {
console.log(error);
}
}
// render product cards
function renderCardList(cardData) {
let cardList = `
<div class="card-list">
${cardData
.map((item) =>
getCard(
item.Image,
item.Title,
item.Description,
item.Rating,
item.Price,
item.id,
)
)
.join("")}
</div>
`;
mainContainer.innerHTML = cardList;
// let editLinks = document.querySelectorAll(".card__link");
// for (let editLink of editLinks) {
// editLink.addEventListener("click", (e) => {
// e.preventDefault();
// let currentId = e.target.dataset.id;
// populateEditForms(currentId);
// });
// }
}
function getCard(image, title, desc, rating, price, id) {
let card = `
<div class="product-card">
<img src="${image}" alt="undefined" class = "productImg">
<a href="productsdetails.html">
<h3 class="product-title">${title}</h3>
</a>
<p class="product-description">${desc}</p>
<div class="rating">${rating}
<img src="icons8.png">
<img src="icons8.png">
<img src="icons8.png">
<img src="icons8.png">
<img src="icons8.png">
</div>
<p>${price}(20% off)</p>
<!-- <button class="productBtn" id="editProductButton">EDIT</button> -->
<span>Reference ID:${id}</span>
</div>
`;
return card;
}
// update alll the fields of the products
updateProductUpdateBtn.addEventListener("click", updateProduct);
async function updateProduct() {
try {
let id = +updateid.value;
let obj = {
// id: updateid.value,
Image: updateProductImage.value,
Title: updateProductTitle.value,
Description: updateProductDescription.value,
Rating: updateProductRating.value,
Price: updateProductPrice.value,
};
console.log(obj);
let updateProduct = await fetch(`https://63c812db075b3f3a91d99323.mockapi.io/Clothes/${id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(obj),
});
console.log(updateProduct);
alert("Product updated");
} catch (error) {
console.log(error);
}
}
// SHOW THE PRODUCT UPDATE SUCCESSFULL TOAST +++++++++++++++++++++++++++++++++
// const toastTrigger = document.getElementById('update-product')
// const toastLiveExample = document.getElementById('liveToast')
// if (toastTrigger) {
// toastTrigger.addEventListener('click', () => {
// setTimeout(() => {
// const toast = new bootstrap.Toast(toastLiveExample)
// toast.show()
// }, 1000);
// })
// }
// SHOW THE PRODUCT UPDATE SUCCESSFULL TOAST ------------------------------------------
// creating new products+++++++++++++++++++++++++++++++++++++++++
addProductBtn.addEventListener("click", createProduct);
async function createProduct() {
try {
let obj = {
// id: updateid.value,
Image: addProductImage.value,
Title: addProductTitle.value,
Description: addProductDescription.value,
Rating: addProductRating.value,
Price: addProductPrice.value,
};
console.log(obj);
let productData = await fetch(`${url}`, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(obj),
});
console.log(productData)
alert("product added successfully")
} catch (error) {
console.log(error);
}
}
// creating new products------------------------------------
// deleting products+++++++++++++++++++++++++++++++++++++++++
deleteProductBtn.addEventListener("click", deleteProduct);
async function deleteProduct() {
try {
let deleteId = +deleteid.value;
let productData = await fetch(`${url}/${deleteId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({id:deleteId}),
});
console.log(productData)
alert("product deleted successfully")
} catch (error) {
console.log(error);
}
}
// deleting products------------------------------------
// redirecting to update section on clicking the edit button present in product card++++++++++++++++++++++++++++++
// editProductButton.addEventListener("click",function(){
// let toggle =document.getElementById('ex1-tab-2').getAttribute('aria-expanded')
// console.log(toggle);
// })
// redirecting to update section on clicking the edit button present in product card----------------------------------