-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.js
105 lines (86 loc) · 3.2 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
import app from "./index.js";
import { getFirestore, collection, addDoc, getDocs, getDoc, onSnapshot, deleteDoc, doc, updateDoc} from "https://www.gstatic.com/firebasejs/9.1.2/firebase-firestore.js"
const db = getFirestore(app);
//Form Element
const taskForm = document.getElementById("task-form");
const taskTitle = document.getElementById("task-title");
const taskDes = document.getElementById("task-description");
// Card Element
const taskCard = document.getElementById("task-card");
//Update Flag
let editStatus = false;
let id = '';
//CREATE Section
const saveTask = (title, description) =>
addDoc(collection(db, "tasks"),{
title,
description,
});
taskForm.addEventListener("submit", async (e) => {
e.preventDefault();
const title = taskTitle.value;
const description = taskDes.value;
console.log(title, description);
if(!editStatus){
await saveTask(title, description);
} else {
await updateTask(id, {
title: title,
description: description,
})
editStatus = false;
id= '';
taskForm['btn-task-form'].innerText = 'Save';
}
await getTasks();
taskForm.reset();
taskTitle.focus();
});
//READ Section
const getTasks = () => getDocs(collection(db, "tasks"));
const onGetTasks = (callback) => onSnapshot(collection(db, "tasks"), callback);
//Delete Section
const deleteTask = (id) => deleteDoc(doc(db, "tasks", id));
//UPDATE Section
const getTask = (id) => getDoc(doc(db, "tasks", id));
const updateTask = (id, updateTask) => updateDoc(doc(db, "tasks", id), updateTask);
//READ Section
window.addEventListener('DOMContentLoaded', async (e) => {
onGetTasks((querySnapshot) => {
taskCard.innerHTML = ' '
querySnapshot.forEach((doc) => {
console.log(doc.data());
const task = doc.data();
task.id = doc.id;
taskCard.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-danger btn-delete" data-id="${task.id}"> Delete </buttton>
<button class="btn btn-primary btn-edit" data-id="${task.id}"> Edit </buttton>
</div>
</div>`
//DELETE SECTION
const btnsDelete = document.querySelectorAll('.btn-delete');
btnsDelete.forEach(btn => {
btn.addEventListener('click', async (e) => {
await deleteTask(e.target.dataset.id);
})
})
//UPDATE SECTION
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;
taskTitle.value = task.title;
taskDes.value = task.description;
taskForm['btn-task-form'].innerText = 'Update';
})
})
});
})
})