-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
155 lines (105 loc) · 3.76 KB
/
app.js
File metadata and controls
155 lines (105 loc) · 3.76 KB
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
const form = document.querySelector('#form_task');
const todoRef = db.collection('todos');
const mytask = document.querySelector('#task');
const btnadd = document.querySelector('#btnadd');
const btnupdate = document.querySelector('#btnupdate');
// using onSnapShot Realtime Listener
todoRef.orderBy('task').onSnapshot(snapshot => {
let changes = snapshot.docChanges();
changes.forEach(task => {
if(task.type == 'added') {
renderTask(task);
}
if(task.type == 'modified') {
document.querySelector('#d_task-'+task.doc.id).firstElementChild.innerHTML = task.doc.data().task // update the value of this element
toastSuccess('Task Updated');
}
if(task.type == 'removed') {
document.querySelector('#d_task-'+task.doc.id).remove()
toastSuccess('Task Deleted');
}
})
})
// index
function renderTask(val) {
let output = `<tr id='d_task-${val.doc.id}' ondblclick='editTask("${val.doc.id}")'>
<td class='pl-2 text-lowercase'> ${val.doc.data().task} </td>
<td> <a class='text-warning font-weight-bold text-decoration-none' href='javascript:void(0)' onclick='deleteTask("${val.doc.id}")'>
<i class='fas fa-times'> </i>
</a> </td>
</tr>`
document.querySelector('#d_task').innerHTML += output ;
}
// store (promised base)
function addTask(e) {
e.preventDefault();
todoRef.add({
task : form.task.value
})
.then(response => toastSuccess('Task Added'));
form.task.value = '';
}
function editTask(task) {
const selected_task = document.querySelector('#d_task-'+task).firstElementChild.innerHTML.trim() ;
mytask.value = selected_task // attach the fetch value to this el
btnadd.style.display = 'none';
btnupdate.style.display = 'block';
btnupdate.setAttribute('data-id', task);
}
function updateTask(e) {
e.preventDefault();
const id = btnupdate.getAttribute('data-id');
todoRef.doc(id).update({
task: mytask.value
})
mytask.value = "";
btnadd.style.display = 'block';
btnupdate.style.display = 'none';
}
// destroy
function deleteTask(task) {
todoRef.doc(task).delete()
}
// check if the form_field is empty then revert to default value
mytask.onkeydown = function(event) {
if(event.keyCode == 8) {
if(mytask.value == "" || mytask == null) {
btnadd.style.display = 'block';
btnupdate.style.display = 'none';
btnupdate.removeAttribute('data-id');
}
}
}
function toastSuccess(message)
{
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": true,
"progressBar": true,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
Command: toastr["success"](`${message} Successfully`, "Success")
}
// without using onSnapShot .
// function getTask() {
// let output = ``;
// todoRef.orderBy('task').get().then(data => {
// data.forEach(task => {
// output += `<tr>
// <td> ${task.data().task} </td>
// <td> <a class='text-dark font-weight-bold text-decoration-none' href='javascript:void(0)' onclick='deleteTask("${task.id}")'> x </a> </td>
// </tr>`
// })
// document.querySelector('#d_task').innerHTML = output;
// })
// }