Advertisement
Guest User

Untitled

a guest
May 26th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. // Redux action
  2. export function uploadSuccess({ data }) {
  3. return {
  4. type: 'UPLOAD_DOCUMENT_SUCCESS',
  5. data,
  6. };
  7. }
  8.  
  9. export function uploadFail(error) {
  10. return {
  11. type: 'UPLOAD_DOCUMENT_FAIL',
  12. error,
  13. };
  14. }
  15.  
  16. export function uploadDocumentRequest({ file, name }) {
  17. let data = new FormData();
  18. data.append('file', document);
  19. data.append('name', name);
  20.  
  21. return (dispatch) => {
  22. axios.post('/files', data)
  23. .then(response => dispatch(uploadSuccess(response))
  24. .catch(error => dispatch(uploadFail(error));
  25. };
  26. }
  27.  
  28. /*
  29. ... A lot of Redux / React boilerplate happens here
  30. like mapDispatchToProps and mapStateToProps and @connect ...
  31. */
  32.  
  33. // Component method
  34. handleFileUpload({ file }) {
  35. const file = files[0];
  36. this.props.actions.uploadRequest({
  37. file,
  38. name: 'Awesome Cat Pic'
  39. })
  40. }
  41.  
  42. // Component render
  43. <input type="file" onChange={this.handleFileUpload} />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement