summaryrefslogtreecommitdiffstats
path: root/web/react/components/post_attachment_oembed.jsx
blob: f544dbc88741d5775c874ae7b325291eb94a7a5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// 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) {
        this.fetchData(nextProps.link);
    }

    fetchData(link) {
        if (!this.isLoading) {
            this.isLoading = true;
            return $.ajax({
                url: 'https://noembed.com/embed?nowrap=on&url=' + encodeURIComponent(link),
                dataType: 'jsonp',
                success: (result) => {
                    this.isLoading = false;
                    if (result.error) {
                        this.setState({data: {}});
                    } else {
                        this.setState({data: result});
                    }
                },
                error: () => {
                    this.setState({data: {}});
                }
            });
        }
    }

    render() {
        if ($.isEmptyObject(this.state.data)) {
            return <div></div>;
        }

        return (
            <div
                className='attachment attachment--oembed'
                ref='attachment'
            >
                    <div className='attachment__content'>
                        <div
                            className={'clearfix attachment__container'}
                        >
                            <h1
                                className='attachment__title'
                            >
                                <a
                                    className='attachment__title-link'
                                    href={this.state.data.url}
                                    target='_blank'
                                >
                                    {this.state.data.title}
                                </a>
                            </h1>
                            <div>
                                <div className={'attachment__body attachment__body--no_thumb'}>
                                    <div
                                        dangerouslySetInnerHTML={{__html: this.state.data.html}}
                                    >
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
            </div>
        );
    }
}

PostAttachmentOEmbed.propTypes = {
    link: React.PropTypes.string.isRequired
};