@@ -3,7 +3,8 @@ import ReactDOM from 'react-dom';
3
3
import { applyMiddleware , combineReducers , createStore } from 'redux' ;
4
4
import { Provider , connect } from 'react-redux' ;
5
5
import { createLogger } from 'redux-logger' ;
6
- import thunk from 'redux-thunk' ;
6
+ import createSagaMiddleware , { delay } from 'redux-saga' ;
7
+ import { put , takeEvery } from 'redux-saga/effects' ;
7
8
import { schema , normalize } from 'normalizr' ;
8
9
import uuid from 'uuid/v4' ;
9
10
import './index.css' ;
@@ -26,6 +27,7 @@ const TODO_ADD = 'TODO_ADD';
26
27
const TODO_TOGGLE = 'TODO_TOGGLE' ;
27
28
const FILTER_SET = 'FILTER_SET' ;
28
29
const NOTIFICATION_HIDE = 'NOTIFICATION_HIDE' ;
30
+ const TODO_ADD_WITH_NOTIFICATION = 'TODO_ADD_WITH_NOTIFICATION' ;
29
31
30
32
// reducers
31
33
@@ -128,13 +130,10 @@ function applyRemoveNotification(state, action) {
128
130
// action creators
129
131
130
132
function doAddTodoWithNotification ( id , name ) {
131
- return function ( dispatch ) {
132
- dispatch ( doAddTodo ( id , name ) ) ;
133
-
134
- setTimeout ( function ( ) {
135
- dispatch ( doHideNotification ( id ) ) ;
136
- } , 5000 ) ;
137
- }
133
+ return {
134
+ type : TODO_ADD_WITH_NOTIFICATION ,
135
+ todo : { id, name } ,
136
+ } ;
138
137
}
139
138
140
139
function doHideNotification ( id ) {
@@ -186,6 +185,20 @@ function getArrayOfObject(object) {
186
185
return Object . keys ( object ) . map ( key => object [ key ] ) ;
187
186
}
188
187
188
+ // sagas
189
+
190
+ function * watchAddTodoWithNotification ( ) {
191
+ yield takeEvery ( TODO_ADD_WITH_NOTIFICATION , handleAddTodoWithNotification ) ;
192
+ }
193
+
194
+ function * handleAddTodoWithNotification ( action ) {
195
+ const { todo } = action ;
196
+ const { id, name } = todo ;
197
+ yield put ( doAddTodo ( id , name ) ) ;
198
+ yield delay ( 5000 ) ;
199
+ yield put ( doHideNotification ( id ) ) ;
200
+ }
201
+
189
202
// store
190
203
191
204
const rootReducer = combineReducers ( {
@@ -195,13 +208,16 @@ const rootReducer = combineReducers({
195
208
} ) ;
196
209
197
210
const logger = createLogger ( ) ;
211
+ const saga = createSagaMiddleware ( ) ;
198
212
199
213
const store = createStore (
200
214
rootReducer ,
201
215
undefined ,
202
- applyMiddleware ( thunk , logger )
216
+ applyMiddleware ( saga , logger )
203
217
) ;
204
218
219
+ saga . run ( watchAddTodoWithNotification ) ;
220
+
205
221
// components
206
222
207
223
function TodoApp ( ) {
0 commit comments