OSDN Git Service

c2e61ec7d33bb73df301af2c128d9e04ed42aa54
[bytom/bytom-electron.git] / src / features / app / components / Container.jsx
1 import React from 'react'
2 import { connect } from 'react-redux'
3 import actions from 'actions'
4 import { Main, Config, Login, Loading, Modal } from './'
5 import moment from 'moment'
6 import { withI18n } from 'react-i18next'
7
8 const CORE_POLLING_TIME = 2 * 1000
9
10 class Container extends React.Component {
11   constructor(props) {
12     super(props)
13     this.redirectRoot = this.redirectRoot.bind(this)
14   }
15
16   redirectRoot(props) {
17     const {
18       authOk,
19       configKnown,
20       configured,
21       location,
22       accountInit
23     } = props
24
25     if (!authOk || !configKnown) {
26       return
27     }
28
29     if (accountInit) {
30       if (location.pathname === '/'|| location.pathname.indexOf('initialization') >= 0) {
31         this.props.showRoot()
32       }
33     } else {
34       this.props.showInitialization()
35     }
36   }
37
38   componentDidMount() {
39     this.props.fetchAccountItem().then(resp => {
40       if (resp.data.length == 0) {
41         this.props.updateAccountInit(false)
42         this.redirectRoot(this.props)
43       }else{
44         const aliasArray = resp.data.map(account => account.alias)
45         if(!aliasArray.includes(this.props.currentAccount)){
46           this.props.setDefaultAccount()
47         }
48       }
49     })
50     if(this.props.lng === 'zh'){
51       moment.locale('zh-cn')
52     }else{
53       moment.locale(this.props.lng)
54     }
55   }
56
57   componentWillMount() {
58     this.props.fetchInfo().then(() => {
59       this.redirectRoot(this.props)
60     })
61
62     setInterval(() => this.props.fetchInfo(), CORE_POLLING_TIME)
63   }
64
65   componentWillReceiveProps(nextProps) {
66     if (nextProps.accountInit != this.props.accountInit ||
67         nextProps.configured != this.props.configured ||
68         nextProps.location.pathname != this.props.location.pathname) {
69       this.redirectRoot(nextProps)
70     }
71
72   }
73
74   render() {
75     let layout
76
77     const { i18n, t } = this.props
78     i18n.on('languageChanged', function(lng) {
79       if(lng === 'zh'){
80         moment.locale('zh-cn')
81       }else{
82         moment.locale(lng)
83       }
84     })
85
86     if (!this.props.authOk) {
87       layout = <Login/>
88     } else if (!this.props.configKnown) {
89       return <Loading>{t('main.loading')}</Loading>
90     } else if (!this.props.configured) {
91       layout = <Config>{this.props.children}</Config>
92     } else if (!this.props.accountInit){
93       layout = <Config>{this.props.children}</Config>
94     } else{
95       layout = <Main>{this.props.children}</Main>
96     }
97
98     return <div>
99       {layout}
100       <Modal />
101
102       {/* For copyToClipboard(). TODO: move this some place cleaner. */}
103       <input
104         id='_copyInput'
105         onChange={() => 'do nothing'}
106         value='dummy'
107         style={{display: 'none'}}
108       />
109     </div>
110   }
111 }
112
113 export default connect(
114   (state) => ({
115     authOk: !state.core.requireClientToken || state.core.validToken,
116     configKnown: state.core.configKnown,
117     configured: true,
118     accountInit: state.core.accountInit,
119     currentAccount: state.core.currentAccount
120   }),
121   (dispatch) => ({
122     fetchInfo: options => dispatch(actions.core.fetchCoreInfo(options)),
123     showRoot: () => dispatch(actions.app.showRoot),
124     showInitialization: () => dispatch(actions.app.showInitialization()),
125     updateAccountInit: (param) => dispatch(actions.app.updateAccountInit(param)),
126     fetchAccountItem: () => dispatch(actions.account.fetchItems()),
127     setDefaultAccount:() => dispatch(actions.account.setDefaultAccount())
128   })
129 )( withI18n() (Container) )