OSDN Git Service

add the translation to all required field.
[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={ lang === 'zh' ? '别名' : 'Alias'} placeholder={ lang === 'zh' ? '别名' : '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             lang={lang}
44             xpubs={xpubs}
45             quorum={quorum}
46             quorumHint={ lang === 'zh' ? '所需的签名数' : 'Number of signatures required to issue' } />
47         </FormSection>
48
49       </FormContainer>
50     )
51   }
52 }
53
54 const validate = values => {
55   const errors = {}
56
57   const jsonFields = ['definition']
58   jsonFields.forEach(key => {
59     const fieldError = JsonField.validator(values[key])
60     if (fieldError) { errors[key] = fieldError }
61   })
62
63   if (!values.alias) { errors.alias = 'Asset alias is required' }
64
65   return errors
66 }
67
68 const fields = [
69   'alias',
70   'definition',
71   'xpubs[].value',
72   'xpubs[].type',
73   'quorum'
74 ]
75 export default BaseNew.connect(
76   BaseNew.mapStateToProps('asset'),
77   BaseNew.mapDispatchToProps('asset'),
78   reduxForm({
79     form: 'newAssetForm',
80     fields,
81     validate,
82     initialValues: {
83       definition: '{\n  "name": "", \n  "symobol": "",\n  "decimals": 8,\n  "description": {}\n}',
84       quorum: 1,
85     }
86   })(Form)
87 )