// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. export default class PostAttachmentOEmbed extends React.Component { constructor(props) { super(props); this.fetchData = this.fetchData.bind(this); this.isLoading = false; } componentWillMount() { this.setState({data: {}}); } componentWillReceiveProps(nextProps) { if (nextProps.link !== this.props.link) { this.isLoading = false; this.fetchData(nextProps.link); } } componentDidMount() { this.fetchData(this.props.link); } fetchData(link) { if (!this.isLoading) { this.isLoading = true; let url = 'https://noembed.com/embed?nowrap=on'; url += '&url=' + encodeURIComponent(link); url += '&maxheight=' + this.props.provider.height; return $.ajax({ url, dataType: 'jsonp', success: (result) => { this.isLoading = false; if (result.error) { this.setState({data: {}}); } else { this.setState({data: result}); } }, error: () => { this.setState({data: {}}); } }); } } render() { let data = {}; let content; if ($.isEmptyObject(this.state.data)) { content =
; } else { data = this.state.data; content = (
); } return (

{data.title}

{content}
); } } PostAttachmentOEmbed.propTypes = { link: React.PropTypes.string.isRequired, provider: React.PropTypes.object.isRequired };