-
Notifications
You must be signed in to change notification settings - Fork 37
/
index.js
111 lines (94 loc) · 2.67 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import {
onGetTasks,
saveTask,
deleteTask,
getTask,
updateTask,
getTasks,
} from "./firebase.js";
const taskForm = document.getElementById("task-form");
const tasksContainer = document.getElementById("tasks-container");
const butonCancel = document.getElementById("btn-task-cancel");
let editStatus = false;
let id = "";
window.addEventListener("DOMContentLoaded", async (e) => {
// const querySnapshot = await getTasks();
// querySnapshot.forEach((doc) => {
// console.log(doc.data());
// });
onGetTasks((querySnapshot) => {
tasksContainer.innerHTML = "";
querySnapshot.forEach((doc) => {
const task = doc.data();
tasksContainer.innerHTML += `
<div class="card card-body mt-2 border-primary">
<h3 class="h5">${task.title}</h3>
<p>${task.description}</p>
<div>
<button class="btn btn-primary btn-delete" data-id="${doc.id}">
🗑 Delete
</button>
<button class="btn btn-secondary btn-edit" data-id="${doc.id}">
🖉 Edit
</button>
</div>
</div>`;
});
const btnsDelete = tasksContainer.querySelectorAll(".btn-delete");
btnsDelete.forEach((btn) =>
btn.addEventListener("click", async ({ target: { dataset } }) => {
try {
await deleteTask(dataset.id);
} catch (error) {
console.log(error);
}
})
);
const btnsEdit = tasksContainer.querySelectorAll(".btn-edit");
btnsEdit.forEach((btn) => {
btn.addEventListener("click", async (e) => {
try {
const doc = await getTask(e.target.dataset.id);
const task = doc.data();
taskForm["task-title"].value = task.title;
taskForm["task-description"].value = task.description;
editStatus = true;
id = doc.id;
taskForm["btn-task-form"].innerText = "Update";
} catch (error) {
console.log(error);
}
});
});
});
});
taskForm.addEventListener("submit", async (e) => {
e.preventDefault();
const title = taskForm["task-title"];
const description = taskForm["task-description"];
if (!title.value || !description.value) {
return alert("Please complete the form");
}
try {
if (!editStatus) {
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();
} catch (error) {
console.log(error);
}
});
butonCancel.addEventListener("click", (e) => {
taskForm.reset();
editStatus = false;
id = "";
});