summaryrefslogtreecommitdiff
path: root/app/client
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2018-05-29 02:44:25 +0200
committerJules Laplace <julescarbon@gmail.com>2018-05-29 02:44:25 +0200
commitf5c04fc92a4e23948b477f4f579b953e8edd6bb2 (patch)
tree2a7de103dfde225b6ba90f70ba20d8b1c1d951bc /app/client
parentb0440c213ac9f97e558b3d15d3740dab234b7b79 (diff)
omg making an api request
Diffstat (limited to 'app/client')
-rw-r--r--app/client/api/crud.actions.js27
-rw-r--r--app/client/api/crud.type.js30
-rw-r--r--app/client/api/crud.types.js27
-rw-r--r--app/client/api/crud.upload.js10
-rw-r--r--app/client/api/index.js7
-rw-r--r--app/client/modules/samplernn/datasets.component.js13
-rw-r--r--app/client/modules/samplernn/samplernn.reducer.js3
-rw-r--r--app/client/types.js2
8 files changed, 62 insertions, 57 deletions
diff --git a/app/client/api/crud.actions.js b/app/client/api/crud.actions.js
index 583a9cf..1fcae81 100644
--- a/app/client/api/crud.actions.js
+++ b/app/client/api/crud.actions.js
@@ -1,6 +1,6 @@
-import crud from './crud.fetch'
+import { crud_fetch } from './crud.fetch'
import { as_type } from './crud.types'
-import { uploadAction } from './crud.upload'
+import { upload_action } from './crud.upload'
export function crud_actions(type) {
const fetch_type = crud_fetch(type)
@@ -9,22 +9,21 @@ export function crud_actions(type) {
'show',
'create',
'update',
- 'destroy'
+ 'destroy',
].reduce((lookup, param) => {
- lookup[param] = crud_action(type, param, () => fetch_type[type](param)]),
+ lookup[param] = crud_action(type, param, (q) => fetch_type[param](q))
return lookup
}, {
- action: (method, fn) => crud_action(type, method, fn)
- upload: (id, fd) => uploadAction(type, id, fd)
+ action: (method, fn) => crud_action(type, method, fn),
+ upload: (id, fd) => upload_action(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') })
- })
- }
+export const crud_action = (type, method, fn) => q => dispatch => {
+ dispatch({ type: as_type(type, method + '_loading') })
+ fn(q).then(data => {
+ dispatch({ type: as_type(type, method), data })
+ }).catch(e => {
+ dispatch({ type: as_type(type, method + '_error') })
+ })
}
diff --git a/app/client/api/crud.type.js b/app/client/api/crud.type.js
deleted file mode 100644
index 6c4a2f4..0000000
--- a/app/client/api/crud.type.js
+++ /dev/null
@@ -1,30 +0,0 @@
-
-export const as_type = (a, b) => [a, b].join('_').toUpperCase()
-
-export function crud_type(type, actions=[]){
- return actions
- .concat([
- 'index_loading',
- 'index',
- 'index_error',
- 'show_loading',
- 'show',
- 'show_error',
- 'create_loading',
- 'create',
- 'create_error',
- 'update_loading',
- 'update',
- 'update_error',
- 'destroy_loading',
- 'destroy',
- 'destroy_error',
- 'upload_loading',
- 'upload_progress',
- 'upload_complete',
- 'upload_error',
- 'sort',
- ])
- .reduce((a, b) => (a[b] = as_type(type, b)) && a, {})
- return aa
-}
diff --git a/app/client/api/crud.types.js b/app/client/api/crud.types.js
new file mode 100644
index 0000000..b7efdb8
--- /dev/null
+++ b/app/client/api/crud.types.js
@@ -0,0 +1,27 @@
+
+export const as_type = (a, b) => [a, b].join('_').toUpperCase()
+
+export const crud_type = (type, actions=[]) =>
+ actions.concat([
+ 'index_loading',
+ 'index',
+ 'index_error',
+ 'show_loading',
+ 'show',
+ 'show_error',
+ 'create_loading',
+ 'create',
+ 'create_error',
+ 'update_loading',
+ 'update',
+ 'update_error',
+ 'destroy_loading',
+ 'destroy',
+ 'destroy_error',
+ 'upload_loading',
+ 'upload_progress',
+ 'upload_complete',
+ 'upload_error',
+ 'sort',
+ ])
+ .reduce((a, b) => (a[b] = as_type(type, b)) && a, {})
diff --git a/app/client/api/crud.upload.js b/app/client/api/crud.upload.js
index 26917ff..f680a74 100644
--- a/app/client/api/crud.upload.js
+++ b/app/client/api/crud.upload.js
@@ -1,6 +1,6 @@
import { as_type } from './crud.types'
-export function crud_upload(type, id, fd, dispatch) => {
+export function crud_upload(type, id, fd, dispatch) {
return new Promise( (resolve, reject) => {
const xhr = new XMLHttpRequest()
xhr.upload.addEventListener("progress", uploadProgress, false)
@@ -42,7 +42,7 @@ export function crud_upload(type, id, fd, dispatch) => {
}
dispatch && dispatch({
type: as_type(type, 'upload_complete'),
- data
+ data,
[type]: id,
})
}
@@ -58,13 +58,11 @@ export function crud_upload(type, id, fd, dispatch) => {
uploadCancelled = function (evt) {
dispatch && dispatch({
type: as_type(type, 'upload_error'),
- error: 'upload cancelled'
+ error: 'upload cancelled',
[type]: id,
})
}
})
}
-export function uploadAction(type, id, fd) {
- return dispatch => crud_upload(type, id, fd, dispatch)
-} \ No newline at end of file
+export const upload_action = (type, id, fd) => dispatch => crud_upload(type, id, fd, dispatch)
diff --git a/app/client/api/index.js b/app/client/api/index.js
index 6f12227..82cd364 100644
--- a/app/client/api/index.js
+++ b/app/client/api/index.js
@@ -1,6 +1,3 @@
-import FormData from 'form-data'
-import fetch from 'node-fetch'
-
import { crud_actions } from './crud.actions'
/*
@@ -16,10 +13,10 @@ so you can do ...
folderActions.upload(12, form_data)
*/
-export default [
+export const actions = [
'folder',
'file',
'dataset',
'task',
'user',
-].reduce((a,b) => (a[b + 'Actions'] = crud_actions(b)) && a, {})
+].reduce((a,b) => (a[b] = crud_actions(b)) && a, {})
diff --git a/app/client/modules/samplernn/datasets.component.js b/app/client/modules/samplernn/datasets.component.js
index 56024e7..05f9cc7 100644
--- a/app/client/modules/samplernn/datasets.component.js
+++ b/app/client/modules/samplernn/datasets.component.js
@@ -2,6 +2,14 @@ import { h, Component } from 'preact'
import { bindActionCreators } from 'redux'
import { connect } from 'react-redux'
+import { actions } from '../../api'
+ // folderActions.index({ module: 'samplernn' })
+ // folderActions.show(12)
+ // folderActions.create({ module: 'samplernn', name: 'foo' })
+ // folderActions.update(12, { module: 'pix2pix' })
+ // folderActions.destroy(12, { confirm: true })
+ // folderActions.upload(12, form_data)
+
import Group from '../../common/group.component'
import Slider from '../../common/slider.component'
import Select from '../../common/select.component'
@@ -15,6 +23,7 @@ class SampleRNNDatasets extends Component {
// fetch file list
this.handleUpload = this.handleUpload.bind(this)
this.handleURL = this.handleURL.bind(this)
+ props.actions.folder.index({ module: 'samplernn' })
}
handleUpload(file) {
@@ -63,7 +72,9 @@ const mapStateToProps = state => ({
})
const mapDispatchToProps = (dispatch, ownProps) => ({
- // actions: bindActionCreators(liveActions, dispatch)
+ actions: {
+ folder: bindActionCreators(actions.folder, dispatch)
+ }
})
export default connect(mapStateToProps, mapDispatchToProps)(SampleRNNDatasets)
diff --git a/app/client/modules/samplernn/samplernn.reducer.js b/app/client/modules/samplernn/samplernn.reducer.js
index a642919..5424422 100644
--- a/app/client/modules/samplernn/samplernn.reducer.js
+++ b/app/client/modules/samplernn/samplernn.reducer.js
@@ -24,6 +24,9 @@ const samplernnReducer = (state = samplernnInitialState, action) => {
return {
...state,
}
+ case types.folder.index:
+ console.log(action)
+ return
default:
return state
}
diff --git a/app/client/types.js b/app/client/types.js
index e43b2b2..c8fa26f 100644
--- a/app/client/types.js
+++ b/app/client/types.js
@@ -1,4 +1,4 @@
-import { crud_type } from './api/crud.type'
+import { crud_type } from './api/crud.types'
export default {
system: {