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

import AbstractOutgoingWebhook from 'components/integrations/components/abstract_outgoing_webhook.jsx';
import ConfirmModal from 'components/confirm_modal.jsx';
import LoadingScreen from 'components/loading_screen.jsx';

import React from 'react';
import PropTypes from 'prop-types';
import {browserHistory} from 'react-router/es6';
import {FormattedMessage} from 'react-intl';

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

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

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

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

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

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

        actions: PropTypes.shape({

            /**
             * The function to call to update an outgoing webhook
             */
            updateOutgoingHook: PropTypes.func.isRequired,

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

    constructor(props) {
        super(props);

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

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

    editOutgoingHook = 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;
        }

        const triggerWordsSame = (this.props.hook.trigger_words.length === hook.trigger_words.length) &&
            this.props.hook.trigger_words.every((v, i) => v === hook.trigger_words[i]);

        const callbackUrlsSame = (this.props.hook.callback_urls.length === hook.callback_urls.length) &&
            this.props.hook.callback_urls.every((v, i) => v === hook.callback_urls[i]);

        if (this.props.hook.content_type !== hook.content_type ||
            !triggerWordsSame || !callbackUrlsSame) {
            this.handleConfirmModal();
        } else {
            await this.submitHook();
        }
    }

    handleConfirmModal = () => {
        this.setState({showConfirmModal: true});
    }

    confirmModalDismissed = () => {
        this.setState({showConfirmModal: false});
    }

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

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

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

        this.setState({showConfirmModal: false});

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

    renderExtra = () => {
        const confirmButton = (
            <FormattedMessage
                id='update_outgoing_webhook.update'
                defaultMessage='Update'
            />
        );

        const confirmTitle = (
            <FormattedMessage
                id='update_outgoing_webhook.confirm'
                defaultMessage='Edit Outgoing Webhook'
            />
        );

        const confirmMessage = (
            <FormattedMessage
                id='update_outgoing_webhook.question'
                defaultMessage='Your changes may break the existing outgoing webhook. Are you sure you would like to update it?'
            />
        );

        return (
            <ConfirmModal
                title={confirmTitle}
                message={confirmMessage}
                confirmButtonText={confirmButton}
                show={this.state.showConfirmModal}
                onConfirm={this.submitHook}
                onCancel={this.confirmModalDismissed}
            />
        );
    }

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

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