OSDN Git Service

update the default account selection logic
authorZhiting Lin <zlin035@uottawa.ca>
Wed, 17 Jul 2019 01:49:46 +0000 (09:49 +0800)
committerZhiting Lin <zlin035@uottawa.ca>
Wed, 17 Jul 2019 01:49:46 +0000 (09:49 +0800)
src/features/accounts/actions.js
src/features/accounts/components/List.jsx
src/features/app/components/Container.jsx
src/features/initialization/actions.js

index 1273049..ea1ab06 100644 (file)
@@ -17,24 +17,30 @@ const update = baseUpdateActions(type, {
 
 const switchAccount = (accountAlias) => {
   return (dispatch) => {
-    return action.transaction.getAddresses({accountAlias}).then(address => {
-      return chainClient().accounts.setMiningAddress({'miningAddress':address}).then(resp =>{
-        if (resp.status === 'fail') {
-          throw resp
-        }
+    dispatch({type: 'SET_CURRENT_ACCOUNT', account: accountAlias})
+  }
+}
 
-        dispatch({type: 'SET_CURRENT_ACCOUNT', account: accountAlias})
-      })
+const setMiningAddress = (accountAlias) =>{
+  return action.transaction.getAddresses({accountAlias}).then(address => {
+    return chainClient().accounts.setMiningAddress({'miningAddress':address}).then(resp =>{
+      if (resp.status === 'fail') {
+        throw resp
+      }
+
+      return resp
     })
-  }
+  })
 }
 
 const setDefaultAccount = () =>{
   return (dispatch) => {
     return chainClient().accounts.query().then(result => {
       const account = result.data[0].alias
-      dispatch(switchAccount((account)))
-      return account
+      return setMiningAddress(account).then(()=>{
+        dispatch(switchAccount((account)))
+        return account
+      })
     })
   }
 }
@@ -111,7 +117,8 @@ let actions = {
   switchAccount,
   setDefaultAccount,
   createAccount,
-  createSuccess
+  createSuccess,
+  setMiningAddress
 }
 
 export default actions
index bd3d561..f695a0b 100644 (file)
@@ -37,7 +37,9 @@ const mapDispatchToProps = (dispatch) => ({
   ...BaseList.mapDispatchToProps(type)(dispatch),
   itemActions: {
     switch: (account) => {
-      dispatch(actions.switchAccount(account))
+      return actions.setMiningAddress(account).then(()=>{
+        dispatch(actions.switchAccount((account)))
+      })
     }
   },
 })
index 298d139..47d2011 100644 (file)
@@ -36,26 +36,6 @@ class Container extends React.Component {
   }
 
   componentDidMount() {
-    this.props.fetchKeyItem().then(resp => {
-      if (resp.data.length == 0) {
-        this.props.updateAccountInit(false)
-        this.redirectRoot(this.props)
-      }else{
-        this.props.updateAccountInit(true)
-        this.props.fetchAccountItem().then(resp => {
-          if (resp.data.length == 0) {
-            this.props.switchAccount('')
-            this.redirectRoot(this.props)
-          }else{
-            const aliasArray = resp.data.map(account => account.alias)
-            if(!aliasArray.includes(this.props.currentAccount) ){
-              this.props.setDefaultAccount()
-              this.props.showRoot()
-            }
-          }
-        })
-      }
-    })
     if(this.props.lng === 'zh'){
       moment.locale('zh-cn')
     }else{
@@ -64,8 +44,35 @@ class Container extends React.Component {
   }
 
   componentWillMount() {
-    this.props.fetchInfo().then(() => {
-      this.redirectRoot(this.props)
+    this.props.fetchAccountItem().then(resp => {
+      const promise = new Promise((resolve, reject) => {
+        if (resp.data.length == 0) {
+          this.props.switchAccount('')
+          resolve()
+        }else{
+          const aliasArray = resp.data.map(account => account.alias)
+          if(!aliasArray.includes(this.props.currentAccount) ){
+            this.props.setDefaultAccount().then(()=>{
+              resolve()
+            })
+          }else{
+            resolve()
+          }
+        }
+      })
+
+      return promise.then(()=>{
+        return this.props.fetchKeyItem().then(resp => {
+          if (resp.data.length == 0) {
+            this.props.updateAccountInit(false)
+          }else{
+            this.props.updateAccountInit(true)
+          }
+          return this.props.fetchInfo().then(() => {
+            this.redirectRoot(this.props)
+          })
+        })
+      })
     })
 
     setInterval(() => this.props.fetchInfo(), CORE_POLLING_TIME)
index 467e4b2..dc053cd 100644 (file)
@@ -110,12 +110,6 @@ const restoreMnemonic = (data, success) => {
 
 const initSucceeded = () => (dispatch) => {
   dispatch({type: 'CREATE_REGISTER_ACCOUNT'})
-  dispatch(push({
-    pathname: '/transactions',
-    state: {
-      preserveFlash: true
-    }
-  }))
 }
 
 let actions = {