OSDN Git Service

redirect to transactions after transaction created
[bytom/bytom-electron.git] / src / features / transactions / actions.js
1 import { chainClient } from 'utility/environment'
2 import { parseNonblankJSON } from 'utility/string'
3 import { push } from 'react-router-redux'
4 import { baseCreateActions, baseListActions } from 'features/shared/actions'
5
6 const type = 'transaction'
7
8 const list = baseListActions(type, {
9   defaultKey: 'id'
10 })
11 const form = baseCreateActions(type)
12
13 function preprocessTransaction(formParams) {
14   const copy = JSON.parse(JSON.stringify(formParams))
15   const builder = {
16     baseTransaction: copy.baseTransaction,
17     actions: copy.actions,
18   }
19
20   if (builder.baseTransaction == '') {
21     delete builder.baseTransaction
22   }
23
24   if (formParams.submitAction == 'generate') {
25     builder.ttl = '1h' // 1 hour
26   }
27
28   for (let i in builder.actions) {
29     let a = builder.actions[i]
30
31     const intFields = ['amount', 'position']
32     intFields.forEach(key => {
33       const value = a[key]
34       if (value) {
35         if ((parseInt(value)+'') == value) {
36           a[key] = parseInt(value)
37         } else {
38           throw new Error(`Action ${parseInt(i)+1} ${key} must be an integer.`)
39         }
40       }
41     })
42
43     try {
44       a.referenceData = parseNonblankJSON(a.referenceData)
45     } catch (err) {
46       throw new Error(`Action ${parseInt(i)+1} reference data should be valid JSON, or blank.`)
47     }
48
49     try {
50       a.receiver = parseNonblankJSON(a.receiver)
51     } catch (err) {
52       throw new Error(`Action ${parseInt(i)+1} receiver should be valid JSON.`)
53     }
54   }
55
56   return builder
57 }
58
59 form.submitForm = (formParams) => function(dispatch) {
60   const buildPromise = chainClient().transactions.build(builder => {
61     const processed = preprocessTransaction(formParams)
62
63     builder.actions = processed.actions.map(action => {
64       return {
65         amount: action.amount,
66         account_id: action.accountId,
67         account_alias: action.accountAlias,
68         asset_id: action.assetId,
69         asset_alias: action.assetAlias,
70         type: action.type
71       }
72     })
73     if (processed.baseTransaction) {
74       builder.baseTransaction = processed.baseTransaction
75     }
76   })
77
78   if (formParams.submitAction == 'submit') {
79     return buildPromise
80       .then(({data: tpl}) => {
81         const client = chainClient()
82         const body = Object.assign({}, {Auth: '123456', 'transaction': tpl})
83         return client.connection.request('/sign-submit-transaction', body, true)
84       }).then(resp => {
85         if (resp.status === 'fail') {
86           // TODO: deal with failure
87           return
88         }
89
90         dispatch(form.created())
91         dispatch(push({
92           pathname: '/transactions'
93         }))
94       })
95   }
96 }
97
98 export default {
99   ...list,
100   ...form,
101 }