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

import {browserHistory} from 'react-router/es6';
import LoadingScreen from 'components/loading_screen.jsx';

import AbstractIncomingWebhook from 'components/integrations/components/abstract_incoming_webhook.jsx';

import React from 'react';
import PropTypes from 'prop-types';

const HEADER = {id: 'integrations.edit', defaultMessage: 'Edit'};
const FOOTER = {id: 'update_incoming_webhook.update', defaultMessage: 'Update'};

export default class EditIncomingWebhook extends React.PureComponent {
    static propTypes = {

        /**
        * The current team
        */
        team: PropTypes.object.isRequired,

        /**
        * The incoming webhook to edit
        */
        hook: PropTypes.object,

        /**
        * The id of the incoming webhook to edit
        */
        hookId: PropTypes.string.isRequired,

        /**
        * The request state for updateIncomingHook action. Contains status and error
        */
        updateIncomingHookRequest: PropTypes.object.isRequired,

        actions: PropTypes.shape({

            /**
            * The function to call to update an incoming webhook
            */
            updateIncomingHook: PropTypes.func.isRequired,

            /**
            * The function to call to get an incoming webhook
            */
            getIncomingHook: PropTypes.func.isRequired
        }).isRequired
    }

    constructor(props) {
        super(props);

        this.state = {
            showConfirmModal: false,
            serverError: ''
        };
    }

    componentDidMount() {
        if (window.mm_config.EnableIncomingWebhooks === 'true') {
            this.props.actions.getIncomingHook(this.props.hookId);
        }
    }

    editIncomingHook = async (hook) => {
        this.newHook = hook;

        if (this.props.hook.id) {
            hook.id = this.props.hook.id;
        }

        if (this.props.hook.token) {
            hook.token = this.props.hook.token;
        }

        await this.submitHook();
    }

    submitHook = async () => {
        this.setState({serverError: ''});

        const data = await this.props.actions.updateIncomingHook(this.newHook);

        if (data) {
            browserHistory.push(`/${this.props.team.name}/integrations/incoming_webhooks`);
            return;
        }

        if (this.props.updateIncomingHookRequest.error) {
            this.setState({serverError: this.props.updateIncomingHookRequest.error.message});
        }
    }

    render() {
        if (!this.props.hook) {
            return <LoadingScreen/>;
        }

        return (
            <AbstractIncomingWebhook
                team={this.props.team}
                header={HEADER}
                footer={FOOTER}
                action={this.editIncomingHook}
                serverError={this.state.serverError}
                initialHook={this.props.hook}
            />
        );
    }
}