diff --git a/src/components/views/dialogs/DevtoolsDialog.js b/src/components/views/dialogs/DevtoolsDialog.js index 7760ea84..a7155ad1 100644 --- a/src/components/views/dialogs/DevtoolsDialog.js +++ b/src/components/views/dialogs/DevtoolsDialog.js @@ -15,35 +15,24 @@ limitations under the License. */ import React from 'react'; +import PropTypes from 'prop-types'; import sdk from 'matrix-react-sdk'; import { _t } from 'matrix-react-sdk/lib/languageHandler'; import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg'; -class SendCustomEvent extends React.Component { - static propTypes = { - roomId: React.PropTypes.string.isRequired, - onBack: React.PropTypes.func.isRequired, - - eventType: React.PropTypes.string.isRequired, - evContent: React.PropTypes.string.isRequired, +class DevtoolsComponent extends React.Component { + static contextTypes = { + roomId: PropTypes.string.isRequired, }; +} - static defaultProps = { - eventType: '', - evContent: '{\n\n}', - }; +class GenericEditor extends DevtoolsComponent { + // static propTypes = {onBack: PropTypes.func.isRequired}; constructor(props, context) { super(props, context); - this._send = this._send.bind(this); - this.onBack = this.onBack.bind(this); this._onChange = this._onChange.bind(this); - - this.state = { - message: null, - input_eventType: this.props.eventType, - input_evContent: this.props.evContent, - }; + this.onBack = this.onBack.bind(this); } onBack() { @@ -54,6 +43,10 @@ class SendCustomEvent extends React.Component { } } + _onChange(e) { + this.setState({[e.target.id]: e.target.type === 'checkbox' ? e.target.checked : e.target.value}); + } + _buttons() { return
@@ -61,19 +54,64 @@ class SendCustomEvent extends React.Component {
; } + textInput(id, label) { + return
+
+ +
+
+ +
+
; + } +} + +class SendCustomEvent extends GenericEditor { + static getLabel() { return _t('Send Custom Event'); } + + static propTypes = { + onBack: PropTypes.func.isRequired, + forceStateEvent: PropTypes.bool, + inputs: PropTypes.object, + }; + + constructor(props, context) { + super(props, context); + this._send = this._send.bind(this); + + const {eventType, stateKey, evContent} = Object.assign({ + eventType: '', + stateKey: '', + evContent: '{\n\n}', + }, this.props.inputs); + + this.state = { + isStateEvent: Boolean(this.props.forceStateEvent), + + eventType, + stateKey, + evContent, + }; + } + send(content) { - return MatrixClientPeg.get().sendEvent(this.props.roomId, this.state.input_eventType, content); + const cli = MatrixClientPeg.get(); + if (this.state.isStateEvent) { + return cli.sendStateEvent(this.context.roomId, this.state.eventType, content, this.state.stateKey); + } else { + return cli.sendEvent(this.context.roomId, this.state.eventType, content); + } } async _send() { - if (this.state.input_eventType === '') { + if (this.state.eventType === '') { this.setState({ message: _t('You must specify an event type!') }); return; } let message; try { - const content = JSON.parse(this.state.input_evContent); + const content = JSON.parse(this.state.evContent); await this.send(content); message = _t('Event sent!'); } catch (e) { @@ -82,14 +120,6 @@ class SendCustomEvent extends React.Component { this.setState({ message }); } - _additionalFields() { - return
; - } - - _onChange(e) { - this.setState({[`input_${e.target.id}`]: e.target.value}); - } - render() { if (this.state.message) { return
@@ -102,87 +132,176 @@ class SendCustomEvent extends React.Component { return
- { this._additionalFields() } -
- -
-
- -
+ { this.textInput('eventType', _t('Event Type')) } + { this.state.isStateEvent && this.textInput('stateKey', _t('State Key')) } -
+
+ +
-