File tree 1 file changed +23
-4
lines changed
Lecture12/todo-client-side/public
1 file changed +23
-4
lines changed Original file line number Diff line number Diff line change @@ -17,6 +17,7 @@ const fetchFromServer = () => {
17
17
} )
18
18
}
19
19
20
+
20
21
fetchFromServer ( )
21
22
22
23
@@ -43,7 +44,7 @@ document.getElementById("addButton").addEventListener('click', e => {
43
44
} ) . then ( response => response . json ( ) )
44
45
. then ( newBand => bandsState . push ( newBand ) )
45
46
. then ( paint )
46
-
47
+
47
48
// bandsState.push({
48
49
// title: inputText,
49
50
// striked: false
@@ -54,10 +55,28 @@ document.getElementById("addButton").addEventListener('click', e => {
54
55
document . getElementById ( 'list' ) . addEventListener ( 'click' , e => {
55
56
const index = e . target . dataset . index
56
57
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
+
58
70
} )
59
71
60
72
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()
63
82
} )
You can’t perform that action at this time.
0 commit comments