spee.ch/react/components/PublishThumbnailInput.jsx

87 lines
2.6 KiB
React
Raw Normal View History

2018-01-04 23:14:03 +01:00
import React from 'react';
2018-01-11 02:41:17 +01:00
import { connect } from 'react-redux';
import { updateMetadata } from '../actions';
2018-01-04 23:14:03 +01:00
class ThumbnailInput extends React.Component {
2018-01-11 02:41:17 +01:00
constructor (props) {
super(props);
this.state = {
videoPreviewSrc: null,
}
this.urlIsAnImage = this.urlIsAnImage.bind(this);
this.testImage = this.testImage.bind(this);
this.updateVideoThumb = this.updateVideoThumb.bind(this);
}
urlIsAnImage (url) {
return (url.match(/\.(jpeg|jpg|gif|png)$/) != null);
}
testImage (url, timeoutT) {
return new Promise(function (resolve, reject) {
const timeout = timeoutT || 5000;
let timer;
let img = new Image();
img.onerror = img.onabort = function () {
clearTimeout(timer);
reject('error');
};
img.onload = function () {
clearTimeout(timer);
resolve('success');
};
timer = setTimeout(function () {
// reset .src to invalid URL so it stops previous
// loading, but doesn't trigger new load
img.src = '//!!!!/test.jpg';
reject('timeout');
}, timeout);
img.src = url;
});
}
updateVideoThumb (event) {
var imageUrl = event.target.value;
const that = this;
if (this.urlIsAnImage(imageUrl)) {
this.testImage(imageUrl, 3000)
.then(function (result) {
if (result === 'success') {
that.props.onThumbnailChange('thumbnail', imageUrl);
} else if (result === 'timeout') {
console.log('could not resolve the provided thumbnail image url');
}
})
.catch(error => {
console.log('encountered an error loading thumbnail image url:', error);
});
}
}
2018-01-04 23:14:03 +01:00
render () {
return (
2018-01-11 02:41:17 +01:00
<div className="row row--padded row--wide row--no-top" id="publish-thumbnail">
<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">
<input type="text" id="claim-thumbnail-input" className="input-text input-text--full-width" placeholder="https://spee.ch/xyz/example.jpg" value={this.props.thumbnail} onInput={this.updateVideoThumb} />
</div>
</div>
2018-01-04 23:14:03 +01:00
</div>
);
}
}
2018-01-11 02:41:17 +01:00
const mapStateToProps = state => {
return {
thumbnail: state.metadata.thumbnail,
};
};
const mapDispatchToProps = dispatch => {
return {
onThumbnailChange: (name, value) => {
dispatch(updateMetadata(name, value));
},
};
};
export default connect(mapStateToProps, mapDispatchToProps)(ThumbnailInput);