Skip to content

Instantly share code, notes, and snippets.

@ryo33
Created May 27, 2016 09:38
Show Gist options
  • Save ryo33/aab63c300556616d2514f6541d4d9a0f to your computer and use it in GitHub Desktop.
Save ryo33/aab63c300556616d2514f6541d4d9a0f to your computer and use it in GitHub Desktop.
// State
{
todos: {
todoA: [],
todoB: [],
todoC: []
},
currentTodoKey: "todoA"
}
// Reducer
const reducer = (state = { todos: {}, currentTodoKey: "default" }, action) => {
const addTodo = (state, key, todo) {
return Object.assign({}, state, {
todos: Object.assign({}, state.todos, {
[key]: (state.todos[key] || []).concat(todo)
})
})
}
switch (action.type) {
case 'ADD_TODO':
var { key, todo } = action.payload
return addTodo(state, key, todo)
case 'ADD_TODO_TO_CURRENT_KEY':
var { todo } = action.payload
return addTodo(state, state.currentTodoKey, todo)
case 'UPDATE_CURRENT_KEY':
var { newKey } = action.payload
return Object.assign({}, state, {
currentTodoKey: newKey
})
default:
return state
}
}
const todos = (state = {}, action, entireState) => {
switch (action.type) {
case 'ADD_TODO':
var { key, todo } = action.payload
return Object.assign({}, state, {
[key]: (state[key] || []).concat(todo)
})
case 'ADD_TODO_TO_CURRENT_KEY':
var { todo } = action.payload
var key = entireState.currentTodoKey
return Object.assign({}, state, {
[key]: (state[key] || []).concat(todo)
})
default:
return state
}
}
const currentTodoKey = (state = "default", action) => {
switch (action.type) {
case 'UPDATE_CURRENT_KEY':
var { newKey } = action.payload
return newKey
default:
return state
}
}
const reducer = (state, action) => ({
todos: todos(state.todos, action, state),
currentTodoKey: currentTodoKey(state.currentTodoKey, action)
})
const reducer = (state, action) => ({
todos: todos(state.todos, action, state),
currentTodoKey: currentTodoKey(state.currentTodoKey, action)
})
import combineSectionReducers from 'combine-section-reducers'
const reducer = combineSectionReducers({
todo, currentTodoKey
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment