OSDN Git Service

30ea13aecdc997812f099980ebd2cf476d3aa160
[bytom/bytom-electron.git] / src / features / assets / components / New.jsx
1 import React from 'react'
2 import { BaseNew } from 'features/shared/components'
3 import NewKeyAndSign from './NewKeyAndSign'
4 import NewAssetInfo from './NewAssetInfo'
5 import {withNamespaces} from 'react-i18next'
6 import { destroy } from 'redux-form';
7
8 class Form extends React.Component {
9   constructor(props) {
10     super(props)
11     this.submitWithValidations = this.submitWithValidations.bind(this)
12     this.nextPage = this.nextPage.bind(this)
13     this.previousPage = this.previousPage.bind(this)
14     this.state = {
15       page: 1
16     }
17   }
18
19   componentWillUnmount(){
20     this.props.destroyForm()
21   }
22
23   submitWithValidations(data) {
24     const resultData = {
25       alias: data.alias,
26       quorum: data.quorum,
27       xpubs: data.xpubs,
28       limit_height: data.reissue === 'true'? 0 : this.props.blockHeight+3,
29       definition:{
30         name: data.alias.trim(),
31         symbol: data.symbol.trim(),
32         decimals: data.decimals,
33         quorum: data.quorum,
34         reissue: data.reissue,
35         description: data.description
36       }
37     }
38
39     return new Promise((resolve, reject) => {
40       this.props.submitForm(resultData)
41         .then(()=>{
42           this.props.destroyForm()
43         })
44         .catch((err) => reject({_error: err}))
45     })
46   }
47
48   nextPage() {
49     this.setState({ page: this.state.page + 1 })
50   }
51
52   previousPage() {
53     this.setState({ page: this.state.page - 1 })
54   }
55
56   render() {
57     const { page } = this.state
58     return (
59       <div>
60         {page === 1 && <NewAssetInfo onSubmit={this.nextPage}/>}
61         {page === 2 && <NewKeyAndSign previousPage={this.previousPage} onSubmit={this.submitWithValidations}/>}
62       </div>
63     )
64   }
65 }
66
67 const mapDispatchToProps = ( dispatch ) => ({
68   ...BaseNew.mapDispatchToProps('asset')(dispatch),
69   destroyForm: () => dispatch(destroy('newAssetForm')),
70 })
71
72 const mapStateToProps = ( state ) => ({
73   ...BaseNew.mapStateToProps('asset'),
74   blockHeight: state.core.blockHeight
75 })
76
77 export default withNamespaces('translations') ( BaseNew.connect(
78   mapStateToProps,
79   mapDispatchToProps,
80   Form
81 ))
82
83