-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (69 loc) · 2.84 KB
/
index.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
const db = firebase.firestore();
const taskform = document.getElementById("task-form");
const tasksConteiner = document.getElementById("tasks-conteiner");
let editStatus = false;
let id = "";
const saveTask = (title, description) =>
db.collection("tasks").doc().set({
title: title,
description:description
});
const getTasks = () => db.collection('tasks').get();
const getTask = (id) => db.collection('tasks').doc(id).get();
const onGetTasks = (callback) => db.collection('tasks').onSnapshot(callback);
const deleteTask = id => db.collection('tasks').doc(id).delete();
const updateTask = (id, updatedTask) =>
db.collection('tasks').doc(id).update(updatedTask);
window.addEventListener('DOMContentLoaded',async (e) =>{
onGetTasks((querySnapShot)=>{
tasksConteiner.innerHTML ="";
querySnapShot.forEach(doc =>{
console.log(doc.data());
const task = doc.data();
task.id = doc.id;
tasksConteiner.innerHTML += `<div class="card card-body mt-2
border-primary">
<h5> ${task.title} </h5>
<p> ${task.description} </p>
<button class="btn btn-primary btn-delete mb-2" data-id="${task.id}" > Delete </button>
<button class="btn btn-primary btn-edit" data-id="${task.id}"> Edit </button>
</div>`;
const btnsDetele = document.querySelectorAll('.btn-delete')
btnsDetele.forEach(btn => {
btn.addEventListener('click', async(e) =>{
await deleteTask(e.target.dataset.id);
})
})
const btnsEdit = document.querySelectorAll('.btn-edit')
btnsEdit.forEach(btn => {
btn.addEventListener('click', async(e) =>{
const doc = await getTask(e.target.dataset.id);
const task = doc.data();
editStatus = true;
id = doc.id;
taskform['task-title'].value = task.title;
taskform['task-description'].value = task.description;
taskform['btn-task-form'].innerText = "Update";
})
})
})
})
})
taskform.addEventListener("submit",async (e)=>{
e.preventDefault();
const title = taskform["task-title"];
const description = taskform["task-description"];
if (editStatus = false){
await saveTask(title.value, description.value);
}else {
await updateTask(id, {
title: title.value,
description: description.value
});
editStatus = false;
id= "";
taskForm['btn-task-form'].innerText ="Save"
}
taskform.reset();
title.focus();
})