OSDN Git Service

import dashboard UI.
[bytom/bytom-electron.git] / src / features / core / components / CoreIndex / CoreIndex.jsx
1 import { connect } from 'react-redux'
2 import {DropdownButton, MenuItem} from 'react-bootstrap'
3 import componentClassNames from 'utility/componentClassNames'
4 import { PageContent, PageTitle, ConsoleSection } from 'features/shared/components'
5 import React from 'react'
6 import styles from './CoreIndex.scss'
7 import actions from 'actions'
8 import {withNamespaces} from 'react-i18next'
9
10
11 class CoreIndex extends React.Component {
12   constructor(props) {
13     super(props)
14     this.state = {
15       btmAmountUnit: 'BTM'
16     }
17     this.handleMiningState = this.handleMiningState.bind(this)
18     this.handleAdvancedOptionChange = this.handleAdvancedOptionChange.bind(this)
19     this.changeBTMamount = this.changeBTMamount.bind(this)
20     this.consolePopup = this.consolePopup.bind(this)
21   }
22
23   componentDidMount() {
24     if(window.ipcRenderer){
25       window.ipcRenderer.on('btmAmountUnitState', (event, arg) => {
26         this.props.uptdateBtmAmountUnit(arg)
27       })
28     }
29   }
30
31   changeBTMamount(value) {
32     this.setState({ btmAmountUnit: value })
33     this.props.uptdateBtmAmountUnit(value)
34   }
35
36   handleAdvancedOptionChange(event) {
37     const target = event.target
38     if( target.checked ){
39       this.props.showNavAdvanced()
40     }else{
41       this.props.hideNavAdvanced()
42     }
43   }
44
45   handleMiningState(event){
46     const target = event.target
47     if( target.checked ){
48       this.props.updateMiningState(true)
49     }else{
50       this.props.updateMiningState(false)
51     }
52   }
53
54   consolePopup(e) {
55     e.preventDefault()
56     this.props.showModal(
57       <ConsoleSection
58         cmd={this.props.cmd}
59       />
60     )
61   }
62
63   render() {
64     let navState = this.props.navAdvancedState === 'advance'
65     let miningState = this.props.core.mingStatus
66     let coreData = this.props.core.coreData
67
68     const t = this.props.t
69
70     let configBlock = (
71       <div className={[styles.left, styles.col].join(' ')}>
72         <div>
73           <h4>{t('coreIndex.configuration')}</h4>
74           <table className={styles.table}>
75             <tbody>
76             <tr className={styles.row}>
77               <td className={styles.row_label}>{t('coreIndex.version')}:</td>
78               <td><code>{coreData? coreData['versionInfo']['version']: null}</code></td>
79             </tr>
80             <tr className={styles.row}>
81               <td className={styles.row_label}>{t('commonWords.language')}:</td>
82               <td>{t('languageFull')}</td>
83             </tr>
84             <tr className={styles.row}>
85               <td colSpan={2}><hr /></td>
86             </tr>
87             <tr className={styles.row}>
88               <td className={styles.row_label}>{t('coreIndex.advanced')}: </td>
89               <td>
90                 <label className={styles.switch}>
91                   <input
92                     type='checkbox'
93                     onChange={this.handleAdvancedOptionChange}
94                     checked={navState}
95                   />
96                   <span className={styles.slider}></span>
97                 </label>
98               </td>
99             </tr>
100             <tr className={styles.row}>
101               <td className={styles.row_label}>{t('coreIndex.mining')}: </td>
102               <td>
103                 <label className={styles.switch}>
104                   <input
105                     type='checkbox'
106                     onChange={this.handleMiningState}
107                     checked={miningState}
108                   />
109                   <span className={styles.slider}></span>
110                 </label>
111               </td>
112             </tr>
113             <tr className={styles.row}>
114               <td className={styles.row_label} >{t('coreIndex.unit')} </td>
115               <td>
116                 <DropdownButton
117                   bsSize='xsmall'
118                   id='input-dropdown-amount'
119                   title={this.props.core.btmAmountUnit || this.state.btmAmountUnit}
120                   onSelect={this.changeBTMamount}
121                 >
122                   <MenuItem eventKey='BTM'>BTM</MenuItem>
123                   <MenuItem eventKey='mBTM'>mBTM</MenuItem>
124                   <MenuItem eventKey='NEU'>NEU</MenuItem>
125                 </DropdownButton>
126               </td>
127             </tr>
128             </tbody>
129           </table>
130         </div>
131       </div>
132     )
133
134     let requestStatusBlock
135
136
137     if(!coreData){
138       requestStatusBlock = (<div>loading...</div>)
139     }else {
140       requestStatusBlock = (
141         <div>
142           <h4>{t('coreIndex.networkStatus')}</h4>
143           <table className={styles.table}>
144             <tbody>
145             <tr className={styles.row} key={'core-listening'}>
146               <td className={styles.row_label}> {t('coreIndex.listening')}:</td>
147               <td className={styles.row_value}><code>{(coreData['listening'])? t('coreIndex.connect'): t('coreIndex.disConnect')}</code></td>
148             </tr>
149             <tr className={styles.row} key={'core-syncing'}>
150               <td className={styles.row_label}> {t('coreIndex.syncStatus')}:</td>
151               <td className={styles.row_value}><code>{(coreData['syncing'])? t('coreIndex.synchronizing'): t('coreIndex.synced')}</code></td>
152             </tr>
153             <tr className={styles.row} key={'core-mining'}>
154               <td className={styles.row_label}> {t('coreIndex.miningStatus')}:</td>
155               <td className={styles.row_value}><code>{(coreData['mining'])? t('coreIndex.miningRuning'): t('coreIndex.miningStopped')}</code></td>
156             </tr>
157             <tr className={styles.row} key={'core-peerCount'}>
158               <td className={styles.row_label}> {t('coreIndex.peer')}:</td>
159               <td className={styles.row_value}><code>{String(coreData['peerCount'])}</code></td>
160             </tr>
161             <tr className={styles.row} key={'core-currentBlock'}>
162               <td className={styles.row_label}> {t('coreIndex.currentBlock')}:</td>
163               <td className={styles.row_value}><code>{String(coreData['currentBlock'])}</code></td>
164             </tr>
165             <tr className={styles.row} key={'core-highestBlock'}>
166               <td className={styles.row_label}> {t('coreIndex.highestBlock')}:</td>
167               <td className={styles.row_value}><code>{String(coreData['highestBlock'])}</code></td>
168             </tr>
169             <tr className={styles.row} key={'core-networkID'}>
170               <td className={styles.row_label}> {t('coreIndex.networkId')}:</td>
171               <td className={styles.row_value}><code>{String(coreData['networkId'])}</code></td>
172             </tr>
173             </tbody>
174           </table>
175         </div>
176       )
177     }
178
179     let networkStatusBlock = (
180       <div className={[styles.right, styles.col].join(' ')}>
181         {/*<div ref='requestComponent'>*/}
182           {requestStatusBlock}
183         {/*</div>*/}
184       </div>
185     )
186
187     return (
188       <div className={componentClassNames(this, 'flex-container')}>
189         <PageTitle
190           title={t('coreIndex.coreStatus')}
191           actions={[
192             <button className='btn btn-link' onClick={this.consolePopup}>
193               <img src={require('images/console-window.svg')}/>
194             </button>
195           ]}
196         />
197
198         <PageContent>
199           <div className={`${styles.flex} ${styles.mainContainer}`}>
200             {configBlock}
201             {networkStatusBlock}
202           </div>
203         </PageContent>
204       </div>
205     )
206   }
207 }
208
209 const mapStateToProps = (state) => ({
210   core: state.core,
211   navAdvancedState: state.app.navAdvancedState,
212 })
213
214 const mapDispatchToProps = (dispatch) => ({
215   showNavAdvanced: () => dispatch(actions.app.showNavAdvanced),
216   hideNavAdvanced: () => dispatch(actions.app.hideNavAdvanced),
217   uptdateBtmAmountUnit: (param) => dispatch(actions.core.updateBTMAmountUnit(param)),
218   updateMiningState: (param) => dispatch(actions.core.updateMiningState(param)),
219   showModal: (body) => dispatch(actions.app.showModal(
220     body,
221     actions.app.hideModal,
222     null,
223     {
224       box: true,
225       wide: true,
226       noCloseBtn: true
227     }
228   )),
229   cmd: (data) => dispatch(actions.app.cmd(data))
230 })
231
232 export default connect(
233   mapStateToProps,
234   mapDispatchToProps
235 )( withNamespaces('translations') (CoreIndex) )