forked from grt812/todo-api-ui
-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.js
158 lines (120 loc) · 4.39 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
const endpoint = "https://todo.hackrpi.com";
const API_KEY = "INSERT_API_KEY_HERE";
//Get status with /status GET endpoint
async function getStatus() {
}
async function fetchLists() {
}
//Adds list through /AddList POST endpoint.
async function addList() {
const title = newListInputElement.value.trim();
if (title) {
}
}
//Deletes list through /DeleteList DELETE endpoint
async function deleteList(listIdParam) {
}
//Get all list items using GetListItems GET endpoint until next token is exhausted
async function getListItems(listIdParam){
}
//Adds task through /AddListItem post endpoint
async function addTask(listIdParam) {
const taskInput = document.getElementById(`task-input-${listIdParam}`);
const description = taskInput.value.trim();
if (description) {
}
}
//Rename task through /RenameItem/ PATCH endpoint
async function renameTask(thisItemId, newName) {
}
//Set checked task through /SetChecked/ PATCH endpoint
async function setCheckedTask(thisItemId, newChecked) {
}
//Deletes task through /DeleteListItem/ DELETE endpoint
async function deleteTask(taskId) {
}
const addListElement = document.getElementById("add-list");
const listContainerElement = document.getElementById('list-container');
const newListInputElement = document.getElementById('new-list-input');
//Event listeners for menu
addListElement.addEventListener("click", function(){
addList();
});
newListInputElement.onkeydown = function(e){
if(e.key === "Enter"){
addList();
}
};
//Renders each list given an array of list objects
async function renderLists(lists) {
//To preserve the sequence of lists, use for loop instead of forEach (which would run functions in parallel)
let listItems;
for (const e of lists){
listItems = await getListItems(e.id);
renderList({
id: e.id,
listName: e.listName,
items: listItems
});
}
let loadingEl=document.getElementById('loading');
if(loadingEl!==null) loadingEl.remove();
}
const listHTML = `
<div class="list">
<h2 class="list-header"></h2>
<input type="text">
<button>Add</button>
<button>Delete List</button>
<div class="item-list"></div>
</div>
`;
//Renders list
function renderList(list) {
let tempHTML = `
<div id="list-${list.id}" class="list">
<h2 class="list-header">${list.listName}</h2>
<input id="task-input-${list.id}" type="text" value="" class="text-input">
<button id="add-items-${list.id}">Add</button>
<button id="delete-list-${list.id}">Delete List</button>
<div class="item-list"></div>
</div>
`;
let loadingEl=document.getElementById('loading');
if(loadingEl!==null) loadingEl.remove();
document.getElementById("list-container").insertAdjacentHTML("afterbegin", tempHTML);
document.getElementById(`delete-list-${list.id}`).onclick = () => deleteList(list.id);
document.getElementById(`add-items-${list.id}`).onclick = () => addTask(list.id);
document.getElementById(`task-input-${list.id}`).onkeydown = (e) => {
if(e.key === "Enter"){
addTask(list.id)
}
};
list.items.forEach(task => {
createTaskElement(task, list.id);
});
}
//Renders each to-do task
function createTaskElement(task, listId) {
let tempHTML = `
<div id="task-${task.id}" class="item${task.checked ? " completed":""}">
<label class="checkbox-label">
<input id="checkbox-${task.id}" type="checkbox" ${task.checked ? "checked":""}>
<div class="checkbox-display"></div>
</label>
<input id="input-${task.id}" "type="text" value="${task.itemName}" class="text-input task-input">
<button id="delete-${task.id}">Delete Item</button>
</div>
`;
document.getElementById("list-"+listId).querySelector(".item-list").insertAdjacentHTML("afterbegin", tempHTML);
document.getElementById(`input-${task.id}`).onchange = (e) => {
renameTask(task.id, document.getElementById(`input-${task.id}`).value);
};
document.getElementById(`checkbox-${task.id}`).onchange = function(e){
document.getElementById(`task-${task.id}`).classList.toggle('completed', e.target.checked);
setCheckedTask(task.id, e.target.checked);
};
document.getElementById(`delete-${task.id}`).onclick = () => deleteTask(task.id);
}
fetchLists();
getStatus();