Skip to content

Instantly share code, notes, and snippets.

@colllin
Created November 2, 2015 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colllin/660d7ef5ebedd2d4147d to your computer and use it in GitHub Desktop.
Save colllin/660d7ef5ebedd2d4147d to your computer and use it in GitHub Desktop.
React Component for Filepicker.io pickAndStore dialog
import React from 'react';
import $ from 'jquery';
// Accepts these props:
// `apiKey` - Your Filepicker.io API key.
// `onSuccess` - A callback that receives (blobs) upon pickAndStore success.
// `onError` - A callback that receives (error) upon pickAndStore error. Note that a user-initiated cancel triggers an error with {code: 101}.
// `onCancel` - A callback that is triggered when the user closes/cancels the dialog. If `onCancel` is passed, it will be called instead of onError in the event of a user-initiated cancel.
// `onProgress` - A callback that receives (progress) upon pickAndStore progress.
class FilepickerPickAndStoreDialog extends React.Component {
componentDidMount() {
filepicker.setKey(this.props.apiKey);
filepicker.pickAndStore(
{
// mimetype: 'image/*',
// openTo: '/Imagesearch/lolcats'
multiple: true
},
{
location: 'S3'
},
this.props.onSuccess && ((blobs) => this.props.onSuccess(blobs)),
(error) => {
// Pass an `onCancel` prop to send user-cancelations to onCancel instead of onError.
if (error.code == 101 && this.props.onCancel) {
this.props.onCancel();
} else {
this.props.onError && this.props.onError(error)
}
},
this.props.onProgress && ((progress) => this.props.onProgress(progress))
);
}
componentWillUnmount() {
$('#filepicker_shade').remove();
}
render() {
return (
<span style={{
display: 'none'
}}></span>
);
}
}
export default FilepickerPickAndStoreDialog;
<FilepickerDialog apiKey={YOUR_FILEPICKER_API_KEY}
onSuccess={(blobs) => console.log('onSuccess', JSON.stringify(blobs))}
onCancel={() => this.props.history.goBack()}
onError={(error) => console.log('onError', JSON.stringify(error))}
onProgress={(progress) => console.log('onProgress', JSON.stringify(progress))}
/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment