Skip to content

Commit 25ed8ef

Browse files
committed
complete todo
1 parent be67414 commit 25ed8ef

File tree

1 file changed

+23
-4
lines changed
  • Lecture12/todo-client-side/public

1 file changed

+23
-4
lines changed

Lecture12/todo-client-side/public/index.js

+23-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const fetchFromServer = () => {
1717
})
1818
}
1919

20+
2021
fetchFromServer()
2122

2223

@@ -43,7 +44,7 @@ document.getElementById("addButton").addEventListener('click', e => {
4344
}).then(response => response.json())
4445
.then(newBand => bandsState.push(newBand))
4546
.then(paint)
46-
47+
4748
// bandsState.push({
4849
// title: inputText,
4950
// striked: false
@@ -54,10 +55,28 @@ document.getElementById("addButton").addEventListener('click', e => {
5455
document.getElementById('list').addEventListener('click', e => {
5556
const index = e.target.dataset.index
5657
bandsState[index].striked = !bandsState[index].striked
57-
paint()
58+
59+
fetch('api/todos/' + bandsState[index].id, {
60+
method: 'PATCH',
61+
headers: {
62+
'Content-Type': 'application/json',
63+
},
64+
body: JSON.stringify(bandsState[index])
65+
})
66+
.then(response => response.json())
67+
.then(data => bandsState[index]=data)
68+
.then(paint)
69+
5870
})
5971

6072
document.getElementById('clearButton').addEventListener('click', e => {
61-
bandsState = bandsState.filter(band => !band.striked)
62-
paint()
73+
const bandsToDelete = bandsState.filter(band => band.striked)
74+
const deletePromises = bandsToDelete.map(band => {
75+
return fetch('/api/todos/' + band.id, {
76+
method: 'DELETE'
77+
})
78+
})
79+
80+
Promise.all(deletePromises).then(fetchFromServer)
81+
// paint()
6382
})

0 commit comments

Comments
 (0)