summaryrefslogtreecommitdiffstats
path: root/web/react/components/user_settings/manage_incoming_hooks.jsx
blob: 79a71b5ac717fbff28e9bdc70c91ff353519a630 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import * as Client from '../../utils/client.jsx';
import * as Utils from '../../utils/utils.jsx';
import Constants from '../../utils/constants.jsx';
import ChannelStore from '../../stores/channel_store.jsx';
import LoadingScreen from '../loading_screen.jsx';

import {FormattedMessage, FormattedHTMLMessage} from 'mm-intl';

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

        this.getHooks = this.getHooks.bind(this);
        this.addNewHook = this.addNewHook.bind(this);
        this.updateChannelId = this.updateChannelId.bind(this);

        this.state = {hooks: [], channelId: ChannelStore.getByName(Constants.DEFAULT_CHANNEL).id, getHooksComplete: false};
    }
    componentDidMount() {
        this.getHooks();
    }
    addNewHook() {
        const hook = {};
        hook.channel_id = this.state.channelId;

        Client.addIncomingHook(
            hook,
            (data) => {
                let hooks = this.state.hooks;
                if (!hooks) {
                    hooks = [];
                }
                hooks.push(data);
                this.setState({hooks});
            },
            (err) => {
                this.setState({serverError: err});
            }
        );
    }
    removeHook(id) {
        const data = {};
        data.id = id;

        Client.deleteIncomingHook(
            data,
            () => {
                const hooks = this.state.hooks;
                let index = -1;
                for (let i = 0; i < hooks.length; i++) {
                    if (hooks[i].id === id) {
                        index = i;
                        break;
                    }
                }

                if (index !== -1) {
                    hooks.splice(index, 1);
                }

                this.setState({hooks});
            },
            (err) => {
                this.setState({serverError: err});
            }
        );
    }
    getHooks() {
        Client.listIncomingHooks(
            (data) => {
                const state = this.state;

                if (data) {
                    state.hooks = data;
                }

                state.getHooksComplete = true;
                this.setState(state);
            },
            (err) => {
                this.setState({serverError: err});
            }
        );
    }
    updateChannelId(e) {
        this.setState({channelId: e.target.value});
    }
    render() {
        let serverError;
        if (this.state.serverError) {
            serverError = <label className='has-error'>{this.state.serverError}</label>;
        }

        const channels = ChannelStore.getAll();
        const options = [];
        channels.forEach((channel) => {
            if (channel.type !== Constants.DM_CHANNEL) {
                options.push(
                    <option
                        key={'incoming-hook' + channel.id}
                        value={channel.id}
                    >
                        {channel.display_name}
                    </option>
                );
            }
        });

        let disableButton = '';
        if (this.state.channelId === '') {
            disableButton = ' disable';
        }

        const hooks = [];
        this.state.hooks.forEach((hook) => {
            const c = ChannelStore.get(hook.channel_id);
            if (c) {
                hooks.push(
                    <div
                        key={hook.id}
                        className='webhook__item'
                    >
                        <div className='padding-top x2 webhook__url'>
                            <strong>{'URL: '}</strong>
                            <span className='word-break--all'>{Utils.getWindowLocationOrigin() + '/hooks/' + hook.id}</span>
                        </div>
                        <div className='padding-top'>
                            <strong>
                                <FormattedMessage
                                    id='user.settings.hooks_in.channel'
                                    defaultMessage='Channel: '
                                />
                            </strong>{c.display_name}
                        </div>
                        <a
                            className={'webhook__remove'}
                            href='#'
                            onClick={this.removeHook.bind(this, hook.id)}
                        >
                            <span aria-hidden='true'>{'×'}</span>
                        </a>
                        <div className='padding-top x2 divider-light'></div>
                    </div>
                );
            }
        });

        let displayHooks;
        if (!this.state.getHooksComplete) {
            displayHooks = <LoadingScreen/>;
        } else if (hooks.length > 0) {
            displayHooks = hooks;
        } else {
            displayHooks = (
                <div className='padding-top x2'>
                    <FormattedMessage
                        id='user.settings.hooks_in.none'
                        defaultMessage='None'
                    />
                </div>
            );
        }

        const existingHooks = (
            <div className='webhooks__container'>
                <label className='control-label padding-top x2'>
                    <FormattedMessage
                        id='user.settings.hooks_in.existing'
                        defaultMessage='Existing incoming webhooks'
                    />
                </label>
                <div className='padding-top divider-light'></div>
                <div className='webhooks__list'>
                    {displayHooks}
                </div>
            </div>
        );

        return (
            <div key='addIncomingHook'>
                <FormattedHTMLMessage
                    id='user.settings.hooks_in.description'
                    defaultMessage='Create webhook URLs for use in external integrations. Please see <a href="http://docs.mattermost.com/developer/webhooks-incoming.html" target="_blank">incoming webhooks documentation</a> to learn more. View all incoming webhooks configured on this team below.'
                />
                <div><label className='control-label padding-top x2'>
                    <FormattedMessage
                        id='user.settings.hooks_in.addTitle'
                        defaultMessage='Add a new incoming webhook'
                    />
                </label></div>
                <div className='row padding-top'>
                    <div className='col-sm-10 padding-bottom'>
                        <select
                            ref='channelName'
                            className='form-control'
                            value={this.state.channelId}
                            onChange={this.updateChannelId}
                        >
                            {options}
                        </select>
                        {serverError}
                    </div>
                    <div className='col-sm-2 col-xs-4 no-padding--left padding-bottom'>
                        <a
                            className={'btn form-control no-padding btn-sm btn-primary' + disableButton}
                            href='#'
                            onClick={this.addNewHook}
                        >
                            <FormattedMessage
                                id='user.settings.hooks_in.add'
                                defaultMessage='Add'
                            />
                        </a>
                    </div>
                </div>
                {existingHooks}
            </div>
        );
    }
}