-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtodo.js
62 lines (50 loc) · 1.66 KB
/
todo.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
import renderItem from './renderItem.js';
const editorInput = document.querySelector('#editorInput');
const addButton = document.querySelector('#addButton');
const itemUl = document.querySelector('#itemUl');
async function render() {
itemUl.replaceChildren('Loading…');
const response = await fetch('/todos?full');
const todos = await response.json();
itemUl.replaceChildren();
for (const { id, data: todo } of todos) {
const li = document.createElement('li');
li.style.cssText = 'display: flex; gap: 1ex;';
renderItem(id, todo, li);
itemUl.append(li);
}
}
await render();
editorInput.addEventListener('keydown', async (event) => {
const value = editorInput.value.trim();
if (!value) {
return;
}
if (event.key !== 'Enter') {
return;
}
editorInput.disabled = true;
await fetch('/todos/' + value, { method: 'POST', body: JSON.stringify({ text: value }) });
editorInput.value = '';
editorInput.disabled = false;
const li = document.createElement('li');
li.style.cssText = 'display: flex; gap: 1ex;';
itemUl.prepend(li);
renderItem(value, { text: value, done: false }, li);
});
editorInput.addEventListener('keyup', () => {
addButton.disabled = !editorInput.value.trim();
});
addButton.addEventListener('click', async () => {
const value = editorInput.value.trim();
if (!value) {
return;
}
addButton.disabled = true;
await fetch('/todos/' + value, { method: 'POST', body: JSON.stringify({ text: value }) });
editorInput.value = '';
const li = document.createElement('li');
li.style.cssText = 'display: flex; gap: 1ex;';
itemUl.prepend(li);
renderItem(value, { text: value, done: false }, li);
});