Actions
Data-fetching tier for Lore
Data-fetching tier for Lore
This blueprint...
Example usage is below:
lore.actions.tweet.create({
text: ''
})
import { ActionTypes, PayloadStates, payload, normalize } from 'lore-utils';
export default function create(params) {
return function(dispatch) {
const Model = lore.models.{{ modelName }};
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, '{{ modelName }}').model(model);
dispatch({
type: ActionTypes.update('{{ modelName }}'),
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('{{ modelName }}'),
payload: payload(model, PayloadStates.ERROR_CREATING, error)
});
});
return dispatch({
type: ActionTypes.add('{{ modelName }}'),
payload: payload(model, PayloadStates.CREATING)
});
};
};