// 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'; 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}; } 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 = ; } let editError; if (this.state.editError) { addError = ; } const cmds = []; this.state.cmds.forEach((cmd) => { let triggerDiv; if (cmd.trigger && cmd.trigger.length !== 0) { triggerDiv = (
{'Trigger: '}{cmd.trigger}
); } cmds.push(
{'Display Name: '}{cmd.display_name}
{'Username: '}{cmd.username}
{'Icon URL: '}{cmd.icon_url}
{'Auto Complete: '}{cmd.auto_complete ? 'yes' : 'no'}
{'Auto Complete Description: '}{cmd.auto_complete_desc}
{'Auto Complete Hint: '}{cmd.auto_complete_hint}
{'Request Type: '}{cmd.method === 'P' ? 'POST' : 'GET'}
{'URL: '}{cmd.url}
{triggerDiv}
{'Token: '}{cmd.token}
{'Regen Token'}
); }); let displayCmds; if (!this.state.getCmdsComplete) { displayCmds = ; } else if (cmds.length > 0) { displayCmds = cmds; } else { displayCmds =
{'None'}
; } const existingCmds = (
{displayCmds}
); const disableButton = this.state.cmd.trigger === '' || this.state.cmd.url === ''; return (
{'Create commands to send new message events to an external integration. Please see '} {'http://mattermost.org/commands'} {' to learn more.'}
{'Command display name.'}
{'The username to use when overriding the post.'}
{'URL to an icon'}
{'/'}
{'Word to trigger on'}
{'Show this command in autocomplete list.'}
{'A short description of what this commands does.'}
{'List parameters to be passed to the command.'}
{'Command request type issued to the callback URL.'}
{'URL that will receive the HTTP POST or GET event'}
{addError}
{'Add'}
{existingCmds} {editError}
); } }