Skip to content

Commit f3b1bdd

Browse files
committed
Async dispatch using redux-thunk middleware
1 parent 12cb8d5 commit f3b1bdd

4 files changed

Lines changed: 48 additions & 9 deletions

File tree

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
"react-redux": "^4.0.0",
2424
"react-router": "^0.13.5",
2525
"redux": "^3.0.4",
26+
"redux-logger": "^2.0.4",
2627
"redux-thunk": "^1.0.0"
2728
},
2829
"devDependencies": {

web/static/js/actions.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
* action types
33
*/
44

5-
export const ADD_TODO = 'ADD_TODO';
5+
export const ADD_TODO_REQUEST = 'ADD_TODO_REQUEST';
6+
export const ADD_TODO_SUCCESS = 'ADD_TODO_SUCCESS';
7+
export const ADD_TODO_FAILURE = 'ADD_TODO_FAILURE';
8+
69
export const COMPLETE_TODO = 'COMPLETE_TODO';
710
export const SET_VISIBILITY_FILTER = 'SET_VISIBILITY_FILTER';
811

@@ -20,14 +23,30 @@ export const VisibilityFilters = {
2023
* action creators
2124
*/
2225

26+
function addTodoRequest(text) {
27+
return { type: ADD_TODO_REQUEST, text };
28+
}
29+
30+
function addTodoSuccess(text) {
31+
return { type: ADD_TODO_SUCCESS, text };
32+
}
33+
34+
function addTodoFailure(text) {
35+
return { type: ADD_TODO_FAILURE, text };
36+
}
37+
2338
export function addTodo(text) {
24-
return { type: ADD_TODO, text }
39+
return dispatch => {
40+
dispatch(addTodoRequest(text));
41+
dispatch(addTodoSuccess(text));
42+
};
2543
}
2644

45+
2746
export function completeTodo(index) {
28-
return { type: COMPLETE_TODO, index }
47+
return { type: COMPLETE_TODO, index };
2948
}
3049

3150
export function setVisibilityFilter(filter) {
32-
return { type: SET_VISIBILITY_FILTER, filter }
51+
return { type: SET_VISIBILITY_FILTER, filter };
3352
}

web/static/js/index.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
11
import React from 'react';
22
import { render } from 'react-dom';
3-
import { createStore } from 'redux'
4-
import { Provider } from 'react-redux'
3+
import thunkMiddleware from 'redux-thunk';
4+
import createLogger from 'redux-logger';
5+
import { createStore, applyMiddleware } from 'redux';
6+
import { Provider } from 'react-redux';
57
import App from './containers/App';
68
import todoApp from './reducers';
79

8-
let store = createStore(todoApp);
10+
const loggerMiddleware = createLogger();
11+
12+
const createStoreWithMiddleware = applyMiddleware(
13+
thunkMiddleware, // lets us dispatch() functions
14+
loggerMiddleware // neat middleware that logs actions
15+
)(createStore);
16+
17+
const store = createStoreWithMiddleware(todoApp);
918

1019
render(
1120
<Provider store={store}>

web/static/js/reducers.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,35 @@
11
import { combineReducers } from 'redux';
2-
import { ADD_TODO, COMPLETE_TODO, SET_VISIBILITY_FILTER, VisibilityFilters } from './actions';
2+
import { ADD_TODO_REQUEST, ADD_TODO_SUCCESS, ADD_TODO_FAILURE, COMPLETE_TODO, SET_VISIBILITY_FILTER, VisibilityFilters } from './actions';
33
const { SHOW_ALL } = VisibilityFilters;
44

55
function visibilityFilter(state = SHOW_ALL, action) {
66
switch (action.type) {
77
case SET_VISIBILITY_FILTER:
88
return action.filter;
9+
910
default:
1011
return state;
1112
}
1213
}
1314

1415
function todos(state = [], action) {
1516
switch (action.type) {
16-
case ADD_TODO:
17+
case ADD_TODO_REQUEST:
1718
return [
1819
...state,
1920
{
2021
text: action.text,
2122
completed: false
2223
}
2324
];
25+
case ADD_TODO_SUCCESS:
26+
console.log('ADD_TODO_SUCCESS');
27+
return state;
28+
29+
case ADD_TODO_FAILURE:
30+
console.error('ADD_TODO_FAILURE');
31+
return state;
32+
2433
case COMPLETE_TODO:
2534
return [
2635
...state.slice(0, action.index),
@@ -29,6 +38,7 @@ function todos(state = [], action) {
2938
}),
3039
...state.slice(action.index + 1)
3140
];
41+
3242
default:
3343
return state;
3444
}

0 commit comments

Comments
 (0)