OSDN Git Service

4143bf3f55be02410f92d39205a6c62820ec91eb
[bytom/bytom-dashboard.git] / src / features / assets / components / NewKeyAndSign.jsx
1 import React from 'react'
2 import { FormContainer, FormSection, KeyConfiguration } from 'features/shared/components'
3 import { reduxForm } from 'redux-form'
4 import { withNamespaces } from 'react-i18next'
5
6 class NewKeyAndSign extends React.Component {
7   constructor(props) {
8     super(props)
9   }
10
11   render() {
12     const {
13       fields: { xpubs, quorum },
14       error,
15       previousPage,
16       handleSubmit,
17       submitting,
18       t
19     } = this.props
20
21     const prev = () => {
22       const promise = new Promise(function(resolve, reject) {
23         try {
24           for (let i = 0; i < xpubs.length; i++) {
25             xpubs.removeField()
26           }
27         } catch (err) {
28           reject(err)
29         }
30         resolve()
31       })
32
33       promise.then(previousPage)
34     }
35
36     return(
37       <FormContainer
38         error={error}
39         label= { t('asset.new') }
40         onSubmit={handleSubmit}
41         submitting={submitting}
42         >
43
44         <FormSection title={t('form.keyAndSign')}>
45           <KeyConfiguration
46             xpubs={xpubs}
47             quorum={quorum}
48             quorumHint={t('asset.quorumHint')} />
49         </FormSection>
50         <button type='button' onClick={prev}>
51           <i/> Previous
52         </button>
53
54       </FormContainer>
55     )
56   }
57 }
58
59 const validate = (values, props) => {
60   const errors = { xpubs:{} }
61   const t = props.t
62
63   values.xpubs.forEach((xpub, index) => {
64     if (!values.xpubs[index].value) {
65       errors.xpubs[index] = {...errors.xpubs[index], value: t('asset.keysError')}
66     }
67   })
68
69   return errors
70 }
71
72 const fields = [
73   'alias',
74   'symbol',
75   'decimals',
76   'reissue',
77   'description[].key',
78   'description[].value',
79   'xpubs[].value',
80   'xpubs[].type',
81   'quorum'
82 ]
83 export default withNamespaces('translations') (
84   reduxForm({
85     form: 'newAssetForm',
86     fields,
87     validate,
88     destroyOnUnmount: false,
89   })(NewKeyAndSign)
90 )