-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderItem.js
52 lines (43 loc) · 1.5 KB
/
renderItem.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
export default function renderItem(id, todo, li) {
const label = document.createElement('label');
label.style.cssText = 'display: flex; flex: 1; gap: 1ex;';
li.append(label);
const input = document.createElement('input');
input.type = 'checkbox';
input.checked = todo.done;
label.append(input);
input.addEventListener('change', async () => {
input.disabled = true;
await fetch('/todos/' + id, { method: 'POST', body: JSON.stringify({ ...todo, done: input.checked }) });
input.disabled = false;
itemUl.prepend(li);
});
const nameSpan = document.createElement('span');
nameSpan.textContent = todo.text;
label.append(nameSpan);
const renameButton = document.createElement('button');
renameButton.textContent = 'Rename';
li.append(renameButton);
renameButton.addEventListener('click', async () => {
const text = prompt('Name:', todo.text)?.trim();
if (!text) {
return;
}
renameButton.disabled = true;
await fetch('/todos/' + id, { method: 'POST', body: JSON.stringify({ ...todo, text }) });
renameButton.disabled = false;
label.replaceChildren(input, ' ', text);
itemUl.prepend(li);
});
const deleteButton = document.createElement('button');
deleteButton.textContent = 'Delete';
li.append(deleteButton);
deleteButton.addEventListener('click', async () => {
if (!confirm(`Delete "${todo.text}"?`)) {
return;
}
deleteButton.disabled = true;
await fetch('/todos/' + id, { method: 'DELETE' });
li.remove();
});
}