spee.ch/react/containers/PublishThumbnailInput/view.jsx

81 lines
2.4 KiB
React
Raw Normal View History

2018-01-04 23:14:03 +01:00
import React from 'react';
2018-01-17 19:49:57 +01:00
class PublishThumbnailInput extends React.Component {
2018-01-11 02:41:17 +01:00
constructor (props) {
super(props);
this.state = {
videoPreviewSrc: null,
2018-01-17 21:54:26 +01:00
thumbnailError : null,
thumbnailInput : '',
2018-01-11 02:41:17 +01:00
}
2018-01-17 21:54:26 +01:00
this.handleInput = this.handleInput.bind(this);
2018-01-11 02:41:17 +01:00
this.updateVideoThumb = this.updateVideoThumb.bind(this);
}
2018-01-17 21:54:26 +01:00
handleInput (event) {
const value = event.target.value;
this.setState({thumbnailInput: value});
}
2018-01-11 02:41:17 +01:00
urlIsAnImage (url) {
return (url.match(/\.(jpeg|jpg|gif|png)$/) != null);
}
2018-01-17 21:54:26 +01:00
testImage (url) {
return new Promise((resolve, reject) => {
const xhttp = new XMLHttpRequest();
xhttp.open('HEAD', url, true);
xhttp.onreadystatechange = () => {
if (xhttp.readyState === 4) {
if (xhttp.status === 200) {
resolve();
} else {
reject();
}
}
2018-01-11 02:41:17 +01:00
};
2018-01-17 21:54:26 +01:00
xhttp.send();
2018-01-11 02:41:17 +01:00
});
}
updateVideoThumb (event) {
2018-01-17 21:54:26 +01:00
const imageUrl = event.target.value;
2018-01-11 02:41:17 +01:00
if (this.urlIsAnImage(imageUrl)) {
this.testImage(imageUrl, 3000)
2018-01-17 21:54:26 +01:00
.then(() => {
console.log('thumbnail is a valid image');
2018-02-07 00:05:31 +01:00
this.props.onThumbnailChange('thumbnail', imageUrl);
this.setState({thumbnailError: null});
2018-01-11 02:41:17 +01:00
})
.catch(error => {
console.log('encountered an error loading thumbnail image url:', error);
2018-02-07 00:05:31 +01:00
this.props.onThumbnailChange('thumbnail', null);
this.setState({thumbnailError: 'That is an invalid image url'});
2018-01-11 02:41:17 +01:00
});
2018-01-17 21:54:26 +01:00
} else {
2018-02-07 00:05:31 +01:00
this.props.onThumbnailChange('thumbnail', null);
this.setState({thumbnailError: null});
2018-01-11 02:41:17 +01:00
}
}
2018-01-04 23:14:03 +01:00
render () {
return (
2018-01-11 21:51:38 +01:00
<div>
2018-01-11 02:41:17 +01:00
<div className="column column--3 column--sml-10">
<label className="label">Thumbnail:</label>
</div><div className="column column--6 column--sml-10">
<div className="input-text--primary">
2018-01-17 21:54:26 +01:00
<p className="info-message-placeholder info-message--failure">{this.state.thumbnailError}</p>
<input
type="text" id="claim-thumbnail-input"
className="input-text input-text--full-width"
placeholder="https://spee.ch/xyz/example.jpg"
value={this.state.thumbnailInput}
onChange={ (event) => {
this.handleInput(event);
this.updateVideoThumb(event);
}} />
2018-01-11 02:41:17 +01:00
</div>
</div>
2018-01-04 23:14:03 +01:00
</div>
);
}
}
2018-01-17 19:49:57 +01:00
export default PublishThumbnailInput;