lore-extract-action

Extracts the action blueprint for a model to src/actions

create

CLI command to extract the create action for a given model into your project.

Usage

lore extract action [model]/create

Example

lore extract action post/create
import { ActionTypes, PayloadStates, payload, normalize } from 'lore-utils';

/*
 * Blueprint for Create method
 */
export default function create(params) {
  return function(dispatch) {
    const Model = lore.models.post;
    const model = new Model(params);

    model.save().then(function() {
      // look through the model and generate actions for any attributes with
      // nested data that should be normalized
      const actions = normalize(lore, 'post').model(model);

      dispatch({
        type: ActionTypes.update('post'),
        payload: payload(model, PayloadStates.RESOLVED)
      });

      // dispatch any actions created from normalizing nested data
      actions.forEach(dispatch);
    }).catch(function(response) {
      const error = response.data;

      dispatch({
        type: ActionTypes.remove('post'),
        payload: payload(model, PayloadStates.ERROR_CREATING, error)
      });
    });

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