-
Notifications
You must be signed in to change notification settings - Fork 3
/
admin_page.js
286 lines (248 loc) · 6.92 KB
/
admin_page.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
var companyData=[]
var allUserData=[]
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
//append company data
let datatemp = document.querySelector("#selectdata");
datatemp.addEventListener("change", (event) => {
if (datatemp.value === "company" ) {
compdata()
} else if (datatemp.value === "user" ) {
userdata();
}
});
async function compdata(page_limit=10,page_num=1) {
try {
let alldata = await fetch(
`https://636d633891576e19e327545a.mockapi.io/companies?page=${page_num}&limit=${page_limit}`,{
method: "GET",
headers: {
"Content-Type": "application/json",
}
});
if(alldata.ok){
let data = await alldata.json();
companyData=[...data]
let total_page=10
renderPaginationButtons(total_page)
renderCompany(data);
}
} catch (error) {
alert("something is wrong");
}
}
window.addEventListener("load",(event)=>{
compdata()
})
async function userdata(page_limit=10,page_num=1) {
try {
let alldata = await fetch(
`https://639a1a15e916a46ec0a9808d.mockapi.io/userdata?page=${page_num}&limit=${page_limit}`
);
if(alldata.ok){
let data = await alldata.json();
allUserData=[...data]
let total_page=10
renderPaginationButtons(total_page)
renderUser(data);
}
} catch (error) {
alert("Bad reqest");
}
}
function renderCompany(data) {
let tabelCont = document.querySelector(".maincontainer");
let tabelhead = document.querySelector(".comp_tbl_head");
tabelhead.innerHTML = `
<div class="comp_logo">
<h4>Logo</h4>
</div>
<div class="comp_name">
<h4>Name of the Company</h4>
</div>
<div class="job_role">
<h4>Job Role</h4>
</div>
<div class="Edit_sec">
<h4>Edit Data</h4>
</div>
<div class="delet_sec">
<h4>Delete Data</h4>
</div>
`;
tabelCont.innerHTML = "";
let newdata = data.map((item) => {
return `
<div class="task">
<div class="comp_logo">
<img src="${item.avatar}" alt="${item.companyName}">
</div>
<div class="comp_name">
<h4>${item.companyName}</h4>
</div>
<div class="job_role">
<h4>${item.jobRole}</h4>
</div>
<div class="Edit_sec">
<button class="edt_btn" data-id=${item.id}>Edit Data</button>
</div>
<div class="delet_sec">
<button class="dlt_btn" data-id=${item.id}>Delete Data</button>
</div>
</div>
`;
});
tabelCont.innerHTML = newdata.join(" ");
let all_delete_btn = document.querySelectorAll(".dlt_btn");
for(let btn of all_delete_btn){
btn.addEventListener("click",(event)=>{
let data_id = event.target.dataset.id;
let page_temp=Math.ceil(data_id/10)
deleteData(data_id,page_temp)
});
}
let all_edit_btn = document.querySelectorAll(".edt_btn");
for(let btn of all_edit_btn){
btn.addEventListener("click",(event)=>{
let data_id = event.target.dataset.id;
localStorage.setItem("editId",data_id)
window.location.href="admin_edit_page.html"
});
}
}
function renderUser(data) {
let tabelCont = document.querySelector(".maincontainer");
let tabelhead = document.querySelector(".comp_tbl_head");
tabelhead.innerHTML = `
<div class="profile_pic">
<h4>Profile</h4>
</div>
<div class="username">
<h4>User Name</h4>
</div>
<div class="email_id">
<h4>Email ID</h4>
</div>
<div class="phone_num">
<h4>Phone Number</h4>
</div>
<div class="date">
<h4>Created Date</h4>
</div>
`;
tabelCont.innerHTML = "";
let newdata = data.map((item) => {
return `
<div class="task">
<div class="profile_pic">
<img src="
${item.avatar}" alt="${item.name}">
</div>
<div class="username">
<h4>${item.name}</h4>
</div>
<div class="email_id">
<h4>${item.email}</h4>
</div>
<div class="phone_num">
<h4>${item.phone}</h4>
</div>
<div class="date">
<h5>${item.createdAt}</h5>
</div>
</div>
`;
});
tabelCont.innerHTML = newdata.join(" ");
}
//search functionality
let serbtn=document.querySelector("#search_data")
serbtn.addEventListener("change",(event)=>{
let datashow = document.querySelector("#selectdata");
let showdata=document.querySelector("#search_data").value;
if (datashow.value === "company" ||datashow.value ==="all_data") {
if(serbtn.value.length>0){
searchComp(showdata)
}else{
compdata()
}
}
})
async function searchComp(data){
try {
let showdata=await fetch(`https://636d633891576e19e327545a.mockapi.io/companies?filter=${data}`)
if(showdata.ok){
let datatoshow=await showdata.json()
renderCompany(datatoshow)
}else{
alert("Data not found")
}
} catch (error) {
alert("the requst request")
}
}
//Deletion part
async function deleteData(id,page){
try {
let delete_request = await fetch(`https://636d633891576e19e327545a.mockapi.io/companies/${id}`,{
method : "DELETE",
});
if(delete_request.ok){
alert("Data deleted successfully")
compdata(10,page)
}else{
alert("Data Not Deleted")
}
}
catch (error) {
alert("You don't have access.")
}
}
// PAGINTAION PART
let paginationWrapper = document.querySelector("#pagination-wrapper");
function renderPaginationButtons(total_pages){
paginationWrapper.innerHTML = `
<div className="pagination-btn-list">
${CreatePagButton(total_pages).join(" ")}
</div>
`;
// handle click of pagination-btn(s)
let paginationButtons = document.querySelectorAll('.pagination-btn');
for (let paginationButton of paginationButtons) {
paginationButton.addEventListener('click', function(e){
let page_number = e.target.dataset.id;
let page_limit = 10
let getdata=document.querySelector("#selectdata").value;
if (getdata === "all_data"){
compdata(page_limit,page_number);
}
if( getdata==="company"){
compdata(page_limit,page_number);
}
if(getdata=="user"){
userdata(page_limit,page_number)
}
})
}
}
function getAsButton(text, cls, dataId ) {
return `<button class="${cls}" ${dataId ? `data-id = ${dataId}` : ''} >${text}</button>`
}
function CreatePagButton(total_page){
let array = [];
for(let page=1; page<=total_page;page++){
array.push(getAsButton(page,"pagination-btn",page))
}
return array;
}
let add_btn=document.querySelector("#add_page_btn")
add_btn.addEventListener("click",(event)=>{
localStorage.removeItem("editId")
window.location.href="admin_edit_page.html"
})