2017-07-25 18:40:03 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
This component supports either uploading and storing a single image, or
|
|
|
|
an array of images.
|
|
|
|
|
|
|
|
Note also that an image can be stored as a simple string, or as an array of formats
|
|
|
|
(each format being itself an object).
|
|
|
|
|
|
|
|
*/
|
2017-09-22 12:24:15 +02:00
|
|
|
import { Components, getSetting, registerSetting, registerComponent } from 'meteor/vulcan:lib';
|
2017-05-19 14:42:43 -06:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-03-28 12:07:01 +09:00
|
|
|
import Dropzone from 'react-dropzone';
|
2017-10-05 06:24:47 -03:00
|
|
|
import 'cross-fetch/polyfill'; // patch for browser which don't have fetch implemented
|
2017-08-05 14:46:02 +09:00
|
|
|
import { FormattedMessage } from 'meteor/vulcan:i18n';
|
2017-03-28 12:07:01 +09:00
|
|
|
|
2017-09-22 12:24:15 +02:00
|
|
|
registerSetting('cloudinary.cloudName', null, 'Cloudinary cloud name (for image uploads)');
|
|
|
|
|
2017-07-25 18:40:03 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Get a URL from an image or an array of images
|
|
|
|
|
|
|
|
*/
|
|
|
|
const getImageUrl = imageOrImageArray => {
|
|
|
|
// if image is actually an array of formats, use first format
|
|
|
|
const image = Array.isArray(imageOrImageArray) ? imageOrImageArray[0] : imageOrImageArray;
|
|
|
|
// if image is an object, return secure_url; else return image itself
|
|
|
|
const imageUrl = typeof image === 'string' ? image : image.secure_url;
|
2018-03-30 17:22:24 +09:00
|
|
|
return imageUrl;
|
|
|
|
};
|
2017-07-25 18:40:03 +09:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Display a single image
|
|
|
|
|
|
|
|
*/
|
|
|
|
class Image extends PureComponent {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.clearImage = this.clearImage.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
clearImage(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
this.props.clearImage(this.props.index);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2018-04-06 17:56:45 +09:00
|
|
|
<div className={`upload-image ${this.props.loading ? 'upload-image-loading' : ''}`}>
|
2017-08-05 14:46:02 +09:00
|
|
|
<div className="upload-image-contents">
|
2018-03-30 17:22:24 +09:00
|
|
|
<img style={{ width: 150 }} src={getImageUrl(this.props.image)} />
|
2018-04-06 17:56:45 +09:00
|
|
|
{this.props.loading && (
|
2018-03-30 17:22:24 +09:00
|
|
|
<div className="upload-loading">
|
|
|
|
<Components.Loading />
|
|
|
|
</div>
|
2018-04-06 17:56:45 +09:00
|
|
|
)}
|
2017-08-05 14:46:02 +09:00
|
|
|
</div>
|
2018-03-30 17:22:24 +09:00
|
|
|
<a href="javascript:void(0)" onClick={this.clearImage}>
|
|
|
|
<Components.Icon name="close" /> Remove image
|
|
|
|
</a>
|
2017-07-25 18:40:03 +09:00
|
|
|
</div>
|
2018-03-30 17:22:24 +09:00
|
|
|
);
|
2017-07-25 18:40:03 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Cloudinary Image Upload component
|
|
|
|
|
|
|
|
*/
|
2017-05-19 14:42:43 -06:00
|
|
|
class Upload extends PureComponent {
|
2017-03-28 12:07:01 +09:00
|
|
|
|
2018-04-09 13:11:09 +09:00
|
|
|
state = { uploading: false }
|
|
|
|
|
|
|
|
count = this.props.value.length;
|
2017-03-28 12:07:01 +09:00
|
|
|
|
2017-07-25 18:40:03 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Check the field's type to decide if the component should handle
|
2018-04-09 13:11:09 +09:00
|
|
|
multiple image uploads or not.
|
|
|
|
|
|
|
|
For multiple images, the component expects an array of images;
|
|
|
|
for single images it expects a single image object.
|
2017-07-25 18:40:03 +09:00
|
|
|
|
|
|
|
*/
|
2018-03-30 17:22:24 +09:00
|
|
|
enableMultiple = () => {
|
2018-03-26 17:50:03 +09:00
|
|
|
return this.props.datatype && this.props.datatype[0].type === Array;
|
2018-03-30 17:22:24 +09:00
|
|
|
};
|
2017-07-25 18:40:03 +09:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
When an image is uploaded
|
|
|
|
|
|
|
|
*/
|
2018-03-30 17:22:24 +09:00
|
|
|
onDrop = files => {
|
2018-04-09 13:11:09 +09:00
|
|
|
const promises = [];
|
2017-08-05 14:46:02 +09:00
|
|
|
|
2018-04-09 13:11:09 +09:00
|
|
|
// set the component in upload mode
|
2017-03-28 12:07:01 +09:00
|
|
|
this.setState({
|
|
|
|
uploading: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// request url to cloudinary
|
2017-09-22 12:24:15 +02:00
|
|
|
const cloudinaryUrl = `https://api.cloudinary.com/v1_1/${getSetting('cloudinary.cloudName')}/upload`;
|
2017-03-28 12:07:01 +09:00
|
|
|
|
2018-04-09 13:11:09 +09:00
|
|
|
// trigger a request for each file
|
|
|
|
files.forEach((file, index) => {
|
|
|
|
|
|
|
|
// figure out update path for current image
|
|
|
|
const updateIndex = this.count + index;
|
|
|
|
const updatePath = this.enableMultiple() ? `${this.props.path}.${updateIndex}` : this.props.path;
|
|
|
|
|
|
|
|
// build preview object
|
|
|
|
const previewObject = { secure_url: file.preview, loading: true, preview: true };
|
|
|
|
|
|
|
|
// update current values using preview object
|
|
|
|
this.props.updateCurrentValues({ [updatePath]: previewObject });
|
|
|
|
|
|
|
|
// request body
|
|
|
|
const body = new FormData();
|
|
|
|
body.append('file', file);
|
|
|
|
body.append('upload_preset', this.props.options.preset);
|
|
|
|
|
|
|
|
// post request to cloudinary
|
|
|
|
promises.push(
|
|
|
|
fetch(cloudinaryUrl, {
|
|
|
|
method: 'POST',
|
|
|
|
body,
|
|
|
|
})
|
|
|
|
.then(res => res.json()) // json-ify the readable strem
|
|
|
|
.then(body => {
|
|
|
|
if (body.error) {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(body.error);
|
|
|
|
this.props.throwError({ id: 'upload.error', path: this.props.path, message: body.error.message });
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
// use the https:// url given by cloudinary; or eager property if using transformations
|
|
|
|
const imageObject = body.eager ? body.eager : body.secure_url;
|
|
|
|
this.props.updateCurrentValues({ [updatePath]: imageObject });
|
|
|
|
return imageObject;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
console.log(error);
|
|
|
|
this.props.throwError({ id: 'upload.error', path: this.props.path, message: error.message });
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.all(promises).then(values => {
|
|
|
|
// console.log(values);
|
|
|
|
// set the uploading status to false
|
|
|
|
this.setState({
|
|
|
|
uploading: false,
|
|
|
|
});
|
|
|
|
});
|
2018-03-30 17:22:24 +09:00
|
|
|
};
|
2018-04-06 17:56:45 +09:00
|
|
|
|
2018-03-30 17:22:24 +09:00
|
|
|
isDeleted = index => {
|
2018-04-09 13:11:09 +09:00
|
|
|
return this.props.deletedValues.includes(`${this.props.path}.${index}`);
|
2018-03-30 17:22:24 +09:00
|
|
|
};
|
2017-03-28 12:07:01 +09:00
|
|
|
|
2017-07-25 18:40:03 +09:00
|
|
|
/*
|
|
|
|
|
|
|
|
Remove the image at `index` (or just remove image if no index is passed)
|
|
|
|
|
|
|
|
*/
|
2018-03-30 17:22:24 +09:00
|
|
|
clearImage = index => {
|
|
|
|
if (this.enableMultiple()) {
|
2018-04-09 13:11:09 +09:00
|
|
|
this.props.addToDeletedValues(`${this.props.path}.${index}`);
|
2018-03-30 17:22:24 +09:00
|
|
|
} else {
|
2018-04-09 13:11:09 +09:00
|
|
|
this.props.addToDeletedValues(this.props.path);
|
2018-03-30 17:22:24 +09:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
getImages = () => {
|
2018-04-09 13:11:09 +09:00
|
|
|
// show the actual uploaded image(s)
|
|
|
|
return this.enableMultiple() ? this.props.value : [this.props.value];
|
2018-03-30 17:22:24 +09:00
|
|
|
};
|
2017-03-28 12:07:01 +09:00
|
|
|
|
2018-04-09 13:11:09 +09:00
|
|
|
|
2017-03-28 12:07:01 +09:00
|
|
|
render() {
|
2018-03-30 17:22:24 +09:00
|
|
|
const { uploading } = this.state;
|
|
|
|
const images = this.getImages();
|
2017-03-28 12:07:01 +09:00
|
|
|
return (
|
|
|
|
<div className="form-group row">
|
|
|
|
<label className="control-label col-sm-3">{this.props.label}</label>
|
|
|
|
<div className="col-sm-9">
|
|
|
|
<div className="upload-field">
|
2018-03-30 17:22:24 +09:00
|
|
|
<Dropzone
|
|
|
|
ref="dropzone"
|
2017-07-25 18:40:03 +09:00
|
|
|
multiple={this.enableMultiple()}
|
2017-03-28 12:07:01 +09:00
|
|
|
onDrop={this.onDrop}
|
|
|
|
accept="image/*"
|
|
|
|
className="dropzone-base"
|
|
|
|
activeClassName="dropzone-active"
|
|
|
|
rejectClassName="dropzone-reject"
|
2018-04-09 13:11:09 +09:00
|
|
|
disabled={this.state.uploading}
|
2017-03-28 12:07:01 +09:00
|
|
|
>
|
2018-03-30 17:22:24 +09:00
|
|
|
<div>
|
|
|
|
<FormattedMessage id="upload.prompt" />
|
|
|
|
</div>
|
|
|
|
{uploading && (
|
|
|
|
<div className="upload-uploading">
|
|
|
|
<span>
|
|
|
|
<FormattedMessage id="upload.uploading" />
|
|
|
|
</span>
|
|
|
|
</div>
|
|
|
|
)}
|
2017-03-28 12:07:01 +09:00
|
|
|
</Dropzone>
|
|
|
|
|
2018-03-30 17:22:24 +09:00
|
|
|
{!!images.length && (
|
2017-03-28 12:07:01 +09:00
|
|
|
<div className="upload-state">
|
2017-08-05 14:46:02 +09:00
|
|
|
<div className="upload-images">
|
2018-04-06 17:56:45 +09:00
|
|
|
{images.map(
|
|
|
|
(image, index) =>
|
|
|
|
!this.isDeleted(index) &&
|
2018-04-09 13:11:09 +09:00
|
|
|
image && (
|
|
|
|
<Image
|
|
|
|
clearImage={this.clearImage}
|
|
|
|
key={index}
|
|
|
|
index={index}
|
|
|
|
image={image}
|
|
|
|
loading={image.loading}
|
|
|
|
preview={image.preview}
|
|
|
|
/>
|
|
|
|
)
|
2018-04-06 17:56:45 +09:00
|
|
|
)}
|
2017-07-25 18:40:03 +09:00
|
|
|
</div>
|
2017-03-28 12:07:01 +09:00
|
|
|
</div>
|
2018-03-30 17:22:24 +09:00
|
|
|
)}
|
2017-03-28 12:07:01 +09:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Upload.propTypes = {
|
2017-05-19 14:42:43 -06:00
|
|
|
name: PropTypes.string,
|
|
|
|
value: PropTypes.any,
|
2018-03-30 17:22:24 +09:00
|
|
|
label: PropTypes.string,
|
2017-03-28 12:07:01 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
registerComponent('Upload', Upload);
|
|
|
|
|
2018-03-30 17:22:24 +09:00
|
|
|
export default Upload;
|