Skip to content

Instantly share code, notes, and snippets.

@hedgerh
Created March 25, 2018 04:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hedgerh/3ad730a7acc5b7542188597717a64cb5 to your computer and use it in GitHub Desktop.
Save hedgerh/3ad730a7acc5b7542188597717a64cb5 to your computer and use it in GitHub Desktop.
const initialState = {
1: {
todos: [],
visibilityFilter: 'SHOW_ALL'
},
2: {
todos: [],
visibilityFilter: 'SHOW_ALL'
},
3: {
todos: [],
visibilityFilter: 'SHOW_ALL'
}
}
const addTodo = (state, action) => {
const taskList = state[action.taskListId]
const { todos } = taskList
return {
...state,
[action.taskListId]: {
...taskList,
todos: [
...todos,
{
id: action.id,
text: action.text,
completed: false
}
]
}
}
}
const toggleTodo = (state, action) => {
const taskList = state[action.taskListId]
const { todos } = taskList
return {
...state,
[action.taskListId]: {
...taskList,
todos: todos.map(todo =>
(todo.id === action.id)
? {...todo, completed: !todo.completed}
: todo
)
}
}
}
const setVisibilityFilter = (state, action) => {
const taskList = state[action.taskListId]
return {
...state,
[action.taskListId]: {
...taskList,
visibilityFilter: action.filter
}
}
}
const todoList = (state = initialState, action) => {
const taskList = state[action.taskListId]
const { todos, visibilityFilter } = taskList
switch (action.type) {
case 'ADD_TODO':
return addTodo(state, action)
case 'TOGGLE_TODO':
return toggleTodo(state, action)
case 'SET_VISIBILITY_FILTER':
return setVisibilityFilter(state, action)
default:
return state
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment