/ embark-ui / src / containers / CommunicationContainer.js
CommunicationContainer.js
 1  import PropTypes from "prop-types";
 2  import React, {Component} from 'react';
 3  import connect from "react-redux/es/connect/connect";
 4  import { Alert } from 'reactstrap';
 5  import {messageSend, messageListen} from "../actions";
 6  import Communication from "../components/Communication";
 7  import {getMessages, getMessageChannels, isOldWeb3, isWeb3Enabled} from "../reducers/selectors";
 8  
 9  class CommunicationContainer extends Component {
10    sendMessage(topic, message) {
11      this.props.messageSend({topic, message});
12    }
13  
14    listenToChannel(channel) {
15      this.props.messageListen(channel);
16    }
17  
18    web3DisabledWarning() {
19      return <Alert color="danger">The node you are using does not support Whisper</Alert>;
20    }
21  
22    web3Enabled() {
23      return this.props.isOldWeb3 ? this.web3DisabledWarning() : this.showCommunication();
24    }
25  
26    web3OldWarning() {
27      return <Alert color="danger">The node uses an unsupported version of Whisper</Alert>;
28    }
29  
30    showCommunication() {
31      return <Communication listenToMessages={(channel) => this.listenToChannel(channel)}
32                            sendMessage={(channel, message) => this.sendMessage(channel, message)}
33                            channels={this.props.messages}
34                            subscriptions={this.props.messageChannels}/>;
35    }
36  
37    render() {
38      return (
39        <React.Fragment>
40          {this.props.isWeb3Enabled ? this.web3Enabled() : this.web3DisabledWarning()}
41        </React.Fragment>
42      );
43    }
44  }
45  
46  CommunicationContainer.propTypes = {
47    messageSend: PropTypes.func,
48    messageListen: PropTypes.func,
49    isOldWeb3: PropTypes.bool,
50    isWeb3Enabled: PropTypes.bool,
51    messages: PropTypes.object,
52    messageChannels: PropTypes.array
53  };
54  
55  function mapStateToProps(state) {
56    return {
57      messages: getMessages(state),
58      messageChannels: getMessageChannels(state),
59      isOldWeb3: isOldWeb3(state),
60      isWeb3Enabled: isWeb3Enabled(state)
61    };
62  }
63  
64  export default connect(
65    mapStateToProps,
66    {
67      messageSend: messageSend.request,
68      messageListen: messageListen.request
69    }
70  )(CommunicationContainer);
71