// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved. // See License.txt for license information. import LoadingScreen from '../loading_screen.jsx'; import ChannelStore from '../../stores/channel_store.jsx'; import * as Client from '../../utils/client.jsx'; import Constants from '../../utils/constants.jsx'; import {intlShape, injectIntl, defineMessages, FormattedMessage, FormattedHTMLMessage} from 'mm-intl'; const holders = defineMessages({ optional: { id: 'user.settings.hooks_out.optional', defaultMessage: 'Optional if channel selected' }, callbackHolder: { id: 'user.settings.hooks_out.callbackHolder', defaultMessage: 'Each URL must start with http:// or https://' }, select: { id: 'user.settings.hooks_out.select', defaultMessage: '--- Select a channel ---' } }); class ManageOutgoingHooks extends React.Component { constructor() { super(); this.getHooks = this.getHooks.bind(this); this.addNewHook = this.addNewHook.bind(this); this.updateChannelId = this.updateChannelId.bind(this); this.updateTriggerWords = this.updateTriggerWords.bind(this); this.updateCallbackURLs = this.updateCallbackURLs.bind(this); this.state = {hooks: [], channelId: '', triggerWords: '', callbackURLs: '', getHooksComplete: false}; } componentDidMount() { this.getHooks(); } addNewHook(e) { e.preventDefault(); if ((this.state.channelId === '' && this.state.triggerWords === '') || this.state.callbackURLs === '') { return; } const hook = {}; hook.channel_id = this.state.channelId; if (this.state.triggerWords.length !== 0) { hook.trigger_words = this.state.triggerWords.trim().split(','); } hook.callback_urls = this.state.callbackURLs.split('\n').map((url) => url.trim()); Client.addOutgoingHook( hook, (data) => { let hooks = Object.assign([], this.state.hooks); if (!hooks) { hooks = []; } hooks.push(data); this.setState({hooks, addError: null, channelId: '', triggerWords: '', callbackURLs: ''}); }, (err) => { this.setState({addError: err.message}); } ); } removeHook(id) { const data = {}; data.id = id; Client.deleteOutgoingHook( data, () => { const hooks = this.state.hooks; let index = -1; for (let i = 0; i < hooks.length; i++) { if (hooks[i].id === id) { index = i; break; } } if (index !== -1) { hooks.splice(index, 1); } this.setState({hooks}); }, (err) => { this.setState({editError: err.message}); } ); } regenToken(id) { const regenData = {}; regenData.id = id; Client.regenOutgoingHookToken( regenData, (data) => { const hooks = Object.assign([], this.state.hooks); for (let i = 0; i < hooks.length; i++) { if (hooks[i].id === id) { hooks[i] = data; break; } } this.setState({hooks, editError: null}); }, (err) => { this.setState({editError: err.message}); } ); } getHooks() { Client.listOutgoingHooks( (data) => { if (data) { this.setState({hooks: data, getHooksComplete: true, editError: null}); } }, (err) => { this.setState({editError: err.message}); } ); } updateChannelId(e) { this.setState({channelId: e.target.value}); } updateTriggerWords(e) { this.setState({triggerWords: e.target.value}); } updateCallbackURLs(e) { this.setState({callbackURLs: e.target.value}); } render() { let addError; if (this.state.addError) { addError = ; } let editError; if (this.state.editError) { addError = ; } const channels = ChannelStore.getAll(); const options = []; options.push( ); channels.forEach((channel) => { if (channel.type === Constants.OPEN_CHANNEL) { options.push( ); } }); const hooks = []; this.state.hooks.forEach((hook) => { const c = ChannelStore.get(hook.channel_id); if (!c && hook.channel_id && hook.channel_id.length !== 0) { return; } let channelDiv; if (c) { channelDiv = (