Created
October 23, 2017 12:36
-
-
Save madx/d025d2a9cb8ed330806fde66a87a1e7a to your computer and use it in GitHub Desktop.
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
function makeAPIActions(entity) { | |
const path = ["entities", entity] | |
const EntityClass = Record({ | |
isRequesting: false, | |
data: null, | |
error: null, | |
}, `Record$${entity}`) | |
const defaults = new EntityClass() | |
const actions = { | |
request(state) { | |
return state.updateIn(path, e => e.set("isRequesting", true)) | |
}, | |
response(state, data) { | |
return state.setIn(path, defaults.set("data", data)) | |
}, | |
error(state, error) { | |
return state.setIn(path, defaults.set("error", error)) | |
}, | |
reset(state) { | |
return state.setIn(path, defaults) | |
}, | |
} | |
for (const actionName of Object.keys(actions)) { | |
actions[actionName].displayName = `${entity}:${actionName}` | |
} | |
return actions | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment