summaryrefslogtreecommitdiffstats
path: root/web/react/components/more_direct_channels.jsx
blob: 31ecb4c5df201b97a081fcdd8aef94293b763646 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

var ChannelStore = require('../stores/channel_store.jsx');
var TeamStore = require('../stores/team_store.jsx');
var Client = require('../utils/client.jsx');
var AsyncClient = require('../utils/async_client.jsx');
var utils = require('../utils/utils.jsx');

export default class MoreDirectChannels extends React.Component {
    constructor(props) {
        super(props);

        this.state = {channels: [], loadingDMChannel: -1};
    }

    componentDidMount() {
        var self = this;
        $(React.findDOMNode(this.refs.modal)).on('show.bs.modal', function showModal(e) {
            var button = e.relatedTarget;
            self.setState({channels: $(button).data('channels')});
        });
    }

    render() {
        var self = this;

        var directMessageItems = this.state.channels.map(function mapActivityToChannel(channel, index) {
            var badge = '';
            var titleClass = '';
            var active = '';
            var handleClick = null;

            if (channel.fake) {
                // It's a direct message channel that doesn't exist yet so let's create it now
                var otherUserId = utils.getUserIdFromChannelName(channel);

                if (self.state.loadingDMChannel === index) {
                    badge = (
                        <img
                            className='channel-loading-gif pull-right'
                            src='/static/images/load.gif'
                        />
                    );
                }

                if (self.state.loadingDMChannel === -1) {
                    handleClick = function clickHandler(e) {
                        e.preventDefault();
                        self.setState({loadingDMChannel: index});

                        Client.createDirectChannel(channel, otherUserId,
                            function success(data) {
                                $(React.findDOMNode(self.refs.modal)).modal('hide');
                                self.setState({loadingDMChannel: -1});
                                AsyncClient.getChannel(data.id);
                                utils.switchChannel(data);
                            },
                            function error() {
                                self.setState({loadingDMChannel: -1});
                                window.location.href = TeamStore.getCurrentTeamUrl() + '/channels/' + channel.name;
                            }
                        );
                    };
                }
            } else {
                if (channel.id === ChannelStore.getCurrentId()) {
                    active = 'active';
                }

                if (channel.unread) {
                    badge = <span className='badge pull-right small'>{channel.unread}</span>;
                    titleClass = 'unread-title';
                }

                handleClick = function clickHandler(e) {
                    e.preventDefault();
                    utils.switchChannel(channel);
                    $(React.findDOMNode(self.refs.modal)).modal('hide');
                };
            }

            return (
                <li
                    key={channel.name}
                    className={active}
                >
                    <a
                        className={'sidebar-channel ' + titleClass}
                        href='#'
                        onClick={handleClick}
                    >{badge}{channel.display_name}</a>
                </li>
            );
        });

        return (
            <div
                className='modal fade'
                id='more_direct_channels'
                ref='modal'
                tabIndex='-1'
                role='dialog'
                aria-hidden='true'
            >
                <div className='modal-dialog'>
                    <div className='modal-content'>
                        <div className='modal-header'>
                            <button
                                type='button'
                                className='close'
                                data-dismiss='modal'
                            >
                                <span aria-hidden='true'>&times;</span>
                                <span className='sr-only'>Close</span>
                            </button>
                            <h4 className='modal-title'>More Direct Messages</h4>
                        </div>
                        <div className='modal-body'>
                            <ul className='nav nav-pills nav-stacked'>
                                {directMessageItems}
                            </ul>
                        </div>
                        <div className='modal-footer'>
                            <button
                                type='button'
                                className='btn btn-default'
                                data-dismiss='modal'
                            >Close</button>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}