LoadingPage.js 2.66 KB
Newer Older
jim's avatar
jim committed
1 2 3
import React, { PureComponent } from 'react';
import { Spin } from 'antd';
import { connect } from 'dva';
jim's avatar
jim committed
4
import { enquireScreen, unenquireScreen } from 'enquire-js';
jim's avatar
jim committed
5
import BasicLayout from './BasicLayout';
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
// TODO: should use this.props.routes
import config from '../../../config/config';
const menuData = config['routes'];

// change router to menu.
function formatter(data, parentPath = '', parentAuthority, parentName) {
  return data.map(item => {
    const id = parentName ? `${parentName}.${item.name}` : `menu.${item.name}`;
    const result = {
      ...item,
      locale: id,
      authority: item.authority || parentAuthority,
    };
    if (item.routes) {
      result.children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, id);
    }
    return result;
  });
}
jim's avatar
jim committed
25 26 27
/**
 * 根据菜单取得重定向地址.
 */
28 29
const MenuData = formatter(menuData[1].routes);
const routerData = config.routes;
jim's avatar
jim committed
30 31
const getRedirectData = () => {
  const redirectData = [];
jim's avatar
jim committed
32
  const getRedirect = item => {
jim's avatar
jim committed
33 34 35 36 37 38
    if (item && item.children) {
      if (item.children[0] && item.children[0].path) {
        redirectData.push({
          from: `${item.path}`,
          to: `${item.children[0].path}`,
        });
jim's avatar
jim committed
39
        item.children.forEach(children => {
jim's avatar
jim committed
40 41 42 43 44 45 46 47 48 49
          getRedirect(children);
        });
      }
    }
  };
  MenuData.forEach(getRedirect);
  return redirectData;
};
const redirectData = getRedirectData();

lijiehua's avatar
lijiehua committed
50
class LoadingPage extends PureComponent {
jim's avatar
jim committed
51 52 53 54
  state = {
    loading: true,
    isMobile: false,
  };
55

jim's avatar
jim committed
56
  componentDidMount() {
57
    const { dispatch } = this.props;
jim's avatar
jim committed
58
    this.enquireHandler = enquireScreen(mobile => {
jim's avatar
jim committed
59 60 61 62
      this.setState({
        isMobile: mobile,
      });
    });
63
    dispatch({
jim's avatar
jim committed
64 65 66
      type: 'user/fetchCurrent',
    });
    this.hideLoading();
jim's avatar
jim committed
67
    this.initSetting();
jim's avatar
jim committed
68
  }
69

jim's avatar
jim committed
70 71 72
  componentWillUnmount() {
    unenquireScreen(this.enquireHandler);
  }
73

jim's avatar
jim committed
74 75 76 77 78
  hideLoading() {
    this.setState({
      loading: false,
    });
  }
79

jim's avatar
jim committed
80 81 82
  /**
   * get setting from url params
   */
jim's avatar
jim committed
83
  initSetting() {
84 85
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
86
      type: 'setting/getSetting',
jim's avatar
jim committed
87 88
    });
  }
89

jim's avatar
jim committed
90
  render() {
91 92
    const { loading, isMobile } = this.state;
    if (loading) {
jim's avatar
jim committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
      return (
        <div
          style={{
            width: '100%',
            height: '100%',
            margin: 'auto',
            paddingTop: 50,
            textAlign: 'center',
          }}
        >
          <Spin size="large" />
        </div>
      );
    }
    return (
      <BasicLayout
109
        isMobile={isMobile}
jim's avatar
jim committed
110
        menuData={MenuData}
愚道's avatar
愚道 committed
111
        routerData={routerData}
jim's avatar
jim committed
112 113 114 115 116 117 118
        redirectData={redirectData}
        {...this.props}
      />
    );
  }
}

lijiehua's avatar
lijiehua committed
119
export default connect()(LoadingPage);