lore-extract-action

Extracts the action blueprint for a model to src/actions

destroy

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

Usage

lore extract action [model]/destroy

Example

lore extract action post/destroy
import _ from 'lodash';
import { ActionTypes, PayloadStates, normalize } from 'lore-utils';

/*
 * Blueprint for Destroy method
 */
export default function destroy(model) {
  return function(dispatch) {
    const Model = lore.models.post;
    const proxyModel = new Model(model.data);

    proxyModel.destroy().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(proxyModel);

      dispatch({
        type: ActionTypes.update('post'),
        payload: _.merge(model, {
          state: PayloadStates.DELETED
        })
      });

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

      if (response.status === 404) {
        dispatch({
          type: ActionTypes.update('post'),
          payload: _.merge(model, {
            state: PayloadStates.NOT_FOUND,
            error: error
          })
        });
      } else {
        dispatch({
          type: ActionTypes.update('post'),
          payload: _.merge(model, {
            state: PayloadStates.ERROR_DELETING,
            error: error
          })
        });
      }
    });

    return dispatch({
      type: ActionTypes.update('post'),
      payload: _.merge(model, {
        state: PayloadStates.DELETING
      })
    });
  };
};