diff options
| author | Jules Laplace <julescarbon@gmail.com> | 2018-05-29 01:48:20 +0200 |
|---|---|---|
| committer | Jules Laplace <julescarbon@gmail.com> | 2018-05-29 01:48:20 +0200 |
| commit | 0296adc3ace0e36b92a56ec3a01a933b9bbd2e99 (patch) | |
| tree | a66276faa5e14124f3d29070fe5befbb80d82ead /app/client/api/crud.actions.js | |
| parent | f82d95c5f5b589bec0fc503c86f9e644b07b4ddd (diff) | |
write a buncha crud code
Diffstat (limited to 'app/client/api/crud.actions.js')
| -rw-r--r-- | app/client/api/crud.actions.js | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/app/client/api/crud.actions.js b/app/client/api/crud.actions.js new file mode 100644 index 0000000..ca51dbb --- /dev/null +++ b/app/client/api/crud.actions.js @@ -0,0 +1,35 @@ +import crud from '../../api/crud.fetch' +import { as_type } from './crud.types' + +/* +for our crud events, create corresponding actions +the actions fire a 'loading' event, call the underlying api method, and then resolve. +so you can do ... + var folderActions = crud_actions('folder') + folderActions('upload', (dispatch) => { + new Promise((resolve, reject) => { + fetch(...) + .then(() => resolve(data)) + .catch(e => throw e) + }) + }) + folderActions.index({ module: 'samplernn' }) +*/ + +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, 'index', () => fetch_type[type](param)]), + return lookup + }, { action: (method, fn) => crud_action(type, method, fn) }) +} + +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') }) + }) + } +} |
