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'
8 const CORE_POLLING_TIME = 2 * 1000
10 class Container extends React.Component {
13 this.redirectRoot = this.redirectRoot.bind(this)
25 if (!authOk || !configKnown) {
30 if (location.pathname === '/'|| location.pathname.indexOf('initialization') >= 0) {
34 this.props.showInitialization()
39 this.props.fetchAccountItem().then(resp => {
40 if (resp.data.length == 0) {
41 this.props.updateAccountInit(false)
42 this.redirectRoot(this.props)
44 const aliasArray = resp.data.map(account => account.alias)
45 if(!aliasArray.includes(this.props.currentAccount)){
46 this.props.setDefaultAccount()
50 if(this.props.lng === 'zh'){
51 moment.locale('zh-cn')
53 moment.locale(this.props.lng)
57 componentWillMount() {
58 this.props.fetchInfo().then(() => {
59 this.redirectRoot(this.props)
62 setInterval(() => this.props.fetchInfo(), CORE_POLLING_TIME)
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)
77 const { i18n, t } = this.props
78 i18n.on('languageChanged', function(lng) {
80 moment.locale('zh-cn')
86 if (!this.props.authOk) {
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>
95 layout = <Main>{this.props.children}</Main>
102 {/* For copyToClipboard(). TODO: move this some place cleaner. */}
105 onChange={() => 'do nothing'}
107 style={{display: 'none'}}
113 export default connect(
115 authOk: !state.core.requireClientToken || state.core.validToken,
116 configKnown: state.core.configKnown,
118 accountInit: state.core.accountInit,
119 currentAccount: state.core.currentAccount
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())
129 )( withI18n() (Container) )