1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import crud from './crud.fetch'
import { as_type } from './crud.types'
import { uploadAction } from './crud.upload'
/*
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.index({ module: 'samplernn' })
folderActions.show(12)
folderActions.create({ module: 'samplernn', name: 'foo' })
folderActions.update(12, { module: 'pix2pix' })
folderActions.destroy(12, { confirm: true })
*/
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') })
})
}
}
|