Actions

Data-fetching tier for Lore

Overriding Actions

Actions are responsible for server communicating and emitting the proper events to reducers to enable them to accurately keep track of the applications state.

When you create a model in Lore, the framework automatically creates a set of actions for finding, creating, updating, deleting, and getting that resource.

The blueprints have an implicit file structure that looks like this:

|-src
  |-actions
    |-post
      |-create.js
      |-destroy.js
      |-find.js
      |-get.js
      |-update.js
  |-models
    |-post.js

All blueprints actions can be overridden by creating a file that matches the location of the blueprint. For example, if you wanted to override the create blueprint for a post model, you would create your own action at actions/post/create.js.

|-src
  |-actions
    |-post
      |-create.js
  |-models
    |-post.js

There are two ways to override an action.

Override Option #1: Template

If your file contains an object, Lore will assume you just want to tailor (or tap into) the existing blueprint behavior, but you don't want to change the underlying implementation. This is useful if (for some reason) you wanted to turn off the default optimistic update behavior.

This approach is also useful if you want to listen and log errors, or display them to the user using a snackbar or toast. This can be achieved by defining a beforeDispatch function, example below.

At the moment, the beforeDispatch property only exists on the error path.

module.exports = {

  optimistic: {
    actionType: ActionTypes.ADD_POST,
    payloadState: PayloadStates.CREATING
  },

  onSuccess: {
    actionType: ActionTypes.UPDATE_POST,
    payloadState: PayloadStates.RESOLVED
  },

  onError: {
    actionType: ActionTypes.REMOVE_POST,
    payloadState: PayloadStates.ERROR_CREATING,
    beforeDispatch: function(response, args) {
      console.log({
        message: "Post could not be created",
        response: response
      });
    }
  }
};

Override Option #2: Full Replacement

If your file contains a function, Lore will assume you want to replace the blueprint with your own implementation. While the need to do this should be rare, this would be necessary if (for example) you needed to make multiple API calls to complete an operation, or if the API was forcing business logic into the client (bad) and if a request failed you needed to make an API call to rollback changes or something. For well behaved/designed and convention-abiding APIs, the need to overwrite the default blueprint actions completely should be fairly uncommon.

module.exports = function create( params ) {
  return function( dispatch ) {
    var post = new Post(params);

    post.save().then(function() {
      dispatch({
        type: ActionTypes.UPDATE_POST,
        payload: payload(model, PayloadStates.RESOLVED)
      });
    }).catch(function( response ) {
      var error = response.responseJSON;
      dispatch({
        type: ActionTypes.UPDATE_POST,
        payload: payload(model, PayloadStates.ERROR_CREATING, error)
      });
    });

    return dispatch({
      type: ActionTypes.ADD_POST,
      payload: payload(model, PayloadStates.CREATING)
    });
  };
};

Override Option #3: Redefine the Blueprints

While not an option at the moment, Lore will be providing the ability to define your own blueprints, in case you want (or need) to overwrite the default behavior across all models.

Extracting the Existing Actions

If you're ever curious what the blueprints for Lore look like, or need a quick way to modify a specific action's behavior, Lore provides a CLI command that will provide a model-specific copy of the blueprints for you to debug and/or modify.

To extract the actions for the post model in our example above, run this command:

lore extract action post

You can learn more about the extract command in the CLI documentation.