Created
June 8, 2016 16:01
-
-
Save callmephilip/ea4d6b339af1bef5e52a30f667710890 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
import { fromJS } from 'immutable'; | |
import { TOGGLE_TASK_COMPLETION, ADD_TASK, DELETE_TASK } from './constants'; | |
const initialState = fromJS([ | |
{ text:'Buy milk', id: '1', isComplete: false }, | |
{ text:'Drink milk', id: '2', isComplete: false }, | |
{ text:'Repeat', id:'3', isComplete: false } | |
]); | |
function todos(state = initialState, action) { | |
switch (action.type) { | |
case TOGGLE_TASK_COMPLETION: | |
const itemIndex = state.findIndex( i => i.get('id') === action.payload.id); | |
if (itemIndex !== -1) { | |
return state.update(itemIndex, (i) => i.updateIn(['isComplete'], ic => !ic)); | |
} | |
return state; | |
case ADD_TASK: | |
return state.push(fromJS({ | |
text: action.payload.text, | |
id: new Date().getTime().toString(), | |
isComplete: false | |
})); | |
case DELETE_TASK: | |
return state.filter(todo => todo.get('id') !== action.payload.id); | |
default: | |
return state; | |
} | |
} | |
export default todos; | |
export function selectAllTodos(state) { | |
return state.get('todos'); | |
} | |
export function selectCompletedTodos(state) { | |
return state.get('todos').filter(todo => todo.get('isComplete')); | |
} | |
export function selectActiveTodos(state) { | |
return state.get('todos').filter(todo => !todo.get('isComplete')); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment