-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
111 lines (102 loc) · 2.48 KB
/
main.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
const url2 = 'https://chat-app2-b59ab.firebaseio.com/todo.json?print=pretty';
//----api CRUD OPERATION--------------//
var getData = function(url){
get('#incomplete-tasks').innerHTML = ""
fetch(url)
.then(function(data) {
return data.json()
})
.then(function(data) {
//to get keys of object
var obkeys = Object.keys(data)
for (const [key, value] of Object.entries(data)) {
addLi('#incomplete-tasks' ,value.task , key )
}
})
.catch(function(error) {
alert('get'+error)
});
}
var sendData = function(url){
var task,
task = getvalue('#new-task')
if(!(task === "")){
fetch(url,{
method: 'POST',
body:JSON.stringify({
task : task,
}),
headers:{
"Content-Type" : "application/json; charset=UTF-8"
}
})
.then(function(data) {
return data.json()
})
.then(function(data) {
get('#new-task').value = ""
getData(url2)
})
.catch(function(error) {
alert(error)
});
}else{
alert('Add some query to save')
}
}
var delData = function(url){
fetch(url,{
method: 'DELETE',
body:JSON.stringify({
}),
headers:{
"Content-Type" : "application/json; charset=UTF-8"
}
})
.then(function(data) {
return data.json()
})
.then(function(data) {
getData(url2)
})
.catch(function(error) {
alert("Error : "+error)
});
}
var updateData = function(url){
var upd = prompt("Eneter new query here");
if (upd != null) {
fetch(url,{
method: 'PATCH',
body:JSON.stringify({
task : upd
}),
headers:{
"Content-Type" : "application/json; charset=UTF-8"
}
})
.then(function(data) {
return data.json()
})
.then(function(data) {
getData(url2)
})
.catch(function(error) {
alert("Error : "+error)
});
}else{
alert('no update')
}
}
//----api CRUD OPERATION--------------//
getData(url2)
function addLi(ul ,title , key){
get(ul).innerHTML +=
`
<li>
<label for="">${title}</label>
<button class="edit" onclick="updateData('https://chat-app2-b59ab.firebaseio.com/todo/${key}.json?print=pretty')">Edit</button>
<button class="delete" onclick="delData('https://chat-app2-b59ab.firebaseio.com/todo/${key}.json?print=pretty')">Delete</button>
</li>
`
}