Skip to content

Instantly share code, notes, and snippets.

@hsunami
Created July 20, 2016 16:59
Show Gist options
  • Save hsunami/7cac560d2a8322fa71d2f0330b438988 to your computer and use it in GitHub Desktop.
Save hsunami/7cac560d2a8322fa71d2f0330b438988 to your computer and use it in GitHub Desktop.
TodoMVC Reducer Example
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