forked from riyashri07/SurveyHomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin_dashboard.js
152 lines (121 loc) · 4.11 KB
/
admin_dashboard.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
let pushData=document.querySelector('#add_Survey')
pushData.addEventListener('click', pushToDB)
async function pushToDB(){
let obj={
title:document.querySelector('#survey-title').value,
image:document.querySelector('#survey-image').value,
category:document.querySelector('#survey-select').value
}
let fetchedData=await fetch('https://636a23c5b10125b78fd19a2e.mockapi.io/products', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(obj)
})
if(fetchedData.ok){
alert('Survey added in database!')
window.location.reload()
}
}
let data=[]
let show_all_templates=0;
window.addEventListener('load',async ()=>{
let fetchedProducts=await fetch('https://636a23c5b10125b78fd19a2e.mockapi.io/products', {
method: 'GET',
headers: {
'content-Type': 'application/json'
}
})
if(fetchedProducts.ok){
let res=await fetchedProducts.json()
data=[...res]
renderData(res)
// console.log(res)
show_all_templates=res.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
}
})
function renderData(data){
let renderto= document.querySelector('.right-container-main');
let newData=data.map((elem)=>{
return `
<div id="right-container-cards">
<div><p>${elem.title}</p></div>
<div><p>${elem.category}</p></div>
<div><button id="delete" data-id=${elem.id}>Delete</button></div>
</div>
`
})
renderto.innerHTML=newData.join('');
let deleteRow=document.querySelectorAll('#delete')
for(let del of deleteRow){
del.addEventListener('click', (e)=>{
let ids=e['target']['attributes']['data-id']['nodeValue'];
// console.log(ids)
DeleteData(ids)
})
}
}
async function DeleteData(id){
try {
let deleteFetched=await fetch(`https://636a23c5b10125b78fd19a2e.mockapi.io/products/${id}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
}
})
if(deleteFetched.ok){
console.log('Deleted')
window.location.reload()
}
} catch (error) {
console.log('ERROR!')
}
}
let a=document.querySelectorAll('.checkbox')
for(let select of a){
select.addEventListener('click', function(event){
document.querySelector('#show-details>input').value=null;
let clicked=event.target.defaultValue
let renderto= document.querySelector('.right-container-main');
renderto.innerHTML=null
let filteredData=data.filter((elem)=>{
return elem.category==clicked
})
renderData(filteredData)
show_all_templates=filteredData.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
})
}
document.querySelector('#show-details>input').addEventListener('input',search);
function search(){
unclick()
let input=document.querySelector('#show-details>input').value;
let newData=data.filter((element)=>{
let entrered=element.title.toLowerCase().includes(input.toLowerCase());
if(input){
return entrered;
}
else{
console.log('Nothing to search')
}
})
if(newData.length==0){
renderData(data)
show_all_templates=data.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
}
else{
renderData(newData)
show_all_templates=newData.length
document.querySelector('#show-all-templates').innerHTML=`Showing <span id="show">${show_all_templates}</span> templates`
}
}
function unclick(){
let a=document.querySelectorAll('#left-container-labels');
let n=a[0].children.length
for(let i=0;i<n;i++){
a[0].children[i].children[0].checked=false
}
}