@@ -3,7 +3,8 @@ import ReactDOM from 'react-dom';
33import { applyMiddleware , combineReducers , createStore } from 'redux' ;
44import { Provider , connect } from 'react-redux' ;
55import { 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' ;
78import { schema , normalize } from 'normalizr' ;
89import uuid from 'uuid/v4' ;
910import './index.css' ;
@@ -26,6 +27,7 @@ const TODO_ADD = 'TODO_ADD';
2627const TODO_TOGGLE = 'TODO_TOGGLE' ;
2728const FILTER_SET = 'FILTER_SET' ;
2829const NOTIFICATION_HIDE = 'NOTIFICATION_HIDE' ;
30+ const TODO_ADD_WITH_NOTIFICATION = 'TODO_ADD_WITH_NOTIFICATION' ;
2931
3032// reducers
3133
@@ -128,13 +130,10 @@ function applyRemoveNotification(state, action) {
128130// action creators
129131
130132function 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+ } ;
138137}
139138
140139function doHideNotification ( id ) {
@@ -186,6 +185,20 @@ function getArrayOfObject(object) {
186185 return Object . keys ( object ) . map ( key => object [ key ] ) ;
187186}
188187
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+
189202// store
190203
191204const rootReducer = combineReducers ( {
@@ -195,13 +208,16 @@ const rootReducer = combineReducers({
195208} ) ;
196209
197210const logger = createLogger ( ) ;
211+ const saga = createSagaMiddleware ( ) ;
198212
199213const store = createStore (
200214 rootReducer ,
201215 undefined ,
202- applyMiddleware ( thunk , logger )
216+ applyMiddleware ( saga , logger )
203217) ;
204218
219+ saga . run ( watchAddTodoWithNotification ) ;
220+
205221// components
206222
207223function TodoApp ( ) {
0 commit comments