LoadingPage.js 2.83 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 => {
陈帅's avatar
陈帅 committed
62 63 64 65 66 67
      const { isMobile } = this.state;
      if (isMobile !== mobile) {
        this.setState({
          isMobile: mobile,
        });
      }
jim's avatar
jim committed
68
    });
69
    dispatch({
jim's avatar
jim committed
70 71 72
      type: 'user/fetchCurrent',
    });
    this.hideLoading();
jim's avatar
jim committed
73
    this.initSetting();
jim's avatar
jim committed
74
  }
75

jim's avatar
jim committed
76 77 78
  componentWillUnmount() {
    unenquireScreen(this.enquireHandler);
  }
79

jim's avatar
jim committed
80 81 82 83 84
  hideLoading() {
    this.setState({
      loading: false,
    });
  }
85

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

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

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