-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (87 loc) · 2.51 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
/* eslint-disable no-undef */
//set a variable equal to the body
const body = document.querySelector("body");
//spongebob button
// const clickBtn = document.createElement("button");
// body.appendChild(clickBtn).innerHTML = "Clear Tasks";
//backend testing button
const loadTasksBtn = document.createElement("button");
body.appendChild(loadTasksBtn).innerHTML = "Load Tasks";
// input submit button
const form = document.getElementById("taskForm");
function beTest() {
axios
.get("/test")
.then((res) => {
console.log("talkin 2 tha back", res.data);
const backendDiv = document.createElement("h1");
console.log(res);
body.appendChild(backendDiv).innerHTML = `${res.data}`;
})
.catch((error) => {
console.log("error:", error);
});
}
function addTask(e) {
console.log("post called");
axios
.post("/api/createTask", {
data: {
task: document.getElementById("taskForm").value,
},
})
.then((res) => {
console.log("the add response", res.data.task);
const taskName = res.data.task;
});
}
let taskCount = 0;
function getTasks(e) {
taskCount++;
// e.preventDefault();
axios.get("/api/getAllTasks").then((res) => {
const dataArr = res.data;
const taskArray = [];
dataArr.map((i) => taskArray.push(i.task));
console.log(taskArray);
taskArray.forEach((el) => {
const todoItem = document.createElement("li");
todoItem.setAttribute("id", `todo${el}`);
todoItem.innerHTML = `${el}`;
const toDo = document.getElementById("toDoList");
toDo.appendChild(todoItem);
const deleteBtn = document.createElement("button");
deleteBtn.setAttribute("id", el);
deleteBtn.innerHTML = "❌";
deleteBtn.addEventListener("click", async () => {
await deleteTask(el);
document.getElementById(`todo${el}`).remove();
});
const completeBtn = document.createElement("button");
completeBtn.innerHTML = "✅";
todoItem.appendChild(deleteBtn);
todoItem.appendChild(completeBtn);
});
});
}
function deleteTask(id) {
axios
.delete("/api/deleteTask", {
data: {
task: id,
},
})
.then((res) => console.log("Successfully Deleted ---> ", res.data));
}
//loadTasksBtn.addEventListener("click", getTasks);
loadTasksBtn.addEventListener("click", () => {
if (taskCount === 0) {
getTasks();
}
});
form.addEventListener("keypress", function (e) {
if (e.key === "Enter") {
addTask(e.target.value);
getTasks();
}
});