1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-23 01:39:36 +01:00
onion/js/components/ascribe_detail/further_details_fileuploader.js

126 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2015-08-12 13:34:41 +02:00
'use strict';
import React from 'react';
import Property from './../ascribe_forms/property';
import ReactS3FineUploader from './../ascribe_uploader/react_s3_fine_uploader';
import ApiUrls from '../../constants/api_urls';
import AppConstants from '../../constants/application_constants';
import { validationTypes } from '../../constants/uploader_constants';
2015-08-12 13:34:41 +02:00
import { getCookie } from '../../utils/fetch_api_utils';
2015-12-02 12:25:11 +01:00
import { getLangText } from '../../utils/lang_utils';
2015-08-12 13:34:41 +02:00
2015-12-08 20:57:40 +01:00
const { func, bool, number, object, string, arrayOf } = React.PropTypes;
2015-08-12 13:34:41 +02:00
let FurtherDetailsFileuploader = React.createClass({
propTypes: {
2016-02-05 17:06:16 +01:00
pieceId: number.isRequired,
editable: bool,
2015-12-08 20:55:13 +01:00
label: string,
otherData: arrayOf(object),
// Props for ReactS3FineUploader
2016-02-05 17:06:16 +01:00
areAssetsDownloadable: bool,
isReadyForFormSubmission: func,
submitFile: func, // TODO: rename to onSubmitFile
2016-02-05 17:06:16 +01:00
onValidationFailed: func,
multiple: bool,
setIsUploadReady: func, //TODO: rename to setIsUploaderValidated
2016-02-05 17:06:16 +01:00
showErrorPrompt: bool,
validation: ReactS3FineUploader.propTypes.validation
2015-08-12 13:34:41 +02:00
},
getDefaultProps() {
return {
areAssetsDownloadable: true,
2015-12-02 12:25:11 +01:00
label: getLangText('Additional files'),
multiple: false,
validation: validationTypes.additionalData
2015-08-12 13:34:41 +02:00
};
},
render() {
2016-02-08 14:08:54 +01:00
const { editable,
isReadyForFormSubmission,
multiple,
onValidationFailed,
otherData,
pieceId,
setIsUploadReady,
showErrorPrompt,
submitFile,
validation } = this.props;
2015-08-12 13:34:41 +02:00
// Essentially there a three cases important to the fileuploader
//
2015-08-24 11:22:44 +02:00
// 1. there is no other_data => do not show the fileuploader at all (where otherData is now an array)
2015-08-12 13:34:41 +02:00
// 2. there is other_data, but user has no edit rights => show fileuploader but without action buttons
// 3. both other_data and editable are defined or true => show fileuploader with all action buttons
if (!editable && (!otherData || otherData.length === 0)) {
2015-08-12 13:34:41 +02:00
return null;
}
let otherDataIds = otherData ? otherData.map((data) => data.id).join() : null;
2015-08-12 13:34:41 +02:00
return (
<Property
name="other_data_key"
label={this.props.label}>
2015-08-12 13:34:41 +02:00
<ReactS3FineUploader
2016-02-05 17:06:16 +01:00
areAssetsDownloadable
areAssetsEditable={editable}
createBlobRoutine={{
url: ApiUrls.blob_otherdatas,
pieceId: pieceId
}}
deleteFile={{
enabled: true,
method: 'DELETE',
endpoint: AppConstants.serverUrl + 's3/delete',
customHeaders: {
'X-CSRFToken': getCookie(AppConstants.csrftoken)
}
}}
isReadyForFormSubmission={isReadyForFormSubmission}
2015-08-12 13:34:41 +02:00
keyRoutine={{
url: AppConstants.serverUrl + 's3/key/',
fileClass: 'otherdata',
pieceId: pieceId
2015-08-12 13:34:41 +02:00
}}
2016-02-05 17:06:16 +01:00
multiple={multiple}
onValidationFailed={onValidationFailed}
setIsUploadReady={setIsUploadReady}
2015-08-12 13:34:41 +02:00
session={{
endpoint: AppConstants.serverUrl + 'api/blob/otherdatas/fineuploader_session/',
customHeaders: {
'X-CSRFToken': getCookie(AppConstants.csrftoken)
},
params: {
'pk': otherDataIds
2015-08-12 13:34:41 +02:00
},
cors: {
expected: true,
sendCredentials: true
}
}}
signature={{
endpoint: AppConstants.serverUrl + 's3/signature/',
customHeaders: {
'X-CSRFToken': getCookie(AppConstants.csrftoken)
}
}}
2016-02-05 17:06:16 +01:00
submitFile={submitFile}
showErrorPrompt={showErrorPrompt}
validation={validation} />
2015-08-12 13:34:41 +02:00
</Property>
);
}
});
export default FurtherDetailsFileuploader;