summaryrefslogtreecommitdiff
path: root/frontend/app/api/crud.upload.js
blob: 8c1b26538cb71757f310957da9b35bd1684863eb (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { as_type } from 'app/api/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)