summaryrefslogtreecommitdiffstats
path: root/web/react/components/post_image.jsx
diff options
context:
space:
mode:
authorChristopher Speller <crspeller@gmail.com>2016-02-24 08:35:34 -0500
committerChristopher Speller <crspeller@gmail.com>2016-02-24 08:35:34 -0500
commitc5dc455a4c930c6521bd355d43b36a079dff8d55 (patch)
tree5b9768aa64d105c59226594dddd10ece52f71e31 /web/react/components/post_image.jsx
parent940b706ac8a790db9f089e000489d89426915fc9 (diff)
parent1dbe949ce8f65f1110e0f43797bd757bfa906ee0 (diff)
downloadchat-c5dc455a4c930c6521bd355d43b36a079dff8d55.tar.gz
chat-c5dc455a4c930c6521bd355d43b36a079dff8d55.tar.bz2
chat-c5dc455a4c930c6521bd355d43b36a079dff8d55.zip
Merge pull request #2233 from hmhealey/plt2010
PLT-2010/PLT-2071 Refactored Post Embed Components
Diffstat (limited to 'web/react/components/post_image.jsx')
-rw-r--r--web/react/components/post_image.jsx81
1 files changed, 81 insertions, 0 deletions
diff --git a/web/react/components/post_image.jsx b/web/react/components/post_image.jsx
new file mode 100644
index 000000000..da4a25794
--- /dev/null
+++ b/web/react/components/post_image.jsx
@@ -0,0 +1,81 @@
+// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
+// See License.txt for license information.
+
+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,
+ errored: false
+ };
+ }
+
+ componentWillMount() {
+ this.loadImg(this.props.link);
+ }
+
+ componentWillReceiveProps(nextProps) {
+ if (nextProps.link !== this.props.link) {
+ this.setState({
+ loaded: false,
+ errored: false
+ });
+ }
+ }
+
+ componentDidUpdate(prevProps) {
+ if (!this.state.loaded && prevProps.link !== this.props.link) {
+ this.loadImg(this.props.link);
+ }
+ }
+
+ loadImg(src) {
+ const img = new Image();
+ 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
+ className='img-div placeholder'
+ height='500px'
+ />
+ );
+ }
+
+ return (
+ <img
+ className='img-div'
+ src={this.props.link}
+ />
+ );
+ }
+}
+
+PostImageEmbed.propTypes = {
+ link: React.PropTypes.string.isRequired
+};