OSDN Git Service

add the mining option for dashboard
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 11 Apr 2018 11:58:27 +0000 (19:58 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 11 Apr 2018 11:58:27 +0000 (19:58 +0800)
src/features/core/actions.js
src/features/core/components/CoreIndex/CoreIndex.jsx
src/features/core/components/CoreIndex/CoreIndex.scss
src/features/core/reducers.js
src/sdk/api/config.js

index cd9c85c..0afc8e9 100644 (file)
@@ -7,6 +7,20 @@ const updateBTMAmountUnit = (param) => ({type: 'UPDATE_BTM_AMOUNT_UNIT', param})
 const updateConfiguredStatus = ({ type: 'SET_CONFIGURED' })
 const updateLang = (param) => ({type: 'UPDATE_CORE_LANGUAGE', lang:param})
 
+const updateMiningState = (param) => {
+  return (dispatch) => {
+    return chainClient().config.mining(param)
+      .then(() => {
+        dispatch({type: 'UPDATE_MINING_STATE', param})
+      })
+      .catch((err) => {
+        if (!err.status) {
+          throw err
+        }
+      })
+  }
+}
+
 const fetchCoreInfo = (options = {}) => {
   return (dispatch) => {
     return chainClient().config.info()
@@ -33,6 +47,7 @@ let actions = {
   updateBTMAmountUnit,
   updateLang,
   updateConfiguredStatus,
+  updateMiningState,
   fetchCoreInfo,
   clearSession,
   logIn: (token) => (dispatch) => {
index 45cce39..d985967 100644 (file)
@@ -15,22 +15,12 @@ class CoreIndex extends React.Component {
     this.state = {
       btmAmountUnit: 'BTM'
     }
-    this.deleteClick = this.deleteClick.bind(this)
+    this.handleMiningState = this.handleMiningState.bind(this)
     this.handleAdvancedOptionChange = this.handleAdvancedOptionChange.bind(this)
     this.changeBTMamount = this.changeBTMamount.bind(this)
   }
 
   componentDidMount() {
-    const fetchInfo = () => {
-      if(this.refs.requestComponent) {
-        chainClient().config.info().then(resp => {
-          this.setState({requestStatus: resp.data})
-        })
-      }
-    }
-    setInterval(fetchInfo.bind(this), 2 * 1000)
-
-
     if(window.ipcRenderer){
       window.ipcRenderer.on('btmAmountUnitState', (event, arg) => {
         this.props.uptdateBtmAmountUnit(arg)
@@ -38,28 +28,6 @@ class CoreIndex extends React.Component {
     }
   }
 
-  deleteClick() {
-    if (!window.confirm('Are you sure you want to delete all data on this core?')) {
-      return
-    }
-
-    this.setState({deleteDisabled: true})
-
-    chainClient().config.reset(true).then(() => {
-      // TODO: Use Redux state reset and nav action instead of window.location.
-      // Also, move confirmation message to a bonafide flash div. alert() in a
-      // browser microtask is going away. cf https://www.chromestatus.com/features/5647113010544640
-      setTimeout(function(){
-        window.location.href = '/'
-      }, 500)
-    }).catch((err) => {
-      this.setState({
-        deleteError: err,
-        deleteDisabled: false,
-      })
-    })
-  }
-
   changeBTMamount(value) {
     this.setState({ btmAmountUnit: value })
     this.props.uptdateBtmAmountUnit(value)
@@ -74,28 +42,18 @@ class CoreIndex extends React.Component {
     }
   }
 
-  render() {
-    const {
-      onTestnet,
-      testnetBlockchainMismatch,
-      testnetNetworkMismatch,
-      testnetNextReset,
-    } = this.props
-
-    let generatorUrl
-    if (this.props.core.generator) {
-      generatorUrl = window.location.origin
-    } else if (onTestnet) {
-      generatorUrl = <span>
-        {this.props.core.generatorUrl}
-        &nbsp;
-        <span className='label label-primary'>Chain Testnet</span>
-      </span>
-    } else {
-      generatorUrl = this.props.core.generatorUrl
+  handleMiningState(event){
+    const target = event.target
+    if( target.checked ){
+      this.props.updateMiningState(true)
+    }else{
+      this.props.updateMiningState(false)
     }
+  }
 
+  render() {
     let navState = this.props.navAdvancedState === 'advance'
+    let miningState = this.props.core.mingStatus
 
     let configBlock = (
       <div className={[styles.left, styles.col].join(' ')}>
@@ -103,59 +61,10 @@ class CoreIndex extends React.Component {
           <h4>Configuration</h4>
           <table className={styles.table}>
             <tbody>
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Core type:</td>*/}
-            {/*<td>{this.props.core.coreType}</td>*/}
-            {/*</tr>*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Setup time:</td>*/}
-            {/*<td>{this.props.core.configuredAt}</td>*/}
-            {/*</tr>*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Version:</td>*/}
-            {/*<td><code>{this.props.core.version}</code></td>*/}
-            {/*</tr>*/}
             <tr>
               <td className={styles.row_label}>Language:</td>
               <td>{this.props.core.lang === 'zh' ? '中文' : 'English'}</td>
             </tr>
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>MockHSM enabled:</td>*/}
-            {/*<td><code>{this.props.core.mockhsm.toString()}</code></td>*/}
-            {/*</tr>*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Localhost auth:</td>*/}
-            {/*<td><code>{this.props.core.localhostAuth.toString()}</code></td>*/}
-            {/*</tr>*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Reset enabled:</td>*/}
-            {/*<td><code>{this.props.core.reset.toString()}</code></td>*/}
-            {/*</tr>*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Non-TLS HTTP requests enabled:</td>*/}
-            {/*<td><code>{this.props.core.httpOk.toString()}</code></td>*/}
-            {/*</tr>*/}
-            {/*<tr>*/}
-            {/*<td colSpan={2}><hr /></td>*/}
-            {/*</tr>*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Generator URL:</td>*/}
-            {/*<td>{generatorUrl}</td>*/}
-            {/*</tr>*/}
-            {/*{onTestnet && !!testnetNextReset &&*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Next Chain Testnet data reset:</td>*/}
-            {/*<td>{testnetNextReset.toString()}</td>*/}
-            {/*</tr>}*/}
-            {/*{!this.props.core.generator &&*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Generator Access Token:</td>*/}
-            {/*<td><code>{this.props.core.generatorAccessToken}</code></td>*/}
-            {/*</tr>}*/}
-            {/*<tr>*/}
-            {/*<td className={styles.row_label}>Blockchain ID:</td>*/}
-            {/*<td><code className={styles.block_hash}>{this.props.core.blockchainId}</code></td>*/}
-            {/*</tr>*/}
             <tr>
               <td colSpan={2}><hr /></td>
             </tr>
@@ -173,6 +82,19 @@ class CoreIndex extends React.Component {
               </td>
             </tr>
             <tr>
+              <td className={styles.row_label}>Mining: </td>
+              <td>
+                <label className={styles.switch}>
+                  <input
+                    type='checkbox'
+                    onChange={this.handleMiningState}
+                    checked={miningState}
+                  />
+                  <span className={styles.slider}></span>
+                </label>
+              </td>
+            </tr>
+            <tr>
               <td className={styles.row_label} >Unit to show amount in </td>
               <td>
                 <DropdownButton
@@ -193,24 +115,16 @@ class CoreIndex extends React.Component {
       </div>
     )
 
-    let testnetErr
-    if (onTestnet) {
-      if (testnetBlockchainMismatch) {
-        testnetErr = 'Chain Testnet has been reset. Please reset your core below.'
-      } else if (testnetNetworkMismatch) {
-        testnetErr = {message: <span>This core is no longer compatible with Chain Testnet. <a href={`${docsRoot}/core/get-started/install`} target='_blank'>Please upgrade Chain Core</a>.</span>}
-      }
-    }
-
-    let requestStatusBlock =
-      this.state.requestStatus && (<div className={styles['sub-row']}>
+    let coreData = this.props.core.coreData
+    let requestStatusBlock =(
+      <div className={styles['sub-row']}>
           <h4>Network status</h4>
           <table className={styles.table}>
             <tbody>
-            {Object.keys(this.state.requestStatus).map(key => (
+            {Object.keys(coreData).map(key => (
               <tr key={key}>
                 <td className={styles.row_label}> {key}: </td>
-                <td className={styles.row_value}>{ String(this.state.requestStatus[key])}</td>
+                <td className={styles.row_value}>{ String(coreData[key])}</td>
               </tr>))}
             </tbody>
           </table>
@@ -220,63 +134,8 @@ class CoreIndex extends React.Component {
     let networkStatusBlock = (
       <div className={styles.right}>
         <div ref='requestComponent'>
-          {/*<div className={[styles.top, styles['sub-row']].join(' ')}>*/}
-          {/*<h4>Network status</h4>*/}
-          {/*<table className={styles.table}>*/}
-          {/*<tbody>*/}
-          {/*<tr>*/}
-          {/*<td className={styles.row_label}>Generator block:</td>*/}
-          {/*<td className={styles.row_value}>{this.props.core.generatorBlockHeight}</td>*/}
-          {/*</tr>*/}
-          {/*<tr>*/}
-          {/*<td className={styles.row_label}>Local block:</td>*/}
-          {/*<td className={styles.row_value}>{this.props.core.blockHeight}</td>*/}
-          {/*</tr>*/}
-          {/*<tr>*/}
-          {/*<td className={styles.row_label}>Replication lag:</td>*/}
-          {/*<td className={`${styles.replication_lag} ${styles[this.props.core.replicationLagClass]}`}>*/}
-          {/*{this.props.core.replicationLag === null ? '???' : this.props.core.replicationLag}*/}
-          {/*</td>*/}
-          {/*</tr>*/}
-          {/*</tbody>*/}
-          {/*</table>*/}
-          {/*</div>*/}
           {requestStatusBlock}
         </div>
-        {testnetErr && <ErrorBanner title='Chain Testnet error' error={testnetErr} />}
-      </div>
-    )
-
-    let resetDataBlock = (
-      <div className='row'>
-        <div className='col-sm-6'>
-          <h4>Reset data</h4>
-
-          {this.props.core.reset ?
-            <div>
-              <p>
-                This will permanently delete all data stored in this core,
-                including blockchain data, accounts, assets, indexes,
-                and MockHSM keys.
-              </p>
-
-              {this.state.deleteError && <ErrorBanner
-                title='Error resetting data'
-                message={this.state.deleteError.toString()}
-              />}
-
-              <button
-                className='btn btn-danger'
-                onClick={this.deleteClick}
-                disabled={this.state.deleteDisabled}
-              >
-                Delete all data
-              </button>
-            </div> :
-            <p>
-              This core is not configured with reset capabilities.
-            </p>}
-        </div>
       </div>
     )
 
@@ -289,7 +148,6 @@ class CoreIndex extends React.Component {
             {configBlock}
             {networkStatusBlock}
           </div>
-          {/*{resetDataBlock}*/}
         </PageContent>
       </div>
     )
@@ -299,16 +157,13 @@ class CoreIndex extends React.Component {
 const mapStateToProps = (state) => ({
   core: state.core,
   navAdvancedState: state.app.navAdvancedState,
-  onTestnet: state.core.onTestnet,
-  testnetBlockchainMismatch: testnetUtils.isBlockchainMismatch(state),
-  testnetNetworkMismatch: testnetUtils.isCrosscoreRpcMismatch(state),
-  testnetNextReset: state.testnet.nextReset,
 })
 
 const mapDispatchToProps = (dispatch) => ({
   showNavAdvanced: () => dispatch(actions.app.showNavAdvanced),
   hideNavAdvanced: () => dispatch(actions.app.hideNavAdvanced),
-  uptdateBtmAmountUnit: (param) => dispatch(actions.core.updateBTMAmountUnit(param))
+  uptdateBtmAmountUnit: (param) => dispatch(actions.core.updateBTMAmountUnit(param)),
+  updateMiningState: (param) => dispatch(actions.core.updateMiningState(param))
 })
 
 export default connect(
index 901b1fb..8cb0bc3 100644 (file)
@@ -12,6 +12,7 @@
 
 .row_label {
   padding-right: $grid-gutter-width;
+  text-transform: capitalize;
   font-weight: bold;
   white-space: pre;
 }
index fac2ef2..f7df2c8 100644 (file)
@@ -229,6 +229,20 @@ const lang = (state = defaultLang, action) => {
   return state
 }
 
+const mingStatus = (state = false, action) => {
+  if (action.type == 'UPDATE_CORE_INFO') {
+    return action.param.data.mining
+  }
+  return state
+}
+
+const coreData = (state = null, action) => {
+  if (action.type == 'UPDATE_CORE_INFO') {
+    return action.param.data || null
+  }
+  return state
+}
+
 const snapshot = (state = null, action) => {
   if (action.type == 'UPDATE_CORE_INFO') {
     return action.param.snapshot || null // snapshot may be undefined, which Redux doesn't like.
@@ -248,12 +262,14 @@ export default combineReducers({
   configured,
   configuredAt,
   coreType,
+  coreData,
   generator,
   generatorAccessToken,
   generatorBlockHeight,
   generatorUrl,
   localhostAuth,
   mockhsm,
+  mingStatus,
   crosscoreRpcVersion,
   onTestnet,
   httpOk,
index d35a95b..40d948f 100644 (file)
@@ -12,6 +12,11 @@ const configAPI = (client) => {
       cb
     ),
 
+    mining: (miningState = false, cb) => shared.tryCallback(
+      client.request('/set-mining', {is_mining: miningState}),
+      cb
+    ),
+
     info: (cb) => shared.tryCallback(
       client.request('/net-info'),
       cb