OSDN Git Service

Merge pull request #18 from Bytom/dev
[bytom/bytom-electron.git] / src / features / assets / components / New.jsx
1 import React from 'react'
2 import { BaseNew, FormContainer, FormSection, JsonField, KeyConfiguration, TextField } from 'features/shared/components'
3 import { reduxForm } from 'redux-form'
4
5 class Form extends React.Component {
6   constructor(props) {
7     super(props)
8
9     this.submitWithErrors = this.submitWithErrors.bind(this)
10   }
11
12   submitWithErrors(data) {
13     return new Promise((resolve, reject) => {
14       this.props.submitForm(data)
15         .catch((err) => reject({_error: err}))
16     })
17   }
18
19   render() {
20     const {
21       fields: { alias, tags, definition, xpubs, quorum },
22       error,
23       handleSubmit,
24       submitting
25     } = this.props
26     const lang = this.props.lang
27
28     return(
29       <FormContainer
30         error={error}
31         label= { lang === 'zh' ? '新建资产' : 'New asset' }
32         onSubmit={handleSubmit(this.submitWithErrors)}
33         submitting={submitting}
34         lang={lang}>
35
36         <FormSection title={ lang === 'zh' ? '资产信息' : 'Asset Information'}>
37           <TextField title='Alias' placeholder='Alias' fieldProps={alias} autoFocus={true} />
38           <JsonField title={ lang === 'zh' ? '定义' : 'Definition' } fieldProps={definition} lang={lang}/>
39         </FormSection>
40
41         <FormSection title={ lang === 'zh' ? '密钥和签名' :'Keys and Signing' }>
42           <KeyConfiguration
43             xpubs={xpubs}
44             quorum={quorum}
45             quorumHint='Number of signatures required to issue' />
46         </FormSection>
47
48       </FormContainer>
49     )
50   }
51 }
52
53 const validate = values => {
54   const errors = {}
55
56   const jsonFields = ['definition']
57   jsonFields.forEach(key => {
58     const fieldError = JsonField.validator(values[key])
59     if (fieldError) { errors[key] = fieldError }
60   })
61
62   if (!values.alias) { errors.alias = 'Asset alias is required' }
63
64   return errors
65 }
66
67 const fields = [
68   'alias',
69   'definition',
70   'xpubs[].value',
71   'xpubs[].type',
72   'quorum'
73 ]
74 export default BaseNew.connect(
75   BaseNew.mapStateToProps('asset'),
76   BaseNew.mapDispatchToProps('asset'),
77   reduxForm({
78     form: 'newAssetForm',
79     fields,
80     validate,
81     initialValues: {
82       definition: '{\n  "name": "", \n  "symobol": "",\n  "decimals": 8,\n  "description": {}\n}',
83       quorum: 1,
84     }
85   })(Form)
86 )