1
0
mirror of https://github.com/ascribe/onion.git synced 2025-01-05 11:25:09 +01:00

Changing behavior for ReactS3Fineuploader not having access to createBlobRoutine

This commit is contained in:
Tim Daubenschütz 2015-11-11 18:17:32 +01:00
parent 0438f461c2
commit 62f19467b7
3 changed files with 102 additions and 100 deletions

View File

@ -21,7 +21,6 @@ const InputFineUploader = React.createClass({
areAssetsDownloadable: bool, areAssetsDownloadable: bool,
onClick: func,
keyRoutine: shape({ keyRoutine: shape({
url: string, url: string,
fileClass: string fileClass: string
@ -69,7 +68,6 @@ const InputFineUploader = React.createClass({
}, },
submitFile(file) { submitFile(file) {
console.log(file);
this.setState({ this.setState({
file, file,
value: file.key value: file.key
@ -97,7 +95,6 @@ const InputFineUploader = React.createClass({
render() { render() {
const { fileInputElement, const { fileInputElement,
onClick,
keyRoutine, keyRoutine,
createBlobRoutine, createBlobRoutine,
validation, validation,
@ -120,7 +117,6 @@ const InputFineUploader = React.createClass({
<ReactS3FineUploader <ReactS3FineUploader
ref="fineuploader" ref="fineuploader"
fileInputElement={fileInputElement} fileInputElement={fileInputElement}
onClick={onClick}
keyRoutine={keyRoutine} keyRoutine={keyRoutine}
createBlobRoutine={createBlobRoutine} createBlobRoutine={createBlobRoutine}
validation={validation} validation={validation}

View File

@ -298,10 +298,7 @@ let ReactS3FineUploader = React.createClass({
resolve(res.key); resolve(res.key);
}) })
.catch((err) => { .catch((err) => {
console.logGlobal(err, false, { this.onErrorPromiseProxy(err);
files: this.state.filesToUpload,
chunks: this.state.chunks
});
reject(err); reject(err);
}); });
}); });
@ -310,12 +307,17 @@ let ReactS3FineUploader = React.createClass({
createBlob(file) { createBlob(file) {
const { createBlobRoutine } = this.props; const { createBlobRoutine } = this.props;
// returning here without doing anything enables us to
// lazy create the blob at any time after the upload
if(!createBlobRoutine) {
return null;
} else {
return Q.Promise((resolve, reject) => { return Q.Promise((resolve, reject) => {
// if createBlobRoutine is not defined,
// we're progressing right away without posting to S3
// so that this can be done manually by the form
if(!createBlobRoutine) {
// still we warn the user of this component
console.warn('createBlobRoutine was not defined for ReactS3FineUploader. Continuing without creating the blob on the server.');
resolve();
}
window.fetch(createBlobRoutine.url, { window.fetch(createBlobRoutine.url, {
method: 'post', method: 'post',
headers: { headers: {
@ -352,14 +354,10 @@ let ReactS3FineUploader = React.createClass({
resolve(res); resolve(res);
}) })
.catch((err) => { .catch((err) => {
console.logGlobal(err, false, { this.onErrorPromiseProxy(err);
files: this.state.filesToUpload,
chunks: this.state.chunks
});
reject(err); reject(err);
}); });
}); });
}
}, },
/* FineUploader specific callback function handlers */ /* FineUploader specific callback function handlers */
@ -415,14 +413,6 @@ let ReactS3FineUploader = React.createClass({
let filesToUpload = React.addons.update(this.state.filesToUpload, { $set: files }); let filesToUpload = React.addons.update(this.state.filesToUpload, { $set: files });
this.setState({ filesToUpload }); this.setState({ filesToUpload });
const createBlobResult = this.createBlob(files[id]);
if(!createBlobResult) {
if(this.props.submitFile) {
this.props.submitFile(files[id]);
} else {
console.warn('You didn\'t define submitFile in as a prop in react-s3-fine-uploader');
}
} else {
// Only after the blob has been created server-side, we can make the form submittable. // Only after the blob has been created server-side, we can make the form submittable.
this.createBlob(files[id]) this.createBlob(files[id])
.then(() => { .then(() => {
@ -447,23 +437,27 @@ let ReactS3FineUploader = React.createClass({
console.warn('You didn\'t define the functions isReadyForFormSubmission and/or setIsUploadReady in as a prop in react-s3-fine-uploader'); console.warn('You didn\'t define the functions isReadyForFormSubmission and/or setIsUploadReady in as a prop in react-s3-fine-uploader');
} }
}) })
.catch((err) => { .catch(this.onErrorPromiseProxy);
console.logGlobal(err, false, {
files: this.state.filesToUpload,
chunks: this.state.chunks
});
let notification = new GlobalNotificationModel(err.message, 'danger', 5000);
GlobalNotificationActions.appendGlobalNotification(notification);
});
}
} }
}, },
/**
* We want to channel all errors in this component through one single method.
* As fineuploader's onError method cannot handle the callback parameters of
* a promise we define this proxy method to crunch them into the correct form.
*
* @param {error} err plain Javascript error
*/
onErrorPromiseProxy(err) {
this.onError(null, null, err.message);
},
onError(id, name, errorReason) { onError(id, name, errorReason) {
console.logGlobal(errorReason, false, { console.logGlobal(errorReason, false, {
files: this.state.filesToUpload, files: this.state.filesToUpload,
chunks: this.state.chunks chunks: this.state.chunks
}); });
this.props.setIsUploadReady(true);
this.state.uploader.cancelAll(); this.state.uploader.cancelAll();
let notification = new GlobalNotificationModel(errorReason || this.props.defaultErrorMessage, 'danger', 5000); let notification = new GlobalNotificationModel(errorReason || this.props.defaultErrorMessage, 'danger', 5000);
@ -628,6 +622,10 @@ let ReactS3FineUploader = React.createClass({
}, },
handleUploadFile(files) { handleUploadFile(files) {
// While files are being uploaded, the form cannot be ready
// for submission
this.props.setIsUploadReady(false);
// If multiple set and user already uploaded its work, // If multiple set and user already uploaded its work,
// cancel upload // cancel upload
if(!this.props.multiple && this.state.filesToUpload.filter(displayValidFilesFilter).length > 0) { if(!this.props.multiple && this.state.filesToUpload.filter(displayValidFilesFilter).length > 0) {

View File

@ -36,7 +36,7 @@ const PRRegisterPieceForm = React.createClass({
getInitialState(){ getInitialState(){
return { return {
isUploadReady: false, isUploadReady: true,
piece: null piece: null
}; };
}, },
@ -124,8 +124,15 @@ const PRRegisterPieceForm = React.createClass({
} }
}, },
setIsUploadReady(isUploadReady) {
this.setState({
isUploadReady
});
},
render() { render() {
const { location } = this.props; const { location } = this.props;
const { isUploadReady } = this.state;
return ( return (
<div className="register-piece--form"> <div className="register-piece--form">
@ -196,7 +203,7 @@ const PRRegisterPieceForm = React.createClass({
<InputFineuploader <InputFineuploader
fileInputElement={UploadButton} fileInputElement={UploadButton}
isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile} isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile}
setIsUploadReady={() =>{/* So that ReactS3FineUploader is not complaining */}} setIsUploadReady={this.setIsUploadReady}
createBlobRoutine={{ createBlobRoutine={{
url: ApiUrls.blob_digitalworks url: ApiUrls.blob_digitalworks
}} }}
@ -226,7 +233,7 @@ const PRRegisterPieceForm = React.createClass({
url: ApiUrls.blob_thumbnails url: ApiUrls.blob_thumbnails
}} }}
isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile} isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile}
setIsUploadReady={() =>{/* So that ReactS3FineUploader is not complaining */}} setIsUploadReady={this.setIsUploadReady}
keyRoutine={{ keyRoutine={{
url: AppConstants.serverUrl + 's3/key/', url: AppConstants.serverUrl + 's3/key/',
fileClass: 'thumbnail' fileClass: 'thumbnail'
@ -249,7 +256,7 @@ const PRRegisterPieceForm = React.createClass({
<InputFineuploader <InputFineuploader
fileInputElement={UploadButton} fileInputElement={UploadButton}
isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile} isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile}
setIsUploadReady={() =>{/* So that ReactS3FineUploader is not complaining */}} setIsUploadReady={this.setIsUploadReady}
createBlobRoutine={this.getCreateBlobRoutine()} createBlobRoutine={this.getCreateBlobRoutine()}
keyRoutine={{ keyRoutine={{
url: AppConstants.serverUrl + 's3/key/', url: AppConstants.serverUrl + 's3/key/',
@ -272,7 +279,7 @@ const PRRegisterPieceForm = React.createClass({
<InputFineuploader <InputFineuploader
fileInputElement={UploadButton} fileInputElement={UploadButton}
isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile} isReadyForFormSubmission={formSubmissionValidation.atLeastOneUploadedFile}
setIsUploadReady={() =>{/* So that ReactS3FineUploader is not complaining */}} setIsUploadReady={this.setIsUploadReady}
createBlobRoutine={this.getCreateBlobRoutine()} createBlobRoutine={this.getCreateBlobRoutine()}
keyRoutine={{ keyRoutine={{
url: AppConstants.serverUrl + 's3/key/', url: AppConstants.serverUrl + 's3/key/',
@ -307,6 +314,7 @@ const PRRegisterPieceForm = React.createClass({
<button <button
type="submit" type="submit"
className="btn btn-default btn-wide" className="btn btn-default btn-wide"
disabled={!isUploadReady}
onClick={this.submit}> onClick={this.submit}>
{getLangText('Submit to Portfolio Review')} {getLangText('Submit to Portfolio Review')}
</button> </button>