Last active
March 30, 2018 17:25
-
-
Save spurton/99bd8f418faa24174b0cc936750e7173 to your computer and use it in GitHub Desktop.
Vimeo Thumbnail Component
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import PropTypes from 'prop-types'; | |
class VimeoThumbnail extends Component { | |
state = { | |
loading: true, | |
thumbnail: null | |
}; | |
componentDidMount() { | |
this.getThumbnail(this.props.videoID); | |
} | |
getThumbnail = videoID => { | |
if (videoID === null) { | |
return ''; | |
} | |
const x = new XMLHttpRequest(); | |
x.open('GET', `http://vimeo.com/api/v2/video/${videoID}.xml/`, true); | |
x.onreadystatechange = () => { | |
if (x.readyState === 4 && x.status === 200) { | |
const doc = x.responseXML; | |
const thumbnail = doc.getElementsByTagName('thumbnail_medium')[0]; | |
this.setState({ loading: false }); | |
this.setState({ thumbnail: thumbnail.textContent }); | |
} | |
}; | |
x.send(null); | |
}; | |
renderThumbnail = thumbnail => { | |
if (thumbnail === undefined) { | |
return ( | |
<div | |
style={{ | |
width: 163, | |
height: 114, | |
borderRadius: 8, | |
backgroundColor: '#D8D8D8' | |
}} | |
/> | |
); | |
} | |
return ( | |
<img | |
src={thumbnail} | |
alt="" | |
width="163" | |
height="114" | |
style={{ borderRadius: 8 }} | |
/> | |
); | |
}; | |
render() { | |
if (this.state.loading) { | |
return <div>Loading...</div>; | |
} | |
return <div>{this.renderThumbnail(this.state.thumbnail)}</div>; | |
} | |
} | |
VimeoThumbnail.propTypes = { | |
videoID: PropTypes.string.isRequired | |
}; | |
export default VimeoThumbnail; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment