/* global window */ /* global document */ import React, { PureComponent, Fragment } from 'react' import PropTypes from 'prop-types' import withRouter from 'umi/withRouter' import { connect } from 'dva' import { MyLayout } from 'components' import { BackTop, Layout, Drawer } from 'antd' import { GlobalFooter } from 'ant-design-pro' import { enquireScreen, unenquireScreen } from 'enquire-js' import { config, pathMatchRegexp, langFromPath } from 'utils' import Error from '../pages/404' import styles from './PrimaryLayout.less' const { Content } = Layout const { Header, Bread, Sider } = MyLayout @withRouter @connect(({ app, loading }) => ({ app, loading })) class PrimaryLayout extends PureComponent { state = { isMobile: false, } componentDidMount() { this.enquireHandler = enquireScreen(mobile => { const { isMobile } = this.state if (isMobile !== mobile) { this.setState({ isMobile: mobile, }) } }) } componentWillUnmount() { unenquireScreen(this.enquireHandler) } onCollapseChange = collapsed => { this.props.dispatch({ type: 'app/handleCollapseChange', payload: collapsed, }) } render() { const { app, location, dispatch, children } = this.props const { user, theme, routeList, collapsed, permissions, notifications, } = app const { isMobile } = this.state const { onCollapseChange } = this // Localized route name. const lang = langFromPath(location.pathname) const newRouteList = lang !== 'en' ? routeList.map(item => { const { name, ...other } = item return { ...other, name: (item[lang] || {}).name || name, } }) : routeList // Find a route that matches the pathname. const currentRoute = newRouteList.find( _ => _.route && pathMatchRegexp(_.route, location.pathname) ) // Query whether you have permission to enter this page const hasPermission = currentRoute ? permissions.visit.includes(currentRoute.id) : false // MenuParentId is equal to -1 is not a available menu. const menus = newRouteList.filter(_ => _.menuParentId !== '-1') const headerProps = { menus, collapsed, notifications, onCollapseChange, avatar: user.avatar, username: user.username, fixed: config.fixedHeader, onAllNotificationsRead() { dispatch({ type: 'app/allNotificationsRead' }) }, onSignOut() { dispatch({ type: 'app/signOut' }) }, } const siderProps = { theme, menus, isMobile, collapsed, onCollapseChange, onThemeChange(theme) { dispatch({ type: 'app/handleThemeChange', payload: theme, }) }, } return ( {isMobile ? ( ) : ( )}
{hasPermission ? children : } document.querySelector('#primaryLayout')} />
) } } PrimaryLayout.propTypes = { children: PropTypes.element.isRequired, location: PropTypes.object, dispatch: PropTypes.func, app: PropTypes.object, loading: PropTypes.object, } export default PrimaryLayout