summaryrefslogtreecommitdiff
path: root/app/client/api
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/api
parentb0440c213ac9f97e558b3d15d3740dab234b7b79 (diff)
omg making an api request
Diffstat (limited to 'app/client/api')
-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
5 files changed, 46 insertions, 55 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, {})