Created
April 21, 2016 13:21
-
-
Save calvinfroedge/7067c45402d52f2805ca99be986bbb9d to your computer and use it in GitHub Desktop.
Example reducer with redux-modifiers
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 { handleActions } from 'redux-actions' | |
import { INVOICE } from '../constants' | |
import { INVOICE_JSON } from '../data' | |
import { array, target, update } from 'redux-modifiers' | |
let obj = {}; | |
let { ADD, UPDATE, REMOVE, RECIPIENT_ADD, ITEM_REMOVE, ITEM_ADD } = INVOICE; | |
let pl = (value)=>{ | |
return {payload: value}; | |
} | |
obj[ADD] = array.add; | |
obj[UPDATE] = (state, action)=>{ | |
let { index, selected, value } = action.payload; | |
let item = target(...selected, update)(state[index], pl(value)); | |
return array.updateByIndex(state, pl({ index, item }) ); | |
}; | |
obj[RECIPIENT_ADD] = (state, action)=>{ | |
let { index } = action.payload; | |
let item = target('to', 'recipients', array.add)(state[index], pl(INVOICE_JSON().to.recipients[0]) ); | |
return array.updateByIndex(state, pl({ index, item }) ); | |
}; | |
obj[ITEM_ADD] = (state, action)=>{ | |
let { index, selected, value } = action.payload; | |
let item = target(...selected, array.add)( state[index], pl(INVOICE_JSON().items[0]) ); | |
return array.updateByIndex(state, pl({ index, item }) ); | |
}; | |
obj[ITEM_REMOVE] = (state, action)=>{ | |
let { index, selected } = action.payload; | |
let itemIndex = selected.pop(); | |
let item = target(...selected, array.removeAtIndex)(state[index], pl(itemIndex)); | |
return array.updateByIndex(state, pl({ index, item })); | |
}; | |
obj[REMOVE] = array.removeWhere; | |
export default handleActions(obj, []); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment