LoadingPage.js 2.41 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
// TODO: should use this.props.routes
陈帅's avatar
陈帅 committed
7
import routerConfig from '../../../config/router.config';
8

9
// Conversion router to menu.
10 11
function formatter(data, parentPath = '', parentAuthority, parentName) {
  return data.map(item => {
陈帅's avatar
陈帅 committed
12 13 14 15 16 17 18 19
    let locale = 'menu';
    if (parentName && item.name) {
      locale = `${parentName}.${item.name}`;
    } else if (item.name) {
      locale = `menu.${item.name}`;
    } else if (parentName) {
      locale = parentName;
    }
20 21
    const result = {
      ...item,
陈帅's avatar
陈帅 committed
22
      locale,
23 24 25
      authority: item.authority || parentAuthority,
    };
    if (item.routes) {
陈帅's avatar
陈帅 committed
26
      const children = formatter(item.routes, `${parentPath}${item.path}/`, item.authority, locale);
27 28
      // Reduce memory usage
      result.children = children;
29
    }
30
    delete result.routes;
31 32 33
    return result;
  });
}
34
// get meun map data
陈帅's avatar
陈帅 committed
35
const MenuData = formatter(routerConfig[1].routes);
jim's avatar
jim committed
36

lijiehua's avatar
lijiehua committed
37
class LoadingPage extends PureComponent {
jim's avatar
jim committed
38 39 40 41
  state = {
    loading: true,
    isMobile: false,
  };
42

jim's avatar
jim committed
43
  componentDidMount() {
44
    const { dispatch } = this.props;
jim's avatar
jim committed
45
    this.enquireHandler = enquireScreen(mobile => {
陈帅's avatar
陈帅 committed
46 47 48 49 50 51
      const { isMobile } = this.state;
      if (isMobile !== mobile) {
        this.setState({
          isMobile: mobile,
        });
      }
jim's avatar
jim committed
52
    });
53
    dispatch({
jim's avatar
jim committed
54 55 56
      type: 'user/fetchCurrent',
    });
    this.hideLoading();
jim's avatar
jim committed
57
    this.initSetting();
jim's avatar
jim committed
58
  }
59

jim's avatar
jim committed
60 61 62
  componentWillUnmount() {
    unenquireScreen(this.enquireHandler);
  }
63

jim's avatar
jim committed
64 65 66 67 68
  hideLoading() {
    this.setState({
      loading: false,
    });
  }
69

jim's avatar
jim committed
70 71 72
  /**
   * get setting from url params
   */
jim's avatar
jim committed
73
  initSetting() {
74 75
    const { dispatch } = this.props;
    dispatch({
jim's avatar
jim committed
76
      type: 'setting/getSetting',
jim's avatar
jim committed
77 78
    });
  }
79

jim's avatar
jim committed
80
  render() {
81 82
    const { loading, isMobile } = this.state;
    if (loading) {
jim's avatar
jim committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
      return (
        <div
          style={{
            width: '100%',
            height: '100%',
            margin: 'auto',
            paddingTop: 50,
            textAlign: 'center',
          }}
        >
          <Spin size="large" />
        </div>
      );
    }
    return (
      <BasicLayout
99
        isMobile={isMobile}
jim's avatar
jim committed
100
        menuData={MenuData}
陈帅's avatar
陈帅 committed
101 102
        routerData={routerConfig}
        redirectData={[]}
jim's avatar
jim committed
103 104 105 106 107 108
        {...this.props}
      />
    );
  }
}

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