Skip to content

Resource Config

Michael Niday edited this page Jun 6, 2018 · 1 revision

Action Definitions

Base actions can be utilized for setting up the primary CRUD actions.

The fetch action is used by default when wrapping a component with the entities HOC to autoload the resource

const baseActions = {
  list: { method: 'get' },
  create: { method: 'post' },
  fetch: { method: 'get' },
  update: { method: 'patch' },
  destroy: { method: 'delete' }
}

You can use these in a resource config which should be shaped like the following per each resource type Here is an example that allows for leveraging the base actions, plus some individual actions for this resource

...
task: {
  endpoint: 'tasks',
  actions: {
    ...baseActions,
    setActive: { method: 'post', path: 'activate' },
    setInactive: { method: 'post', path: 'deactivate' }
  }
}
...

Tying this together with performAction, this resource configuration would allow for the following:

// Where `tasks` is the name of the collection set up in `entities({tasks: {type: 'task'}})(Component)`
this.props.tasks.performAction('setActive', {
  itemId: 'task_1' // the ID of the task we are modifying
})

This results in a POST request to the baseApiUrl/tasks/task_1/activate

Clone this wiki locally