OSDN Git Service

Merge pull request #52 from Bytom/i18n
[bytom/bytom-electron.git] / src / features / mockhsm / actions.js
1 import { baseListActions, baseCreateActions } 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 resetPassword = {
18   submitResetForm: (params) => {
19     let promise = Promise.resolve()
20     return (dispatch)  => {
21       return promise.then(() => clientApi().resetPassword(params).then((resp) => {
22         if(resp.data.changed){
23           dispatch({ type: `RESET_PASSWORD_${type.toUpperCase()}`, resp })
24           dispatch(push({
25             pathname: `/${type}s/${params.xpub}`,
26             state: {
27               preserveFlash: true
28             }
29           }))
30         }else{
31           throw {code: 'F_BTM001'}
32         }
33       }))
34     }
35   }
36 }
37
38 const checkPassword = (data) => (dispatch) => {
39   return clientApi().checkPassword(data)
40     .then((resp) => {
41       if(resp.status === 'fail'){
42         throw resp
43       }else if(!resp.data.checkResult){
44         throw {code: 'F_BTM000'}
45       }
46       dispatch({ type: 'KEY_PASSWORD_SUCCESS'})
47     })
48 }
49
50 const createExport =  (arg, fileName) => (dispatch) => {
51   clientApi().export(arg).then(resp => {
52     if(resp.status == 'success'){
53       const privateKey = resp.data.privateKey
54
55       var element = document.createElement('a')
56       element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(privateKey))
57       element.setAttribute('download', fileName)
58       element.style.display = 'none'
59       document.body.appendChild(element)
60       element.click()
61
62       document.body.removeChild(element)
63     }else if(resp.status == 'fail'){
64       dispatch({ type: 'ERROR', payload: {message: resp.msg} })
65     }
66   })
67 }
68
69 export default {
70   ...create,
71   ...list,
72   ...resetPassword,
73   checkPassword,
74   createExport
75 }