-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
57 lines (48 loc) · 1.97 KB
/
main.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
window.addEventListener("load", function() {
const table = document.getElementById("myTaskTable");
function loadTable() {
fetch('http://localhost:3000/api/tasks')
.then(function(response) {
return response.json();
})
.then(function(taskList) {
taskList.forEach(function(task) {
const row = table.insertRow();
const cell1 = row.insertCell();
cell1.innerText = task.id;
const cell2 = row.insertCell();
cell2.innerText = task.name;
const cell3 = row.insertCell();
cell3.innerText = task.frequency;
});
});
}
loadTable();
const addNewTaskButton = document.getElementById("addNewTask");
addNewTaskButton.addEventListener("click", function() {
/*
1. Get new task input elements
2. Get the contents of the task input elements
3. Create a new row, and add new task cells
*/
const newTaskIDElement = document.getElementById("newTaskID");
const newTaskNameElement = document.getElementById("newTaskName");
const newTaskFrequencyElement = document.getElementById("newTaskFrequency");
const newTaskID = newTaskIDElement.value;
const newTaskName = newTaskNameElement.value;
const newTaskFrequency = newTaskFrequencyElement.value;
const row = table.insertRow();
const cell1 = row.insertCell();
cell1.innerText = newTaskID;
const cell2 = row.insertCell();
cell2.innerText = newTaskName;
const cell3 = row.insertCell();
cell3.innerText = newTaskFrequency;
const newTask = {
id: newTaskID,
name: newTaskName,
frequency: newTaskFrequency
};
fetch("http://localhost:3000/api/tasks", {method: "POST", body: JSON.stringify(newTask)});
});
});