Skip to content

Instantly share code, notes, and snippets.

@madx
Created October 23, 2017 12:36
Show Gist options
  • Save madx/d025d2a9cb8ed330806fde66a87a1e7a to your computer and use it in GitHub Desktop.
Save madx/d025d2a9cb8ed330806fde66a87a1e7a to your computer and use it in GitHub Desktop.
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