Skip to content

Instantly share code, notes, and snippets.

@prenaudin
Last active June 9, 2016 09:13
Show Gist options
  • Save prenaudin/a267b47372565ade548b9f4955dc549a to your computer and use it in GitHub Desktop.
Save prenaudin/a267b47372565ade548b9f4955dc549a to your computer and use it in GitHub Desktop.
Immutable.Record + React + Redux = ❤ - 3
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