LoadingPage.js 2.75 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
// TODO: should use this.props.routes
import config from '../../../config/config';
const menuData = config['routes'];

10
// Conversion router to menu.
11 12 13 14 15 16 17 18 19
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) {
20 21 22
      const children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, id);
      // Reduce memory usage
      result.children = children;
23
    }
24
    delete result.routes;
25 26 27
    return result;
  });
}
jim's avatar
jim committed
28 29 30
/**
 * 根据菜单取得重定向地址.
 */
31 32
const MenuData = formatter(menuData[1].routes);
const routerData = config.routes;
jim's avatar
jim committed
33 34
const getRedirectData = () => {
  const redirectData = [];
jim's avatar
jim committed
35
  const getRedirect = item => {
jim's avatar
jim committed
36 37 38 39 40 41
    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
42
        item.children.forEach(children => {
jim's avatar
jim committed
43 44 45 46 47 48 49 50 51 52
          getRedirect(children);
        });
      }
    }
  };
  MenuData.forEach(getRedirect);
  return redirectData;
};
const redirectData = getRedirectData();

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

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

jim's avatar
jim committed
73 74 75
  componentWillUnmount() {
    unenquireScreen(this.enquireHandler);
  }
76

jim's avatar
jim committed
77 78 79 80 81
  hideLoading() {
    this.setState({
      loading: false,
    });
  }
82

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

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

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