/* 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 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, }; static defaultProps = { eventType: '', evContent: '{\n\n}', }; 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, }; } onBack() { if (this.state.message) { this.setState({ message: null }); } else { this.props.onBack(); } } _buttons() { return
{!this.state.message && }
; } send(content) { return MatrixClientPeg.get().sendEvent(this.props.roomId, this.state.input_eventType, content); } async _send() { if (this.state.input_eventType === '') { this.setState({ message: _t('You must specify an event type!') }); return; } let message; try { const content = JSON.parse(this.state.input_evContent); await this.send(content); message = _t('Event sent!'); } catch (e) { message = _t('Failed to send custom event.') + ' (' + e.toString() + ')'; } this.setState({ message }); } _additionalFields() { return
; } _onChange(e) { this.setState({[`input_${e.target.id}`]: e.target.value}); } render() { if (this.state.message) { return
{this.state.message}
{this._buttons()}
; } return
{this._additionalFields()}