Skip to content

Commit 7618044

Browse files
committed
exchange redux thunk with redux saga
1 parent 18962bb commit 7618044

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"react-redux": "^5.0.5",
1313
"redux": "^3.6.0",
1414
"redux-logger": "^3.0.6",
15-
"redux-thunk": "^2.2.0",
15+
"redux-saga": "^0.15.3",
1616
"uuid": "^3.0.1"
1717
},
1818
"scripts": {

src/index.js

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import ReactDOM from 'react-dom';
33
import { applyMiddleware, combineReducers, createStore } from 'redux';
44
import { Provider, connect } from 'react-redux';
55
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';
78
import { schema, normalize } from 'normalizr';
89
import uuid from 'uuid/v4';
910
import './index.css';
@@ -26,6 +27,7 @@ const TODO_ADD = 'TODO_ADD';
2627
const TODO_TOGGLE = 'TODO_TOGGLE';
2728
const FILTER_SET = 'FILTER_SET';
2829
const 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

130132
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+
};
138137
}
139138

140139
function 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

191204
const rootReducer = combineReducers({
@@ -195,13 +208,16 @@ const rootReducer = combineReducers({
195208
});
196209

197210
const logger = createLogger();
211+
const saga = createSagaMiddleware();
198212

199213
const store = createStore(
200214
rootReducer,
201215
undefined,
202-
applyMiddleware(thunk, logger)
216+
applyMiddleware(saga, logger)
203217
);
204218

219+
saga.run(watchAddTodoWithNotification);
220+
205221
// components
206222

207223
function TodoApp() {

0 commit comments

Comments
 (0)