-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud.js
107 lines (93 loc) · 2.9 KB
/
crud.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
const taskInput = document.querySelector("#taskInput");
const taskList = document.querySelector("#taskList");
const taskBtn = document.querySelector("#taskBtn");
taskBtn.addEventListener("click", fire);
document.addEventListener("DOMContentLoaded", persistor);
taskList.addEventListener("click", removeTask);
function fire() {
addTask();
saveTask();
taskInput.value = "";
}
function addTask() {
if (taskInput.value === "") {
alert("please input a task");
} else {
taskList.style.display = "block";
let li = document.createElement("li");
li.className = "pTask";
li.appendChild(document.createTextNode(taskInput.value));
let a = document.createElement("a");
let img = document.createElement("img");
img.setAttribute("src", "images/cancel.svg");
img.className = "delete";
a.appendChild(img);
li.appendChild(a);
taskList.appendChild(li);
}
}
function saveTask() {
let taskArr = [];
if (taskInput.value !== "") {
if (localStorage.getItem("task") == null) {
taskArr.push(taskInput.value);
localStorage.setItem("task", JSON.stringify(taskArr));
} else {
taskArr = JSON.parse(localStorage.getItem("task"));
taskArr.push(taskInput.value);
localStorage.setItem("task", JSON.stringify(taskArr));
}
}
}
function persistor() {
let savedData;
if (localStorage.getItem("task") == null) {
savedData = [];
} else {
savedData = JSON.parse(localStorage.getItem("task"));
}
savedData.forEach(element => {
taskList.style.display = "block";
let li = document.createElement("li");
li.className = "pTask";
li.appendChild(document.createTextNode(element));
let a = document.createElement("a");
let img = document.createElement("img");
img.setAttribute("src", "images/cancel.svg");
img.className = "delete";
a.appendChild(img);
li.appendChild(a);
taskList.appendChild(li);
});
}
function removeTask(e) {
if (e.target.classList.contains("delete")) {
e.target.parentElement.parentElement.remove();
removeTaskFromLS(e.target.parentElement.parentElement);
}
}
function removeTaskFromLS(value) {
const savedData = JSON.parse(localStorage.getItem("task"));
savedData.forEach(function(element, index) {
if (element === value.textContent) {
savedData.splice(index, 1);
}
});
localStorage.setItem("task", JSON.stringify(savedData));
}
const btn = document.querySelector("#btn");
btn.addEventListener("click", clearTask);
function clearTask() {
taskList.style.display = "none";
taskList.innerHTML = "";
localStorage.removeItem("task");
}
const signOut = document.querySelector("#signOut");
signOut.addEventListener("click", out);
function out() {
location.replace("login.html");
}
const avatarNameNode = document.querySelector("#avatarName");
let moniker = JSON.parse(localStorage.getItem("moniker"));
console.log(moniker);
avatarNameNode.textContent = `Hello ${moniker}`;