summaryrefslogtreecommitdiffstats
path: root/webapp/components/integrations/components/edit_incoming_webhook.jsx
blob: 9e032409aea19cfe62e53316d51c22322bbb4123 (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
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import * as AsyncClient from 'utils/async_client.jsx';

import {browserHistory} from 'react-router/es6';
import IntegrationStore from 'stores/integration_store.jsx';
import {loadIncomingHooks} from 'actions/integration_actions.jsx';

import AbstractIncomingWebhook from './abstract_incoming_webhook.jsx';
import TeamStore from 'stores/team_store.jsx';

export default class EditIncomingWebhook extends AbstractIncomingWebhook {
    constructor(props) {
        super(props);

        this.handleIntegrationChange = this.handleIntegrationChange.bind(this);
        this.originalIncomingHook = null;
    }

    componentDidMount() {
        IntegrationStore.addChangeListener(this.handleIntegrationChange);

        if (window.mm_config.EnableIncomingWebhooks === 'true') {
            loadIncomingHooks();
        }
    }

    componentWillUnmount() {
        IntegrationStore.removeChangeListener(this.handleIntegrationChange);
    }

    handleIntegrationChange() {
        const teamId = TeamStore.getCurrentId();

        this.setState({
            hooks: IntegrationStore.getIncomingWebhooks(teamId),
            loading: !IntegrationStore.hasReceivedIncomingWebhooks(teamId)
        });

        if (!this.state.loading) {
            this.originalIncomingHook = this.state.hooks.filter((hook) => hook.id === this.props.location.query.id)[0];

            this.setState({
                displayName: this.originalIncomingHook.display_name,
                description: this.originalIncomingHook.description,
                channelId: this.originalIncomingHook.channel_id
            });
        }
    }

    performAction(hook) {
        if (this.originalIncomingHook.id) {
            hook.id = this.originalIncomingHook.id;
        }

        AsyncClient.updateIncomingHook(
            hook,
            () => {
                browserHistory.push(`/${this.props.team.name}/integrations/incoming_webhooks`);
            },
            (err) => {
                this.setState({
                    saving: false,
                    serverError: err.message
                });
            }
        );
    }

    header() {
        return {id: 'integrations.edit', defaultMessage: 'Edit'};
    }

    footer() {
        return {id: 'update_incoming_webhook.update', defaultMessage: 'Update'};
    }
}