2015-09-15 11:50:23 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
import React from 'react';
|
|
|
|
|
2015-09-16 09:47:22 +02:00
|
|
|
import ContractListActions from '../../actions/contract_list_actions';
|
|
|
|
|
|
|
|
import GlobalNotificationModel from '../../models/global_notification_model';
|
|
|
|
import GlobalNotificationActions from '../../actions/global_notification_actions';
|
|
|
|
|
2016-01-21 12:24:15 +01:00
|
|
|
import ReactS3FineUploader from '../ascribe_uploader/react_s3_fine_uploader';
|
|
|
|
import UploadButton from '../ascribe_uploader/ascribe_upload_button/upload_button';
|
|
|
|
|
|
|
|
import ApiUrls from '../../constants/api_urls';
|
|
|
|
import AppConstants from '../../constants/application_constants';
|
|
|
|
import { validationTypes } from '../../constants/uploader_constants';
|
|
|
|
|
2015-09-15 16:35:45 +02:00
|
|
|
import { formSubmissionValidation } from '../ascribe_uploader/react_s3_fine_uploader_utils';
|
|
|
|
import { getCookie } from '../../utils/fetch_api_utils';
|
|
|
|
import { getLangText } from '../../utils/lang_utils';
|
|
|
|
|
2015-09-15 11:50:23 +02:00
|
|
|
|
|
|
|
let ContractSettingsUpdateButton = React.createClass({
|
2015-09-16 09:47:22 +02:00
|
|
|
propTypes: {
|
2015-10-30 17:43:20 +01:00
|
|
|
contract: React.PropTypes.object
|
2015-09-15 16:35:45 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
submitFile(file) {
|
2015-09-16 09:47:22 +02:00
|
|
|
// override the blob with the key's value
|
2015-12-21 11:45:50 +01:00
|
|
|
const contract = Object.assign(this.props.contract, { blob: file.key });
|
2015-09-16 09:47:22 +02:00
|
|
|
|
|
|
|
// send it to the server
|
|
|
|
ContractListActions
|
|
|
|
.changeContract(contract)
|
|
|
|
.then((res) => {
|
2015-09-16 14:07:55 +02:00
|
|
|
// Display feedback to the user
|
2015-12-21 11:45:50 +01:00
|
|
|
const notification = new GlobalNotificationModel(getLangText('Contract %s successfully updated', contract.name), 'success', 5000);
|
2015-09-16 09:47:22 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
|
2015-09-16 14:07:55 +02:00
|
|
|
// and refresh the contract list to get the updated contracs
|
2015-12-21 11:45:50 +01:00
|
|
|
return ContractListActions
|
|
|
|
.fetchContractList(true)
|
|
|
|
// Also, reset the fineuploader component if fetch is successful so that the user can again 'update' his contract
|
|
|
|
.then(this.refs.fineuploader.reset)
|
|
|
|
.catch((err) => {
|
|
|
|
const notification = new GlobalNotificationModel(getLangText('Latest contract failed to load'), 'danger', 5000);
|
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
});
|
|
|
|
}, (err) => {
|
|
|
|
const notification = new GlobalNotificationModel(getLangText('Contract could not be updated'), 'danger', 5000);
|
2015-09-16 09:47:22 +02:00
|
|
|
GlobalNotificationActions.appendGlobalNotification(notification);
|
2015-12-21 11:45:50 +01:00
|
|
|
|
|
|
|
return Promise.reject(err);
|
|
|
|
})
|
|
|
|
.catch(console.logGlobal);
|
2015-09-15 16:35:45 +02:00
|
|
|
},
|
|
|
|
|
2015-09-15 11:50:23 +02:00
|
|
|
render() {
|
|
|
|
return (
|
2015-09-15 16:35:45 +02:00
|
|
|
<ReactS3FineUploader
|
2015-12-21 11:45:50 +01:00
|
|
|
ref='fineuploader'
|
2015-12-21 11:44:13 +01:00
|
|
|
fileInputElement={UploadButton({ showLabel: false })}
|
|
|
|
keyRoutine={{
|
|
|
|
url: AppConstants.serverUrl + 's3/key/',
|
|
|
|
fileClass: 'contract'
|
|
|
|
}}
|
|
|
|
createBlobRoutine={{
|
|
|
|
url: ApiUrls.blob_contracts
|
|
|
|
}}
|
|
|
|
validation={{
|
2016-01-21 12:24:15 +01:00
|
|
|
itemLimit: validationTypes.registerWork.itemLimit,
|
|
|
|
sizeLimit: validationTypes.additionalData.sizeLimit,
|
2015-12-21 11:44:13 +01:00
|
|
|
allowedExtensions: ['pdf']
|
|
|
|
}}
|
|
|
|
setIsUploadReady={() =>{/* So that ReactS3FineUploader is not complaining */}}
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
fileClassToUpload={{
|
|
|
|
singular: getLangText('UPDATE'),
|
|
|
|
plural: getLangText('UPDATE')
|
|
|
|
}}
|
|
|
|
isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile}
|
|
|
|
submitFile={this.submitFile} />
|
2015-09-15 11:50:23 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-10-30 17:43:20 +01:00
|
|
|
export default ContractSettingsUpdateButton;
|