summaryrefslogtreecommitdiff
path: root/frontend/api/crud.upload.js
diff options
context:
space:
mode:
authorJules Laplace <julescarbon@gmail.com>2020-09-26 14:56:02 +0200
committerJules Laplace <julescarbon@gmail.com>2020-09-26 14:56:02 +0200
commita17b76ac75f506f5da6fe8adf9c36632b60d4226 (patch)
treeabb0af0c4409b830dea2ef808c146223ee973933 /frontend/api/crud.upload.js
parent2231a6e1c05b07bb7ec5906716aedec93d02429c (diff)
refactor to use app-rooted js imports
Diffstat (limited to 'frontend/api/crud.upload.js')
-rw-r--r--frontend/api/crud.upload.js107
1 files changed, 0 insertions, 107 deletions
diff --git a/frontend/api/crud.upload.js b/frontend/api/crud.upload.js
deleted file mode 100644
index 8a711c7..0000000
--- a/frontend/api/crud.upload.js
+++ /dev/null
@@ -1,107 +0,0 @@
-import { as_type } from './crud.types'
-
-export function crud_upload(type, data, dispatch) {
- return new Promise( (resolve, reject) => {
- // console.log(type, data)
- const { id } = data
-
- const fd = new FormData()
-
- Object.keys(data).forEach(key => {
- if (key !== 'id') {
- fd.append(key, data[key])
- }
- })
-
- let url = id ? '/api/v1/' + type + '/' + id + '/'
- : '/api/v1/' + type + '/'
- // console.log(url)
-
- const xhr = new XMLHttpRequest()
- xhr.upload.addEventListener("progress", uploadProgress, false)
- xhr.addEventListener("load", uploadComplete, false)
- xhr.addEventListener("error", uploadFailed, false)
- xhr.addEventListener("abort", uploadCancelled, false)
- xhr.open("POST", url)
- xhr.send(fd)
-
- dispatch && dispatch({ type: as_type(type, 'upload_loading')})
-
- let complete = false
-
- function uploadProgress (e) {
- if (e.lengthComputable) {
- const percent = Math.round(e.loaded * 100 / e.total) || 0
- if (percent > 99) {
- dispatch && dispatch({
- type: as_type(type, 'upload_waiting'),
- percent,
- [type]: id,
- })
- } else {
- dispatch && dispatch({
- type: as_type(type, 'upload_progress'),
- percent,
- [type]: id,
- })
- }
- }
- else {
- dispatch && dispatch({
- type: as_type(type, 'upload_error'),
- error: 'unable to compute upload progress',
- [type]: id,
- })
- }
- }
-
- function uploadComplete (e) {
- let parsed;
- try {
- parsed = JSON.parse(e.target.responseText)
- } catch (e) {
- dispatch && dispatch({
- type: as_type(type, 'upload_error'),
- error: 'upload failed',
- [type]: id,
- })
- reject(e)
- return
- }
- dispatch && dispatch({
- type: as_type(type, 'upload_complete'),
- data: parsed,
- [type]: id,
- })
- if (parsed.res) {
- (parsed.res.length ? parsed.res : [parsed.res]).forEach(file => {
- dispatch && dispatch({
- type: as_type('upload', 'create'),
- data: { res: file },
- })
- })
- }
- resolve(parsed)
- }
-
- function uploadFailed (evt) {
- dispatch && dispatch({
- type: as_type(type, 'upload_error'),
- error: 'upload failed',
- [type]: id,
- })
- reject(evt)
- }
-
- function uploadCancelled (evt) {
- dispatch && dispatch({
- type: as_type(type, 'upload_error'),
- error: 'upload cancelled',
- [type]: id,
- })
- reject(evt)
- }
- })
-}
-
-export const upload_action = (type, data) => dispatch => crud_upload(type, data, dispatch)