OSDN Git Service

add update key alias function.
[bytom/bytom-electron.git] / src / features / mockhsm / actions.js
1 import { baseListActions, baseCreateActions, baseUpdateActions } from 'features/shared/actions'
2 import { chainClient } from 'utility/environment'
3 import {push} from 'react-router-redux'
4
5 const type = 'key'
6 const clientApi = () => chainClient().mockHsm.keys
7
8 const list = baseListActions(type, {
9   className: 'Key',
10   clientApi,
11 })
12 const create = baseCreateActions(type, {
13   className: 'Key',
14   clientApi,
15 })
16
17 const update = baseUpdateActions(type, {
18   className: 'Key',
19   clientApi,
20 })
21
22 create.submitForm = (data) => function (dispatch) {
23   if (typeof data.alias == 'string')  data.alias = data.alias.trim()
24
25   return clientApi().create(data)
26     .then((resp) => {
27       if (resp.status === 'fail') {
28         throw resp
29       }
30
31       dispatch({type: 'NEW_KEY', data: resp.data.mnemonic})
32       dispatch( push('/keys/mnemonic') )
33     })
34 }
35
36 update.submitUpdateForm = (data, xpub) => {
37   let promise = Promise.resolve()
38
39   return function(dispatch) {
40     return promise.then(() => clientApi().updateAlias({
41       xpub: xpub,
42       password: data.password,
43       newAlias: data.alias,
44     }).then((resp) => {
45       if (resp.status === 'fail') {
46         throw resp
47       }
48       dispatch(update.updated())
49
50       dispatch(push({
51         pathname: `/${type}s/${xpub}`,
52         state: {
53           preserveFlash: true
54         }
55       }))
56     }))
57   }
58 }
59
60 const resetPassword = {
61   submitResetForm: (params) => {
62     let promise = Promise.resolve()
63     return (dispatch)  => {
64       return promise.then(() => clientApi().resetPassword(params).then((resp) => {
65         if(resp.data.changed){
66           dispatch({ type: `RESET_PASSWORD_${type.toUpperCase()}`, resp })
67           dispatch(push({
68             pathname: `/${type}s/${params.xpub}`,
69             state: {
70               preserveFlash: true
71             }
72           }))
73         }else{
74           throw {code: 'F_BTM001'}
75         }
76       }))
77     }
78   }
79 }
80
81 const checkPassword = (data) => (dispatch) => {
82   return clientApi().checkPassword(data)
83     .then((resp) => {
84       if(resp.status === 'fail'){
85         throw resp
86       }else if(!resp.data.checkResult){
87         throw {code: 'F_BTM000'}
88       }
89       dispatch({ type: 'KEY_PASSWORD_SUCCESS'})
90     })
91 }
92
93 const createExport =  (arg, fileName) => (dispatch) => {
94   clientApi().export(arg).then(resp => {
95     if(resp.status == 'success'){
96       const privateKey = resp.data.privateKey
97
98       var element = document.createElement('a')
99       element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(privateKey))
100       element.setAttribute('download', fileName)
101       element.style.display = 'none'
102       document.body.appendChild(element)
103       element.click()
104
105       document.body.removeChild(element)
106     }else if(resp.status == 'fail'){
107       dispatch({ type: 'ERROR', payload: {message: resp.msg} })
108     }
109   })
110 }
111
112 const createSuccess = ()=> (dispatch) =>{
113   dispatch(create.created())
114   dispatch(push('/keys'))
115 }
116
117 export default {
118   ...create,
119   ...list,
120   ...update,
121   ...resetPassword,
122   checkPassword,
123   createExport,
124   createSuccess
125 }