@@ -8,8 +8,48 @@ export const toggleAddBook = () => {
8
8
}
9
9
}
10
10
11
- export const addTodo = ( todo ) => {
11
+ export const addNewTodo = ( todo ) => { console . log ( todo )
12
+ return ( dispatch ) => {
13
+ dispatch ( addNewTodoRequest ( todo ) ) ;
14
+ return fetch ( apiUrl , {
15
+ method :'post' ,
16
+ // headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
17
+ body : todo ,
18
+ } ) . then ( response => {
19
+ if ( response . ok ) {
20
+ response . json ( ) . then ( data => { console . log ( data . todo ) ;
21
+ dispatch ( addNewTodoRequestSuccess ( data . todo , data . message ) )
22
+ } )
23
+ }
24
+ else {
25
+ response . json ( ) . then ( error => {
26
+ dispatch ( addNewTodoRequestFailed ( error ) )
27
+ } )
28
+ }
29
+ } )
30
+ }
31
+ }
12
32
33
+ export const addNewTodoRequest = ( todo ) => {
34
+ return {
35
+ type : 'ADD_NEW_TODO_REQUEST' ,
36
+ todo
37
+ }
38
+ }
39
+
40
+ export const addNewTodoRequestSuccess = ( todo , message ) => {
41
+ return {
42
+ type : 'ADD_NEW_TODO_REQUEST_SUCCESS' ,
43
+ todo :todo ,
44
+ message :message
45
+ }
46
+ }
47
+
48
+ export const addNewTodoRequestFailed = ( error ) => {
49
+ return {
50
+ type : 'ADD_NEW_TODO_REQUEST_FAILED' ,
51
+ error
52
+ }
13
53
}
14
54
15
55
export const deleteTodo = ( todo ) => {
@@ -29,15 +69,20 @@ export const fetchTodos = () => {
29
69
dispatch ( fetchTodosRequest ( ) ) ;
30
70
// Returns a promise
31
71
return fetch ( apiUrl )
32
- . then ( response => response . json ( ) )
33
- . then ( data =>
34
- // dispatch another action
35
- // to consume data
36
- dispatch ( fetchTodosSuccess ( data . todos , data . message ) )
37
- )
38
- . then ( error => {
39
- throw ( error ) ;
72
+ . then ( response => {
73
+ if ( response . ok ) {
74
+ response . json ( ) . then ( data => {
75
+ dispatch ( fetchTodosSuccess ( data . todos , data . message ) ) ;
76
+ } )
77
+ }
78
+ else {
79
+ response . json ( ) . then ( error => {
80
+ dispatch ( fetchTodosFailed ( error ) ) ;
81
+ } )
82
+ }
40
83
} )
84
+
85
+
41
86
}
42
87
}
43
88
@@ -56,4 +101,56 @@ export const fetchTodosSuccess = (todos,message) => {
56
101
message : message ,
57
102
receivedAt : Date . now
58
103
}
59
- } ;
104
+ }
105
+
106
+ export const fetchTodosFailed = ( error ) => {
107
+ return {
108
+ type :'FETCH_TODOS_FAILED' ,
109
+ error
110
+ }
111
+ }
112
+
113
+ export const fetchTodoById = ( todoId ) => {
114
+ return ( dispatch ) => {
115
+ dispatch ( fetchTodoRequest ( ) ) ;
116
+ // Returns a promise
117
+ return fetch ( apiUrl + todoId )
118
+ . then ( response => { console . log ( response )
119
+ if ( response . ok ) {
120
+ response . json ( ) . then ( data => {
121
+ dispatch ( fetchTodoSuccess ( data . todo [ 0 ] , data . message ) ) ;
122
+ } )
123
+ }
124
+ else {
125
+ response . json ( ) . then ( error => {
126
+ dispatch ( fetchTodoFailed ( error ) ) ;
127
+ } )
128
+ }
129
+ } )
130
+
131
+ }
132
+ }
133
+
134
+ export const fetchTodoRequest = ( ) => {
135
+ return {
136
+ type :'FETCH_TODO_REQUEST'
137
+ }
138
+ }
139
+
140
+
141
+ //Sync action
142
+ export const fetchTodoSuccess = ( todo , message ) => {
143
+ return {
144
+ type : 'FETCH_TODO_SUCCESS' ,
145
+ todo : todo ,
146
+ message : message ,
147
+ receivedAt : Date . now
148
+ }
149
+ }
150
+
151
+ export const fetchTodoFailed = ( error ) => {
152
+ return {
153
+ type :'FETCH_TODO_FAILED' ,
154
+ error
155
+ }
156
+ }
0 commit comments