OSDN Git Service

cp code from chain
[bytom/bytom-electron.git] / src / features / app / components / Container.jsx
1 import React from 'react'
2 import { connect } from 'react-redux'
3 import actions from 'actions'
4 import { Main, Config, Login, Loading, Modal } from './'
5
6 const CORE_POLLING_TIME = 2 * 1000
7 const TESTNET_INFO_POLLING_TIME = 30 * 1000
8
9 class Container extends React.Component {
10   constructor(props) {
11     super(props)
12     this.redirectRoot = this.redirectRoot.bind(this)
13   }
14
15   redirectRoot(props) {
16     const {
17       authOk,
18       configKnown,
19       configured,
20       location
21     } = props
22
23     if (!authOk || !configKnown) {
24       return
25     }
26
27     if (configured) {
28       if (location.pathname === '/' ||
29           location.pathname.indexOf('configuration') >= 0) {
30         this.props.showRoot()
31       }
32     } else {
33       this.props.showConfiguration()
34     }
35   }
36
37   componentWillMount() {
38     const checkTestnet = () => {
39       if (this.props.onTestnet) this.props.fetchTestnetInfo()
40     }
41
42     this.props.fetchInfo().then(() => {
43       checkTestnet()
44       this.redirectRoot(this.props)
45     })
46
47     setInterval(() => this.props.fetchInfo(), CORE_POLLING_TIME)
48     setInterval(() => checkTestnet(), TESTNET_INFO_POLLING_TIME)
49   }
50
51   componentWillReceiveProps(nextProps) {
52     if (nextProps.authOk != this.props.authOk ||
53         nextProps.configKnown != this.props.configKnown ||
54         nextProps.configured != this.props.configured ||
55         nextProps.location.pathname != this.props.location.pathname) {
56       this.redirectRoot(nextProps)
57     }
58   }
59
60   render() {
61     let layout
62
63     if (!this.props.authOk) {
64       layout = <Login />
65     } else if (!this.props.configKnown) {
66       return <Loading>Connecting to Chain Core...</Loading>
67     } else if (!this.props.configured) {
68       layout = <Config>{this.props.children}</Config>
69     } else {
70       layout = <Main>{this.props.children}</Main>
71     }
72
73     return <div>
74       {layout}
75       <Modal />
76
77       {/* For copyToClipboard(). TODO: move this some place cleaner. */}
78       <input
79         id='_copyInput'
80         onChange={() => 'do nothing'}
81         value='dummy'
82         style={{display: 'none'}}
83       />
84     </div>
85   }
86 }
87
88 export default connect(
89   (state) => ({
90     authOk: !state.core.requireClientToken || state.core.validToken,
91     configKnown: state.core.configKnown,
92     configured: state.core.configured,
93     onTestnet: state.core.onTestnet,
94   }),
95   (dispatch) => ({
96     fetchInfo: options => dispatch(actions.core.fetchCoreInfo(options)),
97     fetchTestnetInfo: () => dispatch(actions.testnet.fetchTestnetInfo()),
98     showRoot: () => dispatch(actions.app.showRoot),
99     showConfiguration: () => dispatch(actions.app.showConfiguration()),
100   })
101 )(Container)