lore-extract-action
Extracts the action blueprint for a model to src/actions
Extracts the action blueprint for a model to src/actions
CLI command to extract the get action for a given model into your project.
lore extract action [model]/get
lore extract action post/get
import _ from 'lodash';
import { ActionTypes, PayloadStates, payload, normalize } from 'lore-utils';
/*
* Blueprint for Get method
*/
export default function get(modelId, query = {}) {
return function(dispatch) {
const Model = lore.models.post;
const model = new Model({
id: modelId
});
model.fetch({
data: query
}).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;
if (response.status === 404) {
dispatch({
type: ActionTypes.update('post'),
payload: _.merge(payload(model), {
state: PayloadStates.NOT_FOUND,
error: error
})
});
} else {
dispatch({
type: ActionTypes.update('post'),
payload: payload(model, PayloadStates.ERROR_FETCHING, error)
});
}
});
return dispatch({
type: ActionTypes.add('post'),
payload: payload(model, PayloadStates.FETCHING)
});
};
};