Created
March 25, 2018 04:43
-
-
Save hedgerh/3ad730a7acc5b7542188597717a64cb5 to your computer and use it in GitHub Desktop.
This file contains 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
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