Skip to content

Instantly share code, notes, and snippets.

@abachuk
Last active September 6, 2018 23:59
Show Gist options
  • Save abachuk/ab71e1f3d9c11568097446f6fcbc101d to your computer and use it in GitHub Desktop.
Save abachuk/ab71e1f3d9c11568097446f6fcbc101d to your computer and use it in GitHub Desktop.
// 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} />
@xmile1
Copy link

xmile1 commented Sep 6, 2018

// 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