1
0
mirror of https://github.com/ascribe/onion.git synced 2024-06-28 16:48:04 +02:00
onion/js/components/ascribe_forms/input_fineuploader.js

156 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-09-10 10:51:47 +02:00
'use strict';
import React from 'react';
import ReactS3FineUploader from '../ascribe_uploader/react_s3_fine_uploader';
import FileDragAndDrop from '../ascribe_uploader/ascribe_file_drag_and_drop/file_drag_and_drop';
2015-09-10 10:51:47 +02:00
import AppConstants from '../../constants/application_constants';
import { getCookie } from '../../utils/fetch_api_utils';
const { func, bool, shape, string, number, arrayOf } = React.PropTypes;
const InputFineUploader = React.createClass({
2015-09-10 10:51:47 +02:00
propTypes: {
setIsUploadReady: func,
isReadyForFormSubmission: func,
submitFile: func,
fileInputElement: func,
2015-09-14 16:15:01 +02:00
areAssetsDownloadable: bool,
keyRoutine: shape({
url: string,
fileClass: string
2015-09-11 10:11:07 +02:00
}),
createBlobRoutine: shape({
url: string
2015-09-11 10:11:07 +02:00
}),
validation: ReactS3FineUploader.propTypes.validation,
2015-09-10 10:51:47 +02:00
// isFineUploaderActive is used to lock react fine uploader in case
// a user is actually not logged in already to prevent him from droping files
// before login in
isFineUploaderActive: bool,
enableLocalHashing: bool,
uploadMethod: string,
2015-09-10 10:51:47 +02:00
// provided by Property
disabled: bool,
// A class of a file the user has to upload
// Needs to be defined both in singular as well as in plural
fileClassToUpload: shape({
singular: string,
plural: string
}),
handleChangedFile: func,
2015-12-21 11:53:27 +01:00
onValidationFailed: func,
// Provided by `Property`
onChange: React.PropTypes.func
2015-09-10 10:51:47 +02:00
},
getDefaultProps() {
return {
fileInputElement: FileDragAndDrop
};
2015-09-10 10:51:47 +02:00
},
getInitialState() {
return {
value: null,
file: null
2015-09-10 10:51:47 +02:00
};
},
submitFile(file) {
2015-09-10 10:51:47 +02:00
this.setState({
file,
value: file.key
2015-09-10 10:51:47 +02:00
});
2015-09-14 16:15:01 +02:00
if(this.state.value && typeof this.props.onChange === 'function') {
this.props.onChange({ target: { value: this.state.value } });
}
if(typeof this.props.submitFile === 'function') {
this.props.submitFile(file);
2015-09-14 16:15:01 +02:00
}
2015-09-10 10:51:47 +02:00
},
2015-09-10 11:22:42 +02:00
reset() {
this.refs.fineuploader.reset();
},
2015-09-10 10:51:47 +02:00
createBlobRoutine() {
const { fineuploader } = this.refs;
const { file } = this.state;
fineuploader.createBlob(file);
},
2015-09-10 11:22:42 +02:00
render() {
2015-12-04 16:34:25 +01:00
const {
areAssetsDownloadable,
enableLocalHashing,
createBlobRoutine,
disabled,
fileClassToUpload,
fileInputElement,
isFineUploaderActive,
isReadyForFormSubmission,
keyRoutine,
2015-12-21 11:53:27 +01:00
onValidationFailed,
2015-12-04 16:34:25 +01:00
setIsUploadReady,
uploadMethod,
validation,
handleChangedFile } = this.props;
2015-12-04 16:34:25 +01:00
let editable = isFineUploaderActive;
2015-09-10 10:51:47 +02:00
// if disabled is actually set by property, we want to override
// isFineUploaderActive
if(typeof disabled !== 'undefined') {
editable = !disabled;
2015-09-10 10:51:47 +02:00
}
return (
<ReactS3FineUploader
2015-09-10 11:22:42 +02:00
ref="fineuploader"
fileInputElement={fileInputElement}
keyRoutine={keyRoutine}
createBlobRoutine={createBlobRoutine}
validation={validation}
submitFile={this.submitFile}
2015-12-21 11:53:27 +01:00
onValidationFailed={onValidationFailed}
setIsUploadReady={setIsUploadReady}
isReadyForFormSubmission={isReadyForFormSubmission}
areAssetsDownloadable={areAssetsDownloadable}
2015-09-10 10:51:47 +02:00
areAssetsEditable={editable}
signature={{
endpoint: AppConstants.serverUrl + 's3/signature/',
customHeaders: {
'X-CSRFToken': getCookie(AppConstants.csrftoken)
}
}}
deleteFile={{
enabled: true,
method: 'DELETE',
endpoint: AppConstants.serverUrl + 's3/delete',
customHeaders: {
'X-CSRFToken': getCookie(AppConstants.csrftoken)
}
}}
enableLocalHashing={enableLocalHashing}
uploadMethod={uploadMethod}
fileClassToUpload={fileClassToUpload}
handleChangedFile={handleChangedFile}/>
2015-09-10 10:51:47 +02:00
);
}
});
export default InputFineUploader;