Last active
September 6, 2018 23:59
-
-
Save abachuk/ab71e1f3d9c11568097446f6fcbc101d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Redux action | |
export function uploadSuccess({ data }) { | |
return { | |
type: 'UPLOAD_DOCUMENT_SUCCESS', | |
data, | |
}; | |
} | |
export function uploadFail(error) { | |
return { | |
type: 'UPLOAD_DOCUMENT_FAIL', | |
error, | |
}; | |
} | |
export function uploadDocumentRequest({ file, name }) { | |
let data = new FormData(); | |
data.append('file', document); | |
data.append('name', name); | |
return (dispatch) => { | |
axios.post('/files', data) | |
.then(response => dispatch(uploadSuccess(response)) | |
.catch(error => dispatch(uploadFail(error)); | |
}; | |
} | |
/* | |
... A lot of Redux / React boilerplate happens here | |
like mapDispatchToProps and mapStateToProps and @connect ... | |
*/ | |
// Component method | |
handleFileUpload({ file }) { | |
const file = files[0]; | |
this.props.actions.uploadRequest({ | |
file, | |
name: 'Awesome Cat Pic' | |
}) | |
} | |
// Component render | |
<input type="file" onChange={this.handleFileUpload} /> |
// Component method
handleFileUpload({ file }) {
const file = files[0];
this.props.actions.uploadRequest({
file,
name: 'Awesome Cat Pic'
})
}
should be
// Component method
handleFileUpload({ target: files }) {
const file = files[0];
this.props.actions.uploadRequest({
file,
name: 'Awesome Cat Pic'
})
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On
uploadDocumentRequest
you receivefile
but you appenddocument
to thedata
variable. Are those intended to be the same? Or how do you get the information on the document?