spee.ch/react/components/Preview.jsx

50 lines
1.2 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 = {
previewSource: '',
}
this.previewFile = this.previewFile.bind(this);
}
componentWillMount () {
console.log('Preview will mount');
this.previewFile(this.props.file);
}
2018-01-11 02:41:17 +01:00
componentWillReceiveProps (newProps) {
this.previewFile(newProps.file);
}
previewFile (file) {
const that = this;
if (file.type !== 'video/mp4') {
const previewReader = new FileReader();
previewReader.readAsDataURL(file);
previewReader.onloadend = function () {
that.setState({previewSource: previewReader.result});
};
} else {
2018-01-11 02:41:17 +01:00
that.setState({previewSource: (this.props.thumbnail || '/assets/img/video_thumb_default.png')});
}
}
render () {
return (
<img
id="asset-preview"
src={this.state.previewSource}
className={this.props.dimPreview ? 'dim' : ''}
alt="publish preview"
/>
);
}
};
2018-01-17 18:24:17 +01:00
Preview.propTypes = {
dimPreview: PropTypes.bool.isRequired,
file : PropTypes.object.isRequired,
thumbnail : PropTypes.string,
};
export default Preview;