summaryrefslogtreecommitdiffstats
path: root/webapp/components/admin_console/request_button/request_button.jsx
blob: 4aad287d27251cfe60374beb7da5e9bab9d830f5 (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
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import React from 'react';

import {FormattedMessage} from 'react-intl';
import PropTypes from 'prop-types';

import * as Utils from 'utils/utils.jsx';

/**
 * A button which, when clicked, performs an action and displays
 * its outcome as either success, or failure accompanied by the
 * `message` property of the `err` object.
 */
export default class RequestButton extends React.Component {
    static propTypes = {

        /**
         * The action to be called to carry out the request.
         */
        requestAction: PropTypes.func.isRequired,

        /**
         * A component that displays help text for the request button.
         *
         * Typically, this will be a <FormattedMessage/>.
         */
        helpText: PropTypes.element.isRequired,

        /**
         * A component to be displayed on the button.
         *
         * Typically, this will be a <FormattedMessage/>
         */
        buttonText: PropTypes.element.isRequired,

        /**
         * True if the button form control should be disabled, otherwise false.
         */
        disabled: PropTypes.bool,

        /**
         * True if the config needs to be saved before running the request, otherwise false.
         *
         * If set to true, the action provided in the `saveConfigAction` property will be
         * called before the action provided in the `requestAction` property, with the later
         * only being called if the former is successful.
         */
        saveNeeded: PropTypes.bool,

        /**
         * Action to be called to save the config, if saveNeeded is set to true.
         */
        saveConfigAction: PropTypes.func,

        /**
         * True if the success message should be show when the request completes successfully,
         * otherwise false.
         */
        showSuccessMessage: PropTypes.bool,

        /**
         * The message to show when the request completes successfully.
         */
        successMessage: PropTypes.shape({

            /**
             * The i18n string ID for the success message.
             */
            id: PropTypes.string.isRequired,

            /**
             * The i18n default value for the success message.
             */
            defaultMessage: PropTypes.string.isRequired
        }),

        /**
         * The message to show when the request returns an error.
         */
        errorMessage: PropTypes.shape({

            /**
             * The i18n string ID for the error message.
             */
            id: PropTypes.string.isRequired,

            /**
             * The i18n default value for the error message.
             *
             * The placeholder {error} may be used to include the error message returned
             * by the server in response to the failed request.
             */
            defaultMessage: PropTypes.string.isRequired
        }),

        /**
         * True if the {error} placeholder for the `errorMessage` property should include both
         * the `message` and `detailed_error` properties of the error returned from the server,
         * otherwise false to include only the `message` property.
         */
        includeDetailedError: PropTypes.bool
    }

    static defaultProps = {
        disabled: false,
        saveNeeded: false,
        showSuccessMessage: true,
        includeDetailedError: false,
        successMessage: {
            id: 'admin.requestButton.requestSuccess',
            defaultMessage: 'Test Successful'
        },
        errorMessage: {
            id: 'admin.requestButton.requestFailure',
            defaultMessage: 'Test Failure: {error}'
        }
    }

    constructor(props) {
        super(props);

        this.handleRequest = this.handleRequest.bind(this);

        this.state = {
            busy: false,
            fail: null,
            success: false
        };
    }

    handleRequest(e) {
        e.preventDefault();

        this.setState({
            busy: true,
            fail: null,
            success: false
        });

        const doRequest = () => { //eslint-disable-line func-style
            this.props.requestAction(
                () => {
                    this.setState({
                        busy: false,
                        success: true
                    });
                },
                (err) => {
                    let errMsg = err.message;
                    if (this.props.includeDetailedError) {
                        errMsg += ' - ' + err.detailed_error;
                    }

                    this.setState({
                        busy: false,
                        fail: errMsg
                    });
                }
            );
        };

        if (this.props.saveNeeded) {
            this.props.saveConfigAction(doRequest);
        } else {
            doRequest();
        }
    }

    render() {
        let message = null;
        if (this.state.fail) {
            message = (
                <div>
                    <div className='alert alert-warning'>
                        <i className='fa fa-warning'/>
                        <FormattedMessage
                            id={this.props.errorMessage.id}
                            defaultMessage={this.props.errorMessage.defaultMessage}
                            values={{
                                error: this.state.fail
                            }}
                        />
                    </div>
                </div>
            );
        } else if (this.state.success && this.props.showSuccessMessage) {
            message = (
                <div>
                    <div className='alert alert-success'>
                        <i className='fa fa-success'/>
                        <FormattedMessage
                            id={this.props.successMessage.id}
                            defaultMessage={this.props.successMessage.defaultMessage}
                        />
                    </div>
                </div>
            );
        }

        let contents = null;
        if (this.state.busy) {
            contents = (
                <span>
                    <span className='fa fa-refresh icon--rotate'/>
                    {Utils.localizeMessage('admin.requestButton.loading', ' Loading...')}
                </span>
            );
        } else {
            contents = this.props.buttonText;
        }

        return (
            <div className='form-group reload-config'>
                <div className='col-sm-offset-4 col-sm-8'>
                    <div>
                        <button
                            className='btn btn-default'
                            onClick={this.handleRequest}
                            disabled={this.props.disabled}
                        >
                            {contents}
                        </button>
                        {message}
                    </div>
                    <div className='help-text'>
                        {this.props.helpText}
                    </div>
                </div>
            </div>
        );
    }
}