Last active
August 30, 2016 19:52
-
-
Save piq9117/32be179341ee7eb266b7202794a65633 to your computer and use it in GitHub Desktop.
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
// addProps :: {k: v} -> {k: v} | |
const addProps = props => Object.assign({}, {iceCreamFlavor: '', favoriteFood: []}, props) | |
function addFood (state, action) { | |
// food :: Food | |
const food = addProps(Object.assign({}, state[action.data.id], { | |
[action.data.prop]: state[action.data.id].favoriteFood.concat(action.data.food) | |
})) | |
// assocPath :: [String] -> {k: v} -> {k: v} -> {k: v} | |
return R.assocPath([action.data.id])(food)(state) | |
} | |
function iceCreamFlavor (state, action) { | |
// flavor :: Flavor | |
const flavor = addProps(Object.assign({}, state[action.data.id], { | |
[action.data.prop]: action.data.flavor | |
})) | |
// assocPath :: [String] -> {k: v} -> {k: v} -> {k: v} | |
return R.assocPath([action.data.id])(flavor)(state) | |
} | |
export default function reducer (state = devs, action) { | |
switch (action.type) { | |
case 'ADD_FOOD': | |
return addFood(state, action) | |
case 'ICE_CREAM_FLAVOR': | |
return iceCreamFlavor(state, action) | |
default: | |
return state | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment