-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathindex.js
118 lines (98 loc) · 3.34 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
112
113
114
115
116
117
118
const db = firebase.firestore();
const taskForm = document.getElementById("task-form");
const tasksContainer = document.getElementById("tasks-container");
let editStatus = false;
let id = '';
/**
* Save a New Task in Firestore
* @param {string} title the title of the Task
* @param {string} description the description of the Task
*/
const saveTask = (title, description) =>
db.collection("tasks").doc().set({
title,
description,
});
const getTasks = () => db.collection("tasks").get();
const onGetTasks = (callback) => db.collection("tasks").onSnapshot(callback);
const deleteTask = (id) => db.collection("tasks").doc(id).delete();
const getTask = (id) => db.collection("tasks").doc(id).get();
const updateTask = (id, updatedTask) => db.collection('tasks').doc(id).update(updatedTask);
window.addEventListener("DOMContentLoaded", async (e) => {
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 (e) => {
console.log(e.target.dataset.id);
try {
await deleteTask(e.target.dataset.id);
} catch (error) {
console.log(error);
taskContainer.innerHTML += `<div class="window-notice" id="window-notice">
<div class="content">
<div class="content-text">Acaba con la edición primero.
<a href="#">Leer más.</a></div>
<div class="content-buttons"><a href='javascript:void(0);' id="close-button"
onclick='document.getElementById('window-notice').className = 'oculto''>
Aceptar</a></div>
</div>
</div>`;
}
})
);
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"];
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);
}
});