-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
75 lines (64 loc) · 2.02 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
import { initializeApp } from "https://www.gstatic.com/firebasejs/9.15.0/firebase-app.js";
import {
getDatabase,
ref,
push,
onValue,
remove,
} from "https://www.gstatic.com/firebasejs/9.15.0/firebase-database.js";
const appSettings = {
databaseURL: "https://mobile-project-b8bef-default-rtdb.firebaseio.com",
};
const app = initializeApp(appSettings);
const database = getDatabase(app);
const taskListInDB = ref(database, "taskList");
// html element variables
const inputField = document.getElementById("inputField");
const addButton = document.getElementById("addButton");
const taskList = document.getElementById("taskList");
let taskAmount = 0;
addButton.addEventListener("click", function () {
let inputValue = inputField.value;
if (inputValue != "" && taskAmount < 5) {
push(taskListInDB, inputValue);
taskAmount++;
} else if (inputValue == "") {
alert("Sorry, you need to write something");
} else {
alert("You have reached the maxinum amount of tasks");
}
clearInput();
});
function clearInput() {
inputField.value = "";
}
onValue(taskListInDB, function (snapshot) {
if (snapshot.exists()) {
let taskArray = Object.entries(snapshot.val());
clearTaskList();
for (let i = 0; i < taskArray.length; i++) {
let currentTask = taskArray[i];
addTask(currentTask);
}
} else {
clearTaskList();
}
});
function clearTaskList() {
taskList.innerHTML = "";
}
function addTask(taskEntry) {
let taskID = taskEntry[0];
let taskItem = taskEntry[1];
let newElement = document.createElement("li");
newElement.textContent = taskItem;
newElement.addEventListener("click", function () {
let taskLocation = ref(database, `taskList/${taskID}`);
remove(taskLocation);
taskAmount--;
if (taskAmount < 0) {
taskAmount = 0;
}
});
taskList.append(newElement);
}