diff --git a/embark-ui/src/components/Communication.js b/embark-ui/src/components/Communication.js new file mode 100644 index 000000000..8433d69eb --- /dev/null +++ b/embark-ui/src/components/Communication.js @@ -0,0 +1,119 @@ +import PropTypes from "prop-types"; +import React, {Component} from 'react'; +import {Button, Form} from 'tabler-react'; + +class Communication extends Component { + constructor(props) { + super(props); + + this.state = { + listenTo: '', + channel: '', + message: '', + subscribedChannels: [], + messageList: [], + logs: [] + }; + } + + handleChange(e, name) { + this.setState({ + [name]: e.target.value + }); + } + + sendMessage(e) { + e.preventDefault(); + this.props.sendMessage(this.state.channel, this.state.message); + this.addToLog("EmbarkJS.Messages.sendMessage({topic: '" + this.state.channel + "', data: '" + this.state.message + "'})"); + } + + listenToChannel(e) { + e.preventDefault(); + + const subscribedChannels = this.state.subscribedChannels; + subscribedChannels.push(Subscribed to {this.state.listenTo}. Now try sending a message); + this.setState({ + subscribedChannels + }); + + this.props.listenToMessages(this.state.listenTo); + this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})"); + } + + addToLog(txt) { + this.state.logs.push(txt); + this.setState({logs: this.state.logs}); + } + + render() { + return ( + +

Listen To channel

+ + + this.handleChange(e, 'listenTo')}/> + + +
+ {this.state.subscribedChannels.map((item, i) =>

{item}

)} +
+ {this.props.messages && this.props.messages.channels && Boolean(Object.keys(this.props.messages.channels).length) && + +

Messages received:

+
+ {Object.keys(this.props.messages.channels).map((channelName, i) => { + return ( +

{channelName}

+ {this.props.messages.channels[channelName].messages.map((message, f) => { + return

{message}

; + })} +
); + })} +
+
+ } +
+ +

Send Message

+ + + + this.handleChange(e, 'channel')}/> + + + this.handleChange(e, 'message')}/> + + + + + +

Javascript calls being made:

+
+

EmbarkJS.Messages.setProvider('whisper')

+ { + this.state.logs.map((item, i) =>

{item}

) + } +
+
+ ); + } +} + +Communication.propTypes = { + sendMessage: PropTypes.func, + listenToMessages: PropTypes.func, + messages: PropTypes.object +}; + +export default Communication; + diff --git a/embark-ui/src/containers/CommunicationContainer.js b/embark-ui/src/containers/CommunicationContainer.js index 53e131c28..74ecc455e 100644 --- a/embark-ui/src/containers/CommunicationContainer.js +++ b/embark-ui/src/containers/CommunicationContainer.js @@ -1,51 +1,17 @@ import PropTypes from "prop-types"; import React, {Component} from 'react'; import connect from "react-redux/es/connect/connect"; -import {Alert, Button, Form, Icon} from 'tabler-react'; +import {Alert, Icon} from 'tabler-react'; import {messageSend, messageListen} from "../actions"; +import Communication from "../components/Communication"; class CommunicationContainer extends Component { - constructor(props) { - super(props); - - this.state = { - listenTo: '', - channel: '', - message: '', - subscribedChannels: [], - messageList: [], - logs: [] - }; + sendMessage(topic, message) { + this.props.messageSend({topic, message}); } - handleChange(e, name) { - this.setState({ - [name]: e.target.value - }); - } - - sendMessage(e) { - e.preventDefault(); - this.props.messageSend({topic: this.state.channel, message: this.state.message}); - this.addToLog("EmbarkJS.Messages.sendMessage({topic: '" + this.state.channel + "', data: '" + this.state.message + "'})"); - } - - listenToChannel(e) { - e.preventDefault(); - - const subscribedChannels = this.state.subscribedChannels; - subscribedChannels.push(Subscribed to {this.state.listenTo}. Now try sending a message); - this.setState({ - subscribedChannels - }); - - this.props.messageListen(this.state.listenTo); - this.addToLog("EmbarkJS.Messages.listenTo({topic: ['" + this.state.listenTo + "']}).then(function(message) {})"); - } - - addToLog(txt) { - this.state.logs.push(txt); - this.setState({logs: this.state.logs}); + listenToChannel(channel) { + this.props.messageListen(channel); } render() { @@ -56,67 +22,16 @@ class CommunicationContainer extends Component { The node uses an unsupported version of Whisper ; } else if (!this.enabled) { - isEnabledMessage = Checking Whisper support, please wait; + isEnabledMessage = + Checking Whisper support, please wait; } return ( {isEnabledMessage} -

Listen To channel

- - - this.handleChange(e, 'listenTo')}/> - - -
- {this.state.subscribedChannels.map((item, i) =>

{item}

)} -
- {this.props.messages && this.props.messages.channels && Boolean(Object.keys(this.props.messages.channels).length) && - -

Messages received:

-
- {Object.keys(this.props.messages.channels).map((channelName, i) => { - return ( -

{channelName}

- {this.props.messages.channels[channelName].messages.map((message, f) => { - return

{message}

; - })} -
); - })} -
-
- } -
- -

Send Message

- - - - this.handleChange(e, 'channel')}/> - - - this.handleChange(e, 'message')}/> - - - - - -

Javascript calls being made:

-
-

EmbarkJS.Messages.setProvider('whisper')

- { - this.state.logs.map((item, i) =>

{item}

) - } -
+ this.listenToChannel(channel)} + sendMessage={(channel, message) => this.sendMessage(channel, message)} + messages={this.props.messages}/>
); }