Last active
June 9, 2016 09:13
-
-
Save prenaudin/a267b47372565ade548b9f4955dc549a to your computer and use it in GitHub Desktop.
Immutable.Record + React + Redux = ❤ - 3
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 { RECEIVED_ENTITIES, DELETE_TASK, TOGGLE_TASK_DONE } from 'constants/ActionsTypes'; | |
import Task from 'models/Task'; | |
import TaskMap from 'models/TaskMap'; | |
const initialState = new TaskMap(); | |
const mergeEntities = (state, newTasks) => | |
state.merge(newTasks.map((task) => new Task(task))) | |
export default (state = initialState, action) => | |
switch(action.type) { | |
// Received tasks data from an external API | |
case RECEIVED_ENTITIES: | |
if (!action.entities.tasks) { return state } | |
return mergeEntities(state, Immutable.fromJS(action.entities.tasks)); | |
// Handles some UI actions | |
case DELETE_TASK: | |
return state.remove(action.taskId); | |
case TOGGLE_TASK_DONE: | |
return state.update(action.taskId, (task) => task.set('done', !task.get('done'))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment