From ea9b6300828af0d58c46e4c5202e0e52a9a5e4e4 Mon Sep 17 00:00:00 2001 From: Michael Telatynski <7t3chguy@gmail.com> Date: Mon, 30 Oct 2017 12:44:50 +0000 Subject: [PATCH 1/2] Refactor and add Account Data stuffs Signed-off-by: Michael Telatynski <7t3chguy@gmail.com> --- .../views/dialogs/DevtoolsDialog.js | 463 +++++++++++++----- src/i18n/strings/en_EN.json | 4 + .../views/dialogs/_DevtoolsDialog.scss | 103 ++++ 3 files changed, 457 insertions(+), 113 deletions(-) diff --git a/src/components/views/dialogs/DevtoolsDialog.js b/src/components/views/dialogs/DevtoolsDialog.js index 7760ea8418..2c854f99b0 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')) } -
+
+ +
-