import { as_type } from './crud.types' export function crud_upload(type, fd, data, dispatch) { return new Promise( (resolve, reject) => { const id = data.id Object.keys(data).forEach(key => { if (key !== 'id') { fd.append(key, data[key]) } }) console.log('/api/' + type + '/' + id + '/upload/') 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", '/api/' + type + '/' + id + '/upload/') 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.files && parsed.files.length) { parsed.files.forEach(file => { console.log(file) dispatch && dispatch({ type: as_type('file', 'create'), data: 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, id, fd) => dispatch => crud_upload(type, id, fd, dispatch)