Created
July 20, 2016 16:59
-
-
Save hsunami/7cac560d2a8322fa71d2f0330b438988 to your computer and use it in GitHub Desktop.
TodoMVC Reducer Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReducer } from 'redux-act' | |
import Immutable from 'seamless-immutable' | |
import * as Actions from '../actions/todos' | |
const initialState = Immutable([ | |
{ | |
text: 'Use Redux', | |
completed: false, | |
id: 0 | |
} | |
]) | |
export default createReducer({ | |
[Actions.addTodo]: (state, text) => { | |
return Immutable([ | |
{ | |
id: state.reduce((maxId, todo) => Math.max(todo.id, maxId), -1) + 1, | |
completed: false, | |
text: text | |
}, | |
...state | |
]) | |
}, | |
[Actions.deleteTodo]: (state, id) => { | |
return state.filter(todo => todo.id !== id) | |
}, | |
[Actions.editTodo]: (state, { id, text }) => { | |
return state.map(todo => | |
todo.id === id | |
? todo.set('text', text) | |
: todo | |
) | |
}, | |
[Actions.completeTodo]: (state, id) => { | |
return state.map(todo => | |
todo.id === id | |
? todo.set('completed', !todo.completed) | |
: todo | |
) | |
}, | |
[Actions.completeAll]: (state) => { | |
const areAllMarked = state.every(todo => todo.completed) | |
return state.map(todo => todo.set('completed', !areAllMarked)) | |
}, | |
[Actions.clearCompleted]: (state) => state.filter(todo => todo.completed === false) | |
}, initialState) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment