/* Copyright 2017 Michael Telatynski <7t3chguy@gmail.com> Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ import React from 'react'; import PropTypes from 'prop-types'; import sdk from '../../../index'; import SyntaxHighlight from '../elements/SyntaxHighlight'; import { _t } from '../../../languageHandler'; import MatrixClientPeg from '../../../MatrixClientPeg'; class DevtoolsComponent extends React.Component { static contextTypes = { roomId: PropTypes.string.isRequired, }; } class GenericEditor extends DevtoolsComponent { // static propTypes = {onBack: PropTypes.func.isRequired}; constructor(props, context) { super(props, context); this._onChange = this._onChange.bind(this); this.onBack = this.onBack.bind(this); } onBack() { if (this.state.message) { this.setState({ message: null }); } else { this.props.onBack(); } } _onChange(e) { this.setState({[e.target.id]: e.target.type === 'checkbox' ? e.target.checked : e.target.value}); } _buttons() { return
{ !this.state.message && }
; } 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) { 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.eventType === '') { this.setState({ message: _t('You must specify an event type!') }); return; } let message; try { const content = JSON.parse(this.state.evContent); await this.send(content); message = _t('Event sent!'); } catch (e) { message = _t('Failed to send custom event.') + ' (' + e.toString() + ')'; } this.setState({ message }); } render() { if (this.state.message) { return
{ this.state.message }
{ this._buttons() }
; } return
{ this.textInput('eventType', _t('Event Type')) } { this.state.isStateEvent && this.textInput('stateKey', _t('State Key')) }