Created
February 24, 2015 00:55
-
-
Save Polyrhythm/edf19aead4c83e5d1e7f to your computer and use it in GitHub Desktop.
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
var CLIENT_ID = 'nope'; | |
class Matcher extends React.Component { | |
constructor(props: { imageCount: number }) { | |
super(props); | |
this.state = { images: [] }; | |
this.fetchRandomImages(this.props.imageCount); | |
} | |
fetchRandomImages(imageCount) { | |
var req = new XMLHttpRequest(); | |
function success() { | |
var images = JSON.parse(req.response) | |
.data | |
.slice(0, this.props.imageCount); | |
this.setState({ | |
images: images | |
}); | |
} | |
function error(e) { | |
console.log('error', console.log(req.responseText)); | |
} | |
req.addEventListener('load', success.bind(this), false); | |
req.addEventListener('error', error, false); | |
req.open('get', 'https://api.imgur.com/3/gallery/random/random/0', true); | |
req.setRequestHeader('Authorization', `Client-ID ${CLIENT_ID}`); | |
req.send(); | |
} | |
render() { | |
var images; | |
if (!this.state.images.length) { | |
return <div>we do not have images!</div>; | |
} | |
images = this.state.images.map(function(image) { | |
return <li><Image src={image.link} title={image.title} /></li>; | |
}); | |
return <ul>{images}</ul>; | |
} | |
} | |
Matcher.defaultProps = { imageCount: 6 }; | |
React.render(<Matcher />, document.querySelector('#app')); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment