BasicLayout.tsx 2.65 KB
Newer Older
zombieJ's avatar
zombieJ committed
1 2 3 4 5 6
/**
 * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
 * You can view component api by:
 * https://github.com/ant-design/ant-design-pro-layout
 */

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7
import { ConnectState, ConnectProps } from '@/models/connect';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8
import RightContent from '@/components/GlobalHeader/RightContent';
9
import { connect } from 'dva';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10
import React, { useState } from 'react';
11
import logo from '../assets/logo.svg';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12 13
import Authorized from '@/utils/Authorized';
import { formatMessage } from 'umi-plugin-react/locale';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
14
import {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
15 16
  BasicLayout as ProLayoutComponents,
  BasicLayoutProps as ProLayoutComponentsProps,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17 18 19
  MenuDataItem,
  Settings,
} from '@ant-design/pro-layout';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
20
import Link from 'umi/link';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21
export interface BasicLayoutProps extends ProLayoutComponentsProps, ConnectProps {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
22 23 24
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25
  settings: Settings;
26
}
27
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
28 29 30
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
31
};
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32
/**
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33
 * use Authorized check all menu item
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34
 */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35 36

const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
  return menuList.map(item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38
    const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39 40 41 42
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });
};

何乐's avatar
何乐 committed
43
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44
  const { dispatch, children, settings } = props;
何乐's avatar
何乐 committed
45 46 47
  /**
   * constructor
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48

49
  useState(() => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50 51 52 53 54 55 56 57
    if (dispatch) {
      dispatch({
        type: 'user/fetchCurrent',
      });
      dispatch({
        type: 'settings/getSetting',
      });
    }
58
  });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59

何乐's avatar
何乐 committed
60 61 62
  /**
   * init variables
   */
63
  const handleMenuCollapse = (payload: boolean) =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64 65
    dispatch &&
    dispatch({
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
66 67 68 69
      type: 'global/changeLayoutCollapsed',
      payload,
    });

70
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
71
    <ProLayoutComponents
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      logo={logo}
      onCollapse={handleMenuCollapse}
      menuItemRender={(menuItemProps, defaultDom) => {
        return <Link to={menuItemProps.path}>{defaultDom}</Link>;
      }}
      breadcrumbRender={(routers = []) => {
        return [
          {
            path: '/',
            breadcrumbName: formatMessage({
              id: 'menu.home',
              defaultMessage: 'Home',
            }),
          },
          ...routers,
        ];
      }}
      menuDataRender={menuDataRender}
      formatMessage={formatMessage}
      rightContentRender={rightProps => <RightContent {...rightProps} />}
      {...props}
      {...settings}
    >
      {children}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
96
    </ProLayoutComponents>
97 98 99
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
100
export default connect(({ global, settings }: ConnectState) => ({
101
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
102
  settings,
103
}))(BasicLayout);