forked from 0x1428571429/microfrontend-base-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.component.js
More file actions
79 lines (73 loc) · 2.62 KB
/
root.component.js
File metadata and controls
79 lines (73 loc) · 2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from 'react'
import { Provider } from 'react-redux'
import BasicLayout from './layouts/BasicLayout'
import UserLayout from './layouts/UserLayout'
import Authorized, { reloadAuthorized } from './utils/Authorized'
import { setAuthority } from './utils/authority'
import { getQueryString } from './utils/utils'
import { loginPage, logoutPage } from './utils/url'
import { Router, HashRouter, Route, hashHistory, Switch, Redirect } from 'react-router-dom'
import { post, get } from 'Util/request'
import { getRouterData } from './common/router'
import { pushStore } from './common/menu'
import _ from 'lodash'
import createHistory from 'history/createBrowserHistory'
const history = createHistory()
const location = history.location
const { AuthorizedRoute } = Authorized
export default class RootComponent extends React.Component {
state = {
store: this.props.store,
globalEventDistributor: this.props.globalEventDistributor,
currentUser: { name: '' },
isRender: true
};
componentDidCatch(error, info) {
console.log(error, info)
}
setStore(store) {
this.setState({ ...this.state, store: store })
}
setGlobalEventDistributor(globalEventDistributor) {
this.setState({ ...this.state, globalEventDistributor: globalEventDistributor })
}
async componentWillMount() {
this.initMenu()
this.props.history.listen((location, action) => {
if (action === 'PUSH') { this.props.globalEventDistributor.dispatch({ type: 'to', path: location.pathname, owner: 'base' }) }
})
}
initMenu() {
let store = this.state.globalEventDistributor.getState()
let menu = []
Object.keys(store).forEach((name) => {
if (store[name].menu) {
if (_.isArray(store[name].menu)) {
store[name].menu.forEach((item) => {
pushStore(item)
})
} else {
pushStore(store[name].menu)
}
}
})
}
render() {
let ret = <div></div>
const routerData = getRouterData()
let customProps = { routerData: routerData, globalEventDistributor: this.state.globalEventDistributor }
if (this.state.store && this.state.globalEventDistributor && this.state.isRender) {
ret = <Provider store={this.state.store}>
<Router history={this.props.history}>
<Switch>
<AuthorizedRoute
path='/'
render={props => <BasicLayout {...customProps} {...props} currentUser={this.state.currentUser} />}
/>
</Switch>
</Router>
</Provider>
}
return ret
}
}