Actions

Data-fetching tier for Lore

Introduction

This functionality is provided by lore-hook-actions.

Actions are the data-fetching tier for Redux (or least that's what Lore uses them for). Any API communication that occurs will be done by an action, including creating, updating, deleting, and fetching data.

Before reading this documentation, it's recommended that you already understand what actions are, as this section will document the actions that are built into Lore, but it will not explain what actions are in general.

To learn more them at a foundational level, see the Actions and Action Creators documentation on the Redux website.

Action Blueprints

Lore automatically creates the set of default actions for every model you've defined, using the built in blueprints for create, destroy, get, find, and update.

If you have defined custom actions within the src/actions directory, it will also merge those actions into the defaults, overwriting any default actions that have the same name.

Example Usage

Let's say you create a tweet model, similar to the Quickstart:

src
|-models
  |-tweet.js

This hook will automatically create 5 actions associated with that model that cover common CRUD operations. The signatures for those actions look like this:

lore.actions.tweet.create
lore.actions.tweet.destroy
lore.actions.tweet.get
lore.actions.tweet.find
lore.actions.tweet.update

Example usage for each action is provided below:

create

This action is used to create a new tweet. The signature looks like create(params) and is invoked like this:

lore.actions.tweet.create({
  text: 'Some message to tweet'
});

destroy

This action is used to delete a new tweet. The signature looks like destroy(model) and is invoked like this:

const { tweet } = this.props;

lore.actions.tweet.destroy(tweet);

get

This action is used to fetch a specific tweet, and can optionally include query parameters to be sent in the request. The signature looks like get(id, query) and is invoked like this:

const tweetId = 1;
lore.actions.tweet.get(tweetId);

The second query argument is helpful when you need to provide additional query parameters to the API server such as when asking it to nest resources:

const tweetId = 1;

lore.actions.tweet.get(tweetId, {
  _embed: 'user'
});

find

This action is used to fetch a set of tweets. The signature looks like find(query, pagination) and is invoked like this:

var userId = this.props.params.userId;

lore.actions.tweet.find({
  user: userId
},{
  page: 1,
  pageSize: 20
});

update

This action is used to update a tweet. The signature looks like update(model, params) and is invoked like this:

var tweet = this.props.tweet;

lore.actions.tweet.update(tweet, {
  text: 'Modified tweet message'
});