diff options
author | hmhealey <harrisonmhealey@gmail.com> | 2016-02-23 16:35:15 -0500 |
---|---|---|
committer | hmhealey <harrisonmhealey@gmail.com> | 2016-02-23 17:01:25 -0500 |
commit | b2e333e83cf3a9693744fa2baab937bb3ac8be28 (patch) | |
tree | 189879fa8814f0705f589a08b2a59de10b9ef972 /web/react | |
parent | 9b50fb855350ea1e747ee946ab3f955430abeb75 (diff) | |
download | chat-b2e333e83cf3a9693744fa2baab937bb3ac8be28.tar.gz chat-b2e333e83cf3a9693744fa2baab937bb3ac8be28.tar.bz2 chat-b2e333e83cf3a9693744fa2baab937bb3ac8be28.zip |
Improved handling when an embedded image cannot be loaded
Diffstat (limited to 'web/react')
-rw-r--r-- | web/react/components/post_image.jsx | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/web/react/components/post_image.jsx b/web/react/components/post_image.jsx index b35f6d1de..da4a25794 100644 --- a/web/react/components/post_image.jsx +++ b/web/react/components/post_image.jsx @@ -5,8 +5,12 @@ export default class PostImageEmbed extends React.Component { constructor(props) { super(props); + this.handleLoadComplete = this.handleLoadComplete.bind(this); + this.handleLoadError = this.handleLoadError.bind(this); + this.state = { - loaded: false + loaded: false, + errored: false }; } @@ -17,7 +21,8 @@ export default class PostImageEmbed extends React.Component { componentWillReceiveProps(nextProps) { if (nextProps.link !== this.props.link) { this.setState({ - loaded: false + loaded: false, + errored: false }); } } @@ -30,15 +35,29 @@ export default class PostImageEmbed extends React.Component { loadImg(src) { const img = new Image(); - img.onload = () => { - this.setState({ - loaded: true - }); - }; + img.onload = this.handleLoadComplete; + img.onerror = this.handleLoadError; img.src = src; } + handleLoadComplete() { + this.setState({ + loaded: true + }); + } + + handleLoadError() { + this.setState({ + errored: true, + loaded: true + }); + } + render() { + if (this.state.errored) { + return null; + } + if (!this.state.loaded) { return ( <img |