spee.ch/react/components/Preview/index.jsx

63 lines
1.6 KiB
React
Raw Normal View History

import React from 'react';
2018-01-17 18:24:17 +01:00
import PropTypes from 'prop-types';
class Preview extends React.Component {
constructor (props) {
super(props);
this.state = {
2018-01-17 21:54:26 +01:00
imgSource : '',
defaultThumbnail: '/assets/img/video_thumb_default.png',
};
}
componentDidMount () {
2018-03-02 05:09:22 +01:00
this.setPreviewImageSource(this.props.file);
}
2018-01-11 02:41:17 +01:00
componentWillReceiveProps (newProps) {
2018-01-17 21:54:26 +01:00
if (newProps.file !== this.props.file) {
2018-03-02 05:09:22 +01:00
this.setPreviewImageSource(newProps.file);
2018-01-17 21:54:26 +01:00
}
if (newProps.thumbnail !== this.props.thumbnail) {
2018-03-02 05:19:59 +01:00
if (newProps.thumbnail) {
this.setPreviewImageSourceFromFile(newProps.thumbnail);
} else {
this.setState({imgSource: this.state.defaultThumbnail});
}
2018-01-17 21:54:26 +01:00
}
}
2018-03-02 05:09:22 +01:00
setPreviewImageSourceFromFile (file) {
const previewReader = new FileReader();
previewReader.readAsDataURL(file);
previewReader.onloadend = () => {
this.setState({imgSource: previewReader.result});
};
}
setPreviewImageSource (file) {
if (file.type !== 'video/mp4') {
2018-03-02 05:09:22 +01:00
this.setPreviewImageSourceFromFile(file);
} else {
2018-03-02 05:09:22 +01:00
if (this.props.thumbnail) {
this.setPreviewImageSourceFromFile(this.props.thumbnail);
}
this.setState({imgSource: this.state.defaultThumbnail});
}
}
render () {
return (
<img
2018-03-01 06:25:51 +01:00
id='dropzone-preview'
2018-01-17 21:54:26 +01:00
src={this.state.imgSource}
className={this.props.dimPreview ? 'dim' : ''}
2018-03-01 06:25:51 +01:00
alt='publish preview'
/>
);
}
};
2018-01-17 18:24:17 +01:00
Preview.propTypes = {
dimPreview: PropTypes.bool.isRequired,
file : PropTypes.object.isRequired,
2018-03-02 05:09:22 +01:00
thumbnail : PropTypes.object,
2018-01-17 18:24:17 +01:00
};
export default Preview;