OSDN Git Service

updated account alias.
[bytom/bytom-electron.git] / src / features / mockhsm / actions.js
index 0cca365..5445f69 100644 (file)
@@ -1,6 +1,6 @@
-import { baseListActions, baseCreateActions } from 'features/shared/actions'
+import { baseListActions, baseCreateActions, baseUpdateActions } from 'features/shared/actions'
 import { chainClient } from 'utility/environment'
-import {reset} from 'redux-form'
+import {push} from 'react-router-redux'
 
 const type = 'key'
 const clientApi = () => chainClient().mockHsm.keys
@@ -14,6 +14,25 @@ const create = baseCreateActions(type, {
   clientApi,
 })
 
+const update = baseUpdateActions(type, {
+  className: 'Key',
+  clientApi,
+})
+
+create.submitForm = (data) => function (dispatch) {
+  if (typeof data.alias == 'string')  data.alias = data.alias.trim()
+
+  return clientApi().create(data)
+    .then((resp) => {
+      if (resp.status === 'fail') {
+        throw resp
+      }
+
+      dispatch({type: 'NEW_KEY', data: resp.data.mnemonic})
+      dispatch( push('/keys/mnemonic') )
+    })
+}
+
 const resetPassword = {
   submitResetForm: (params) => {
     let promise = Promise.resolve()
@@ -21,37 +40,62 @@ const resetPassword = {
       return promise.then(() => clientApi().resetPassword(params).then((resp) => {
         if(resp.data.changed){
           dispatch({ type: `RESET_PASSWORD_${type.toUpperCase()}`, resp })
-          dispatch(reset('ResetPassword'))
+          dispatch(push({
+            pathname: `/${type}s/${params.xpub}`,
+            state: {
+              preserveFlash: true
+            }
+          }))
         }else{
-          let msg = 'Unable to reset the key password.'
-          dispatch({ type: 'ERROR', payload: {message: msg} })
-          throw new Error(msg)
+          throw {code: 'F_BTM001'}
         }
       }))
     }
   }
 }
 
+const checkPassword = (data) => (dispatch) => {
+  return clientApi().checkPassword(data)
+    .then((resp) => {
+      if(resp.status === 'fail'){
+        throw resp
+      }else if(!resp.data.checkResult){
+        throw {code: 'F_BTM000'}
+      }
+      dispatch({ type: 'KEY_PASSWORD_SUCCESS'})
+    })
+}
+
+const createExport =  (arg, fileName) => (dispatch) => {
+  clientApi().export(arg).then(resp => {
+    if(resp.status == 'success'){
+      const privateKey = resp.data.privateKey
+
+      var element = document.createElement('a')
+      element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(privateKey))
+      element.setAttribute('download', fileName)
+      element.style.display = 'none'
+      document.body.appendChild(element)
+      element.click()
+
+      document.body.removeChild(element)
+    }else if(resp.status == 'fail'){
+      dispatch({ type: 'ERROR', payload: {message: resp.msg} })
+    }
+  })
+}
+
+const createSuccess = ()=> (dispatch) =>{
+  dispatch(create.created())
+  dispatch(push('/keys'))
+}
+
 export default {
   ...create,
   ...list,
+  ...update,
   ...resetPassword,
-  createExport: (arg, fileName) => (dispatch) => {
-    clientApi().export(arg).then(resp => {
-      if(resp.status == 'success'){
-        const privateKey = resp.data.privateKey
-
-        var element = document.createElement('a')
-        element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(privateKey))
-        element.setAttribute('download', fileName)
-        element.style.display = 'none'
-        document.body.appendChild(element)
-        element.click()
-
-        document.body.removeChild(element)
-      }else if(resp.status == 'fail'){
-        dispatch({ type: 'ERROR', payload: {message: resp.msg} })
-      }
-    })
-  }
+  checkPassword,
+  createExport,
+  createSuccess
 }