diff options
author | =Corey Hulen <corey@hulen.com> | 2015-09-22 13:18:42 -0700 |
---|---|---|
committer | =Corey Hulen <corey@hulen.com> | 2015-09-22 13:18:42 -0700 |
commit | 6e60768abe6f0caa639febf068d718d62881ce62 (patch) | |
tree | 0600c72a7a9a3f7c1df167b3f870b38863e9a564 /web/react/components/user_settings/manage_incoming_hooks.jsx | |
parent | e22e7b8b7b66f342c2df693bbfc06a85980d253e (diff) | |
parent | ac7918c5540900ab0dbe43d61b8c1155e4279b55 (diff) | |
download | chat-6e60768abe6f0caa639febf068d718d62881ce62.tar.gz chat-6e60768abe6f0caa639febf068d718d62881ce62.tar.bz2 chat-6e60768abe6f0caa639febf068d718d62881ce62.zip |
Fixing merge conflict
Diffstat (limited to 'web/react/components/user_settings/manage_incoming_hooks.jsx')
-rw-r--r-- | web/react/components/user_settings/manage_incoming_hooks.jsx | 177 |
1 files changed, 177 insertions, 0 deletions
diff --git a/web/react/components/user_settings/manage_incoming_hooks.jsx b/web/react/components/user_settings/manage_incoming_hooks.jsx new file mode 100644 index 000000000..df089a403 --- /dev/null +++ b/web/react/components/user_settings/manage_incoming_hooks.jsx @@ -0,0 +1,177 @@ +// Copyright (c) 2015 Spinpunch, Inc. All Rights Reserved. +// See License.txt for license information. + +var Client = require('../../utils/client.jsx'); +var Utils = require('../../utils/utils.jsx'); +var Constants = require('../../utils/constants.jsx'); +var ChannelStore = require('../../stores/channel_store.jsx'); +var LoadingScreen = require('../loading_screen.jsx'); + +export default class ManageIncomingHooks extends React.Component { + constructor() { + super(); + + this.getHooks = this.getHooks.bind(this); + this.addNewHook = this.addNewHook.bind(this); + this.updateChannelId = this.updateChannelId.bind(this); + + this.state = {hooks: [], channelId: ChannelStore.getByName(Constants.DEFAULT_CHANNEL).id, getHooksComplete: false}; + } + componentDidMount() { + this.getHooks(); + } + addNewHook() { + let hook = {}; //eslint-disable-line prefer-const + hook.channel_id = this.state.channelId; + + Client.addIncomingHook( + hook, + (data) => { + let hooks = this.state.hooks; + if (!hooks) { + hooks = []; + } + hooks.push(data); + this.setState({hooks}); + }, + (err) => { + this.setState({serverError: err}); + } + ); + } + removeHook(id) { + let data = {}; //eslint-disable-line prefer-const + data.id = id; + + Client.deleteIncomingHook( + data, + () => { + let hooks = this.state.hooks; //eslint-disable-line prefer-const + 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({serverError: err}); + } + ); + } + getHooks() { + Client.listIncomingHooks( + (data) => { + let state = this.state; //eslint-disable-line prefer-const + + if (data) { + state.hooks = data; + } + + state.getHooksComplete = true; + this.setState(state); + }, + (err) => { + this.setState({serverError: err}); + } + ); + } + updateChannelId(e) { + this.setState({channelId: e.target.value}); + } + render() { + let serverError; + if (this.state.serverError) { + serverError = <label className='has-error'>{this.state.serverError}</label>; + } + + const channels = ChannelStore.getAll(); + let options = []; //eslint-disable-line prefer-const + channels.forEach((channel) => { + options.push(<option value={channel.id}>{channel.name}</option>); + }); + + let disableButton = ''; + if (this.state.channelId === '') { + disableButton = ' disable'; + } + + let hooks = []; //eslint-disable-line prefer-const + this.state.hooks.forEach((hook) => { + const c = ChannelStore.get(hook.channel_id); + hooks.push( + <div> + <div className='divider-light'></div> + <span> + <strong>{'URL: '}</strong>{Utils.getWindowLocationOrigin() + '/hooks/' + hook.id} + </span> + <br/> + <span> + <strong>{'Channel: '}</strong>{c.name} + </span> + <br/> + <a + className={'btn btn-sm btn-primary'} + href='#' + onClick={this.removeHook.bind(this, hook.id)} + > + {'Remove'} + </a> + </div> + ); + }); + + let displayHooks; + if (!this.state.getHooksComplete) { + displayHooks = <LoadingScreen/>; + } else if (hooks.length > 0) { + displayHooks = hooks; + } else { + displayHooks = <label>{'None'}</label>; + } + + const existingHooks = ( + <div> + <label className='control-label'>{'Existing incoming webhooks'}</label> + <br/> + {displayHooks} + </div> + ); + + return ( + <div + key='addIncomingHook' + className='form-group' + > + <label className='control-label'>{'Add a new incoming webhook'}</label> + <br/> + <div> + <select + ref='channelName' + value={this.state.channelId} + onChange={this.updateChannelId} + > + {options} + </select> + <br/> + {serverError} + <a + className={'btn btn-sm btn-primary' + disableButton} + href='#' + onClick={this.addNewHook} + > + {'Add'} + </a> + </div> + {existingHooks} + </div> + ); + } +} |