Skip to content

Commit 66a0791

Browse files
committed
Update todo example to redux-act
1 parent 3269da9 commit 66a0791

File tree

6 files changed

+38
-89
lines changed

6 files changed

+38
-89
lines changed
Lines changed: 8 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,8 @@
1-
import * as types from '../constants/ActionTypes';
2-
3-
export function addTodo(text) {
4-
return {
5-
type: types.ADD_TODO,
6-
text
7-
};
8-
}
9-
10-
export function deleteTodo(id) {
11-
return {
12-
type: types.DELETE_TODO,
13-
id
14-
};
15-
}
16-
17-
export function editTodo(id, text) {
18-
return {
19-
type: types.EDIT_TODO,
20-
id,
21-
text
22-
};
23-
}
24-
25-
export function markTodo(id) {
26-
return {
27-
type: types.MARK_TODO,
28-
id
29-
};
30-
}
31-
32-
export function markAll() {
33-
return {
34-
type: types.MARK_ALL
35-
};
36-
}
37-
38-
export function clearMarked() {
39-
return {
40-
type: types.CLEAR_MARKED
41-
};
42-
}
1+
import { createAction } from 'redux-act';
2+
3+
export const addTodo = createAction('Add new todo');
4+
export const deleteTodo = createAction('Delete todo');
5+
export const editTodo = createAction('Edit todo', (id, text)=> ({id, text}));
6+
export const markTodo = createAction('Mark todo');
7+
export const markAll = createAction('Mark all todos');
8+
export const clearMarked = createAction('Clear all marked todos');
Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
export const ADD_TODO = 'ADD_TODO';
2-
export const DELETE_TODO = 'DELETE_TODO';
3-
export const EDIT_TODO = 'EDIT_TODO';
4-
export const MARK_TODO = 'MARK_TODO';
5-
export const MARK_ALL = 'MARK_ALL';
6-
export const CLEAR_MARKED = 'CLEAR_MARKED';
1+
// Remove all action constants
2+
// but keeping the file so it appears on the git diff
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// You can still keep constant for other purposes of course
12
export const SHOW_ALL = 'show_all';
23
export const SHOW_MARKED = 'show_marked';
34
export const SHOW_UNMARKED = 'show_unmarked';

examples/todomvc/containers/App.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { createStore, combineReducers, compose } from 'redux';
44
import { devTools, persistState } from 'redux-devtools';
55
import { DevTools, DebugPanel, LogMonitor } from 'redux-devtools/lib/react';
66
import { Provider } from 'react-redux';
7+
import { bindAll } from 'redux-act';
78
import * as reducers from '../reducers';
9+
import * as todoActions from '../actions/TodoActions';
810

911
const finalCreateStore = compose(
1012
devTools(),
@@ -15,6 +17,11 @@ const finalCreateStore = compose(
1517
const reducer = combineReducers(reducers);
1618
const store = finalCreateStore(reducer);
1719

20+
// Just to demonstrate it, we will auto-bind all actions
21+
// to the unique store but feel free to bind them inside
22+
// the components if you prefer so
23+
bindAll(todoActions, store);
24+
1825
export default class App extends Component {
1926
render() {
2027
return (

examples/todomvc/containers/TodoApp.js

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ import { connect } from 'react-redux';
33
import { bindActionCreators } from 'redux';
44
import Header from '../components/Header';
55
import MainSection from '../components/MainSection';
6-
import * as TodoActions from '../actions/TodoActions';
6+
import * as actions from '../actions/TodoActions';
77

88
class TodoApp extends Component {
99
render() {
10-
const { todos, actions } = this.props;
10+
const { todos } = this.props;
1111

1212
return (
1313
<div>
@@ -24,10 +24,7 @@ function mapState(state) {
2424
};
2525
}
2626

27-
function mapDispatch(dispatch) {
28-
return {
29-
actions: bindActionCreators(TodoActions, dispatch)
30-
};
31-
}
27+
// Since we already did bind all our actions to the store,
28+
// no need to do it again in each component
3229

33-
export default connect(mapState, mapDispatch)(TodoApp);
30+
export default connect(mapState)(TodoApp);

examples/todomvc/reducers/todos.js

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,32 @@
1-
import { ADD_TODO, DELETE_TODO, EDIT_TODO, MARK_TODO, MARK_ALL, CLEAR_MARKED } from '../constants/ActionTypes';
1+
import { createReducer } from 'redux-act';
2+
import * as actions from '../actions/TodoActions';
23

34
const initialState = [{
45
text: 'Use Redux',
56
marked: false,
67
id: 0
78
}];
89

9-
export default function todos(state = initialState, action) {
10-
switch (action.type) {
11-
case ADD_TODO:
12-
return [{
13-
id: (state.length === 0) ? 0 : state[0].id + 1,
14-
marked: false,
15-
text: action.text
16-
}, ...state];
10+
export default createReducer({
11+
[actions.addTodo]: (state, text)=> [{
12+
id: (state.length === 0) ? 0 : state[0].id + 1,
13+
marked: false,
14+
text: text
15+
}, ...state],
1716

18-
case DELETE_TODO:
19-
return state.filter(todo =>
20-
todo.id !== action.id
21-
);
17+
[actions.deleteTodo]: (state, id)=> state.filter(todo=> todo.id !== id),
2218

23-
case EDIT_TODO:
24-
return state.map(todo =>
25-
todo.id === action.id ?
26-
{ ...todo, text: action.text } :
27-
todo
28-
);
19+
[actions.editTodo]: (state, todo)=> state.map(t=> todo.id === t.id ? {...t, text: todo.text} : t),
2920

30-
case MARK_TODO:
31-
return state.map(todo =>
32-
todo.id === action.id ?
33-
{ ...todo, marked: !todo.marked } :
34-
todo
35-
);
21+
[actions.markTodo]: (state, id)=> state.map(todo=> todo.id === id ? {...todo, marked: !todo.marked} : todo),
3622

37-
case MARK_ALL:
23+
[actions.markAll]: (state)=> {
3824
const areAllMarked = state.every(todo => todo.marked);
3925
return state.map(todo => ({
4026
...todo,
4127
marked: !areAllMarked
4228
}));
29+
},
4330

44-
case CLEAR_MARKED:
45-
return state.filter(todo => todo.marked === false);
46-
47-
default:
48-
return state;
49-
}
50-
}
31+
[actions.clearMarked]: (state)=> state.filter(todo => todo.marked === false)
32+
}, initialState);

0 commit comments

Comments
 (0)