Commit de4fcfd4 authored by Alf Eaton's avatar Alf Eaton
Browse files

Rewrite file upload actions and reducer

parent 544a34b2
Pipeline #1985 failed with stage
in 1 minute and 22 seconds
import request from '../helpers/api'
import * as T from './types'
function fileUploadRequest () {
return {
type: T.FILE_UPLOAD_REQUEST,
isFetching: true
}
}
function fileUploadSuccess (file) {
return {
type: T.FILE_UPLOAD_SUCCESS,
isFetching: false,
file: file
}
}
const fileUploadRequest = () => ({
type: T.FILE_UPLOAD_REQUEST
})
function fileUploadFailure (message) {
return {
type: T.FILE_UPLOAD_FAILURE,
isFetching: false,
error: message
}
}
const fileUploadSuccess = file => ({
type: T.FILE_UPLOAD_SUCCESS,
file
})
export function fileUpload (file) {
return (dispatch) => {
dispatch(fileUploadRequest())
const fileUploadFailure = error => ({
type: T.FILE_UPLOAD_FAILURE,
error
})
const data = new FormData()
data.append('file', file)
export const fileUpload = file => dispatch => {
dispatch(fileUploadRequest())
let opts = {
method: 'POST',
headers: {
'Accept': 'text/plain' // the response is a URL
// TODO: set the Location header of the response instead
},
body: data
}
const data = new FormData()
data.append('file', file)
return request('/upload', opts)
.then(
file => dispatch(fileUploadSuccess(file)),
err => dispatch(fileUploadFailure(err))
)
const opts = {
method: 'POST',
headers: {
'Accept': 'text/plain' // the response is a URL
// TODO: set the Location header of the response instead
},
body: data
}
return request('/upload', opts).then(
url => dispatch(fileUploadSuccess({ url })),
error => dispatch(fileUploadFailure(error))
)
}
import {
FILE_UPLOAD_REQUEST,
FILE_UPLOAD_SUCCESS
FILE_UPLOAD_SUCCESS,
FILE_UPLOAD_FAILURE
} from '../actions/types'
export default function (state = {
isFetching: false
}, action) {
const initialState = {
isUploading: false,
file: null,
error: null
}
export default function (state = initialState, action) {
switch (action.type) {
case FILE_UPLOAD_REQUEST:
return {
isUploading: true,
error: null,
file: null
}
case FILE_UPLOAD_SUCCESS:
return {
...state,
isFetching: false,
isUploading: false,
error: null,
file: action.file
}
case FILE_UPLOAD_REQUEST:
case FILE_UPLOAD_FAILURE:
return {
...state,
isFetching: true
isUploading: false,
error: action.error.message,
file: null
}
default:
return state
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment