-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
52 lines (47 loc) · 1.43 KB
/
Copy pathscript.js
File metadata and controls
52 lines (47 loc) · 1.43 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
firebase.initializeApp({
// Import the functions you need from the SDKs you need
apiKey: "YOUR API",
authDomain: "YOUR_AUTH_DOMAIN",
projectId: "YOUR PROJECT ID",
storageBucket: "...........com",
messagingSenderId: "00000000000",
appId: "1............",
});
const db = firebase.firestore();
//function to add our tasks
function addTask(){
const taskInput = document.getElementById("task-input");
const task = taskInput.value.trim();
if(task !== ""){
db.collection("tasks").add({
task: task,
timestamp: firebase.firestore.FieldValue.serverTimestamp()
});
taskInput.value = "";
console.log("Task added.");
}
}
function renderTasks(doc){
const taskList = document.getElementById("task-list");
const taskItem = document.createElement("li");
taskItem.className = "task-item";
taskItem.innerHTML = `
<span>${doc.data().task}</span>
<button onclick="deleteTask('${doc.id}')">Delete</button>
`;
taskList.appendChild(taskItem);
}
db.collection("tasks")
.orderBy("timestamp","desc")
.onSnapshot(snapshot =>{
const changes = snapshot.docChanges();
changes.forEach(change =>{
if(change.type ==="added"){
renderTasks(change.doc)
}
});
});
function deleteTask(id){
db.collection("tasks").doc(id).delete();
location.reload();
}