import crud from './crud.fetch' import { as_type } from './crud.types' import { uploadAction } from './crud.upload' export function crud_actions(type) { const fetch_type = crud_fetch(type) return [ 'index', 'show', 'create', 'update', 'destroy' ].reduce((lookup, param) => { lookup[param] = crud_action(type, param, () => fetch_type[type](param)]), return lookup }, { action: (method, fn) => crud_action(type, method, fn) upload: (id, fd) => uploadAction(type, id, fd) }) } export const crud_action = (type, method, fn) => dispatch => { dispatch({ type: as_type(type, method + '_loading') }) fn(dispatch).then(data => { dispatch({ type: as_type(type, method), data }) }).catch(e => { dispatch({ type: as_type(type, method + '_error') }) }) } }