router.js 1.21 KB
Newer Older
1 2
import React from 'react';
import { Router, Route, Redirect } from 'dva/router';
afc163's avatar
afc163 committed
3 4
import { LocaleProvider } from 'antd';
import zhCN from 'antd/lib/locale-provider/zh_CN';
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import navData from './common/nav';

function getRoutes(data, level = 0) {
  return data.map((item, i) => {
    let children;
    if (item.children) {
      children = getRoutes(item.children, level + 1);
    }
    let homePageRedirect;
    if (level === 1 && i === 0) {
      let indexPath;
      // First children router
      if (item.children && item.children[0]) {
        indexPath = `/${item.path}/${item.children[0].path}`;
      } else {
        indexPath = item.path;
      }
      homePageRedirect = <Redirect from="/" to={indexPath} />;
    }
    if (item.noRoute) {
      return null;
    }
    return (
      <Route
        key={item.key || item.path || ''}
        path={item.path}
        breadcrumbName={item.name}
        component={item.component}
      >
        {homePageRedirect}
        {children}
      </Route>
    );
  });
}

function RouterConfig({ history }) {
  return (
afc163's avatar
afc163 committed
43 44 45 46 47
    <LocaleProvider locale={zhCN}>
      <Router history={history}>
        {getRoutes(navData)}
      </Router>
    </LocaleProvider>
48 49 50 51
  );
}

export default RouterConfig;