Created
          April 21, 2016 14:02 
        
      - 
      
- 
        Save calvinfroedge/69a7c36a68a9fdea1c82c280f9dfeaf2 to your computer and use it in GitHub Desktop. 
    Reducer with immutablejs
  
        
  
    
      This file contains hidden or 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 { handleActions } from 'redux-actions' | |
| import { INVOICE } from '../constants' | |
| import { INVOICE_JSON } from '../data' | |
| import Immutable from 'immutable' | |
| let obj = {}; | |
| let { ADD, UPDATE, REMOVE, RECIPIENT_ADD, ITEM_REMOVE, ITEM_ADD } = INVOICE; | |
| obj[ADD] = (state, action)=>{ | |
| return state.push(Immutable.fromJS(action.payload)); | |
| }; | |
| obj[UPDATE] = (state, action)=>{ | |
| let { index, selected, value } = action.payload; | |
| selected.unshift(index); | |
| return state.updateIn(selected, val => value); | |
| }; | |
| obj[RECIPIENT_ADD] = (state, action)=>{ | |
| let { index } = action.payload; | |
| return state.updateIn([index, 'to', 'recipients'], val => val.push(INVOICE_JSON.to.recipients[0])) | |
| }; | |
| obj[ITEM_ADD] = (state, action)=>{ | |
| let { index, selected, value } = action.payload; | |
| selected.unshift(index); | |
| return state.updateIn(selected, val => val.push(INVOICE_JSON.items[0])); | |
| }; | |
| obj[ITEM_REMOVE] = (state, action)=>{ | |
| let { index, selected } = action.payload; | |
| selected.unshift(index); | |
| return state.removeIn(selected); | |
| }; | |
| obj[REMOVE] = (state, action)=>{ | |
| return state.remove(action.payload); | |
| } | |
| export default handleActions(obj, Immutable.fromJS([])); | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment