Skip to content

Commit

Permalink
DRY up code and change flow to be less destructive
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Telatynski <[email protected]>
  • Loading branch information
t3chguy committed Aug 10, 2017
1 parent 3fd1a36 commit 84f4fbe
Showing 1 changed file with 56 additions and 64 deletions.
120 changes: 56 additions & 64 deletions src/components/views/dialogs/DevtoolsDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.

import React from 'react';
import sdk from 'matrix-react-sdk';
import Modal from 'matrix-react-sdk/lib/Modal';
import MatrixClientPeg from 'matrix-react-sdk/lib/MatrixClientPeg';

class SendCustomEvent extends React.Component {
Expand All @@ -28,33 +27,56 @@ class SendCustomEvent extends React.Component {
constructor(props, context) {
super(props, context);
this._send = this._send.bind(this);
this.onBack = this.onBack.bind(this);
}

state = {
message: null,
};

onBack() {
if (this.state.message) {
this.setState({ message: null });
} else {
this.props.onBack();
}
}

_buttons() {
return <div className="mx_Dialog_buttons">
<button onClick={this.onBack}>Back</button>
{!this.state.message && <button onClick={this._send}>Send</button>}
</div>;
}

send(content) {
return MatrixClientPeg.get().sendEvent(this.props.roomId, this.refs.eventType.value, content);
}

async _send() {
let message;
try {
const content = JSON.parse(this.refs.evContent.value);
await MatrixClientPeg.get().sendEvent(this.refs.roomId.value, this.refs.eventType.value, content);
this.props.onFinished(true);
await this.send(content);
message = 'Event sent!';
} catch (e) {
this.props.onFinished(false);
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
Modal.createDialog(ErrorDialog, {
title: 'Failed to send custom event',
description: e.toString(),
});
message = 'Failed to send custom event.' + ` (${e.toString()})`;
}
this.setState({ message });
}

render() {
return <div>
<div className="mx_Dialog_content">
<div className="mx_TextInputDialog_label">
<label htmlFor="roomId"> Room ID </label>
</div>
<div>
<input id="roomId" ref="roomId" className="mx_TextInputDialog_input" defaultValue={this.props.roomId} size="64" />
if (this.state.message) {
return <div>
<div className="mx_Dialog_content">
{this.state.message}
</div>
{this._buttons()}
</div>;
}

return <div>
<div className="mx_Dialog_content">
<div className="mx_TextInputDialog_label">
<label htmlFor="eventType"> Event Type </label>
</div>
Expand All @@ -66,55 +88,32 @@ class SendCustomEvent extends React.Component {
<label htmlFor="evContent"> Event Content </label>
</div>
<div>
<textarea id="evContent" ref="evContent" className="mx_TextInputDialog_input" defaultValue={"{\n\n}"} cols="64" />
<textarea id="evContent" ref="evContent" className="mx_TextInputDialog_input" defaultValue={"{\n\n}"} cols="63" rows="5" />
</div>
</div>
<div className="mx_Dialog_buttons">
<button onClick={this.props.onBack}>Back</button>
<button onClick={this._send}>Send</button>
</div>
{this._buttons()}
</div>;
}
}

class SendCustomStateEvent extends React.Component {
static propTypes = {
roomId: React.PropTypes.string.isRequired,
onBack: React.PropTypes.func.isRequired,
};

constructor(props, context) {
super(props, context);
this._send = this._send.bind(this);
class SendCustomStateEvent extends SendCustomEvent {
send(content) {
return MatrixClientPeg.get().sendStateEvent(this.props.roomId, this.refs.eventType.value, content,
this.refs.stateKey.value);
}

async _send() {
try {
const content = JSON.parse(this.refs.evContent.value);
await MatrixClientPeg.get().sendStateEvent(this.refs.roomId.value, this.refs.eventType.value, content,
this.refs.stateKey.value);

this.props.onFinished(true);
} catch (e) {
this.props.onFinished(false);
const ErrorDialog = sdk.getComponent('dialogs.ErrorDialog');
Modal.createDialog(ErrorDialog, {
title: 'Failed to send custom state event',
description: e.toString(),
});
render() {
if (this.state.message) {
return <div>
<div className="mx_Dialog_content">
{this.state.message}
</div>
{this._buttons()}
</div>;
}
}

render() {
return <div>
<div className="mx_Dialog_content">
<div className="mx_TextInputDialog_label">
<label htmlFor="roomId"> Room ID </label>
</div>
<div>
<input id="roomId" ref="roomId" className="mx_TextInputDialog_input" defaultValue={this.props.roomId} size="64" />
</div>

<div className="mx_TextInputDialog_label">
<label htmlFor="stateKey"> State Key </label>
</div>
Expand All @@ -133,13 +132,10 @@ class SendCustomStateEvent extends React.Component {
<label htmlFor="evContent"> Event Content </label>
</div>
<div>
<textarea id="evContent" ref="evContent" className="mx_TextInputDialog_input" defaultValue={"{\n\n}"} cols="64" />
<textarea id="evContent" ref="evContent" className="mx_TextInputDialog_input" defaultValue={"{\n\n}"} cols="63" rows="5" />
</div>
</div>
<div className="mx_Dialog_buttons">
<button onClick={this.props.onBack}>Back</button>
<button onClick={this._send}>Send</button>
</div>
{this._buttons()}
</div>;
}
}
Expand Down Expand Up @@ -227,12 +223,7 @@ class RoomStateExplorer extends React.Component {

return <div>
<div className="mx_Dialog_content">
<div className="mx_TextInputDialog_label">
Room ID: {this.props.roomId}
</div>
<div>
{rows}
</div>
{rows}
</div>
<div className="mx_Dialog_buttons">
<button onClick={this.onBack}>Back</button>
Expand Down Expand Up @@ -295,6 +286,7 @@ export default class DevtoolsDialog extends React.Component {

const BaseDialog = sdk.getComponent('views.dialogs.BaseDialog');
return <BaseDialog className="mx_QuestionDialog" onFinished={this.props.onFinished} title="Developer Tools">
<div>Room ID: {this.props.roomId}</div>
{ body }
</BaseDialog>;
}
Expand Down

0 comments on commit 84f4fbe

Please sign in to comment.