summaryrefslogtreecommitdiffstats
path: root/web/react/components/user_settings/manage_command_hooks.jsx
blob: 2947138be3aa346abd55c63bfbea5c22d5bf7382 (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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// Copyright (c) 2016 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import LoadingScreen from '../loading_screen.jsx';

import * as Client from '../../utils/client.jsx';

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

const holders = defineMessages({
    requestTypePost: {
        id: 'user.settings.cmds.request_type_post',
        defaultMessage: 'POST'
    },
    requestTypeGet: {
        id: 'user.settings.cmds.request_type_get',
        defaultMessage: 'GET'
    },
    addDisplayNamePlaceholder: {
        id: 'user.settings.cmds.add_display_name.placeholder',
        defaultMessage: 'Example: "Search patient records"'
    },
    addUsernamePlaceholder: {
        id: 'user.settings.cmds.add_username.placeholder',
        defaultMessage: 'Username'
    },
    addTriggerPlaceholder: {
        id: 'user.settings.cmds.add_trigger.placeholder',
        defaultMessage: 'Command trigger e.g. "hello" not including the slash'
    },
    addAutoCompleteDescPlaceholder: {
        id: 'user.settings.cmds.auto_complete_desc.placeholder',
        defaultMessage: 'Example: "Returns search results for patient records"'
    },
    addAutoCompleteHintPlaceholder: {
        id: 'user.settings.cmds.auto_complete_hint.placeholder',
        defaultMessage: 'Example: [Patient Name]'
    },
    adUrlPlaceholder: {
        id: 'user.settings.cmds.url.placeholder',
        defaultMessage: 'Must start with http:// or https://'
    },
    autocompleteYes: {
        id: 'user.settings.cmds.auto_complete.yes',
        defaultMessage: 'yes'
    },
    autocompleteNo: {
        id: 'user.settings.cmds.auto_complete.no',
        defaultMessage: 'no'
    }
});

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

        this.getCmds = this.getCmds.bind(this);
        this.addNewCmd = this.addNewCmd.bind(this);
        this.emptyCmd = this.emptyCmd.bind(this);
        this.updateTrigger = this.updateTrigger.bind(this);
        this.updateURL = this.updateURL.bind(this);
        this.updateMethod = this.updateMethod.bind(this);
        this.updateUsername = this.updateUsername.bind(this);
        this.updateIconURL = this.updateIconURL.bind(this);
        this.updateDisplayName = this.updateDisplayName.bind(this);
        this.updateAutoComplete = this.updateAutoComplete.bind(this);
        this.updateAutoCompleteDesc = this.updateAutoCompleteDesc.bind(this);
        this.updateAutoCompleteHint = this.updateAutoCompleteHint.bind(this);

        this.state = {cmds: [], cmd: this.emptyCmd(), getCmdsComplete: false};
    }

    static propTypes() {
        return {
            intl: intlShape.isRequired
        };
    }

    emptyCmd() {
        var cmd = {};
        cmd.url = '';
        cmd.trigger = '';
        cmd.method = 'P';
        cmd.username = '';
        cmd.icon_url = '';
        cmd.auto_complete = false;
        cmd.auto_complete_desc = '';
        cmd.auto_complete_hint = '';
        cmd.display_name = '';
        return cmd;
    }

    componentDidMount() {
        this.getCmds();
    }

    addNewCmd(e) {
        e.preventDefault();

        if (this.state.cmd.trigger === '' || this.state.cmd.url === '') {
            return;
        }

        var cmd = this.state.cmd;
        if (cmd.trigger.length !== 0) {
            cmd.trigger = cmd.trigger.trim();
        }
        cmd.url = cmd.url.trim();

        Client.addCommand(
            cmd,
            (data) => {
                let cmds = Object.assign([], this.state.cmds);
                if (!cmds) {
                    cmds = [];
                }
                cmds.push(data);
                this.setState({cmds, addError: null, cmd: this.emptyCmd()});
            },
            (err) => {
                this.setState({addError: err.message});
            }
        );
    }

    removeCmd(id) {
        const data = {};
        data.id = id;

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

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

                this.setState({cmds});
            },
            (err) => {
                this.setState({editError: err.message});
            }
        );
    }

    regenToken(id) {
        const regenData = {};
        regenData.id = id;

        Client.regenCommandToken(
            regenData,
            (data) => {
                const cmds = Object.assign([], this.state.cmds);
                for (let i = 0; i < cmds.length; i++) {
                    if (cmds[i].id === id) {
                        cmds[i] = data;
                        break;
                    }
                }

                this.setState({cmds, editError: null});
            },
            (err) => {
                this.setState({editError: err.message});
            }
        );
    }

    getCmds() {
        Client.listTeamCommands(
            (data) => {
                if (data) {
                    this.setState({cmds: data, getCmdsComplete: true, editError: null});
                }
            },
            (err) => {
                this.setState({editError: err.message});
            }
        );
    }

    updateTrigger(e) {
        var cmd = this.state.cmd;
        cmd.trigger = e.target.value;
        this.setState(cmd);
    }

    updateURL(e) {
        var cmd = this.state.cmd;
        cmd.url = e.target.value;
        this.setState(cmd);
    }

    updateMethod(e) {
        var cmd = this.state.cmd;
        cmd.method = e.target.value;
        this.setState(cmd);
    }

    updateUsername(e) {
        var cmd = this.state.cmd;
        cmd.username = e.target.value;
        this.setState(cmd);
    }

    updateIconURL(e) {
        var cmd = this.state.cmd;
        cmd.icon_url = e.target.value;
        this.setState(cmd);
    }

    updateDisplayName(e) {
        var cmd = this.state.cmd;
        cmd.display_name = e.target.value;
        this.setState(cmd);
    }

    updateAutoComplete(e) {
        var cmd = this.state.cmd;
        cmd.auto_complete = e.target.checked;
        this.setState(cmd);
    }

    updateAutoCompleteDesc(e) {
        var cmd = this.state.cmd;
        cmd.auto_complete_desc = e.target.value;
        this.setState(cmd);
    }

    updateAutoCompleteHint(e) {
        var cmd = this.state.cmd;
        cmd.auto_complete_hint = e.target.value;
        this.setState(cmd);
    }

    render() {
        let addError;
        if (this.state.addError) {
            addError = <label className='has-error'>{this.state.addError}</label>;
        }

        let editError;
        if (this.state.editError) {
            addError = <label className='has-error'>{this.state.editError}</label>;
        }

        const cmds = [];
        this.state.cmds.forEach((cmd) => {
            let triggerDiv;
            if (cmd.trigger && cmd.trigger.length !== 0) {
                triggerDiv = (
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.trigger'
                                defaultMessage='Command Trigger Word: '
                            />
                        </strong>{cmd.trigger}
                    </div>
                );
            }

            cmds.push(
                <div
                    key={cmd.id}
                    className='webhook__item webcmd__item'
                >
                    {triggerDiv}
                    <div className='padding-top x2 webcmd__url'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.url'
                                defaultMessage='Request URL: '
                            />
                        </strong><span className='word-break--all'>{cmd.url}</span>
                    </div>
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.request_type'
                                defaultMessage='Request Method: '
                            />
                        </strong>
                        <span className='word-break--all'>
                            {
                                cmd.method === 'P' ?
                                <FormattedMessage
                                    id='user.settings.cmds.request_type_post'
                                    defaultMessage='POST'
                                /> :
                                <FormattedMessage
                                    id='user.settings.cmds.request_type_get'
                                    defaultMessage='GET'
                                />
                            }
                        </span>
                    </div>
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.username'
                                defaultMessage='Response Username: '
                            />
                        </strong><span className='word-break--all'>{cmd.username}</span>
                    </div>
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.icon_url'
                                defaultMessage='Response Icon: '
                            />
                        </strong><span className='word-break--all'>{cmd.icon_url}</span>
                    </div>
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete'
                                defaultMessage='Autocomplete: '
                            />
                        </strong><span className='word-break--all'>{cmd.auto_complete ? this.props.intl.formatMessage(holders.autocompleteYes) : this.props.intl.formatMessage(holders.autocompleteNo)}</span>
                    </div>
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete_hint'
                                defaultMessage='Autocomplete Hint: '
                            />
                        </strong><span className='word-break--all'>{cmd.auto_complete_hint}</span>
                    </div>
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete_desc'
                                defaultMessage='Autocomplete Description: '
                            />
                        </strong><span className='word-break--all'>{cmd.auto_complete_desc}</span>
                    </div>
                    <div className='padding-top x2'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.display_name'
                                defaultMessage='Descriptive Label: '
                            />
                        </strong><span className='word-break--all'>{cmd.display_name}</span>
                    </div>
                    <div className='padding-top'>
                        <strong>
                            <FormattedMessage
                                id='user.settings.cmds.token'
                                defaultMessage='Token: '
                            />
                        </strong>{cmd.token}
                    </div>
                    <div className='padding-top'>
                        <a
                            className='text-danger'
                            href='#'
                            onClick={this.regenToken.bind(this, cmd.id)}
                        >
                            <FormattedMessage
                                id='user.settings.cmds.regen'
                                defaultMessage='Regen Token'
                            />
                        </a>
                        <a
                            className='webhook__remove webcmd__remove'
                            href='#'
                            onClick={this.removeCmd.bind(this, cmd.id)}
                        >
                            <span aria-hidden='true'>{'×'}</span>
                        </a>
                    </div>
                    <div className='padding-top x2 divider-light'></div>
                </div>
            );
        });

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

        const existingCmds = (
            <div className='webhooks__container webcmds__container'>
                <label className='control-label padding-top x2'>
                    <FormattedMessage
                        id='user.settings.cmds.existing'
                        defaultMessage='Existing commands'
                    />
                </label>
                <div className='padding-top divider-light'></div>
                <div className='webhooks__list webcmds__list'>
                    {displayCmds}
                </div>
            </div>
        );

        const disableButton = this.state.cmd.trigger === '' || this.state.cmd.url === '';

        return (
            <div key='addCommandCmd'>
                <FormattedHTMLMessage
                    id='user.settings.cmds.add_desc'
                    defaultMessage='Create slash commands to send events to external integrations and receive a response. For example typing `/patient Joe Smith` could bring back search results from your internal health records management system for the name “Joe Smith”.  Please see <a href="http://docs.mattermost.com/developer/slash-commands.html">Slash commands documentation</a>  for detailed instructions. View all slash commands configured on this team below.'
                />
                <div><label className='control-label padding-top x2'>
                    <FormattedMessage
                        id='user.settings.cmds.add_new'
                        defaultMessage='Add a new command'
                    />
                </label></div>
                <div className='padding-top divider-light'></div>
                <div className='padding-top'>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.trigger'
                                defaultMessage='Command Trigger Word: '
                            />
                        </label>
                        <div className='padding-top'>
                            <input
                                ref='trigger'
                                className='form-control'
                                value={this.state.cmd.trigger}
                                onChange={this.updateTrigger}
                                placeholder={this.props.intl.formatMessage(holders.addTriggerPlaceholder)}
                            />
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.trigger_desc'
                                defaultMessage='Examples: /patient, /client, /employee Reserved: /echo, /join, /logout, /me, /shrug'
                            />
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.url'
                                defaultMessage='Request URL: '
                            />
                        </label>
                        <div className='padding-top'>
                        <input
                            ref='URL'
                            className='form-control'
                            value={this.state.cmd.url}
                            rows={1}
                            onChange={this.updateURL}
                            placeholder={this.props.intl.formatMessage(holders.adUrlPlaceholder)}
                        />
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.url_desc'
                                defaultMessage='The callback URL to receive the HTTP POST or GET event request when the slash command is run.'
                            />
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.request_type'
                                defaultMessage='Request Method: '
                            />
                        </label>
                        <div className='padding-top'>
                            <select
                                ref='method'
                                className='form-control'
                                value={this.state.cmd.method}
                                onChange={this.updateMethod}
                            >
                                <option value='P'>
                                    {this.props.intl.formatMessage(holders.requestTypePost)}
                                </option>
                                <option value='G'>
                                    {this.props.intl.formatMessage(holders.requestTypeGet)}
                                </option>
                            </select>
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.request_type_desc'
                                defaultMessage='The type of command request issued to the Request URL.'
                            />
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.username'
                                defaultMessage='Response Username: '
                            />
                        </label>
                        <div className='padding-top'>
                            <input
                                ref='username'
                                className='form-control'
                                value={this.state.cmd.username}
                                onChange={this.updateUsername}
                                placeholder={this.props.intl.formatMessage(holders.addUsernamePlaceholder)}
                            />
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.username_desc'
                                defaultMessage='Choose a username override for responses for this slash command. Usernames can consist of up to 22 characters consisting of lowercase letters, numbers and they symbols "-", "_", and "." .'
                            />
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.icon_url'
                                defaultMessage='Response Icon: '
                            />
                        </label>
                        <div className='padding-top'>
                            <input
                                ref='iconURL'
                                className='form-control'
                                value={this.state.cmd.icon_url}
                                onChange={this.updateIconURL}
                                placeholder='https://www.example.com/myicon.png'
                            />
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.icon_url_desc'
                                defaultMessage='Choose a profile picture override for the post responses to this slash command. Enter the URL of a .png or .jpg file at least 128 pixels by 128 pixels.'
                            />
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete'
                                defaultMessage='Autocomplete: '
                            />
                        </label>
                        <div className='padding-top'>
                            <div className='checkbox'>
                                <label>
                                    <input
                                        type='checkbox'
                                        checked={this.state.cmd.auto_complete}
                                        onChange={this.updateAutoComplete}
                                    />
                                    <FormattedMessage
                                        id='user.settings.cmds.auto_complete_help'
                                        defaultMessage=' Show this command in the autocomplete list.'
                                    />
                                </label>
                            </div>
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete_hint'
                                defaultMessage='Autocomplete Hint: '
                            />
                        </label>
                        <div className='padding-top'>
                            <input
                                ref='autoCompleteHint'
                                className='form-control'
                                value={this.state.cmd.auto_complete_hint}
                                onChange={this.updateAutoCompleteHint}
                                placeholder={this.props.intl.formatMessage(holders.addAutoCompleteHintPlaceholder)}
                            />
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete_hint_desc'
                                defaultMessage='Optional hint in the autocomplete list about parameters needed for command.'
                            />
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete_desc'
                                defaultMessage='Autocomplete Description: '
                            />
                        </label>
                        <div className='padding-top'>
                            <input
                                ref='autoCompleteDesc'
                                className='form-control'
                                value={this.state.cmd.auto_complete_desc}
                                onChange={this.updateAutoCompleteDesc}
                                placeholder={this.props.intl.formatMessage(holders.addAutoCompleteDescPlaceholder)}
                            />
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.auto_complete_desc_desc'
                                defaultMessage='Optional short description of slash command for the autocomplete list.'
                            />
                        </div>
                    </div>

                    <div className='padding-top x2'>
                        <label className='control-label'>
                            <FormattedMessage
                                id='user.settings.cmds.display_name'
                                defaultMessage='Descriptive Label: '
                            />
                        </label>
                        <div className='padding-top'>
                            <input
                                ref='displayName'
                                className='form-control'
                                value={this.state.cmd.display_name}
                                onChange={this.updateDisplayName}
                                placeholder={this.props.intl.formatMessage(holders.addDisplayNamePlaceholder)}
                            />
                        </div>
                        <div className='padding-top'>
                            <FormattedMessage
                                id='user.settings.cmds.cmd_display_name'
                                defaultMessage='Brief description of slash command to show in listings.'
                            />
                        </div>
                        {addError}
                    </div>

                    <div className='padding-top x2 padding-bottom'>
                        <a
                            className={'btn btn-sm btn-primary'}
                            href='#'
                            disabled={disableButton}
                            onClick={this.addNewCmd}
                        >
                            <FormattedMessage
                                id='user.settings.cmds.add'
                                defaultMessage='Add'
                            />
                        </a>
                    </div>
                </div>
                {existingCmds}
                {editError}
            </div>
        );
    }
}

export default injectIntl(ManageCommandCmds);