BasicLayout.tsx 2.62 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 15 16 17 18 19
import {
  BasicLayout as BasicLayoutComponents,
  BasicLayoutProps as BasicLayoutComponentsProps,
  MenuDataItem,
  Settings,
} from '@ant-design/pro-layout';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
20
import Link from 'umi/link';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21
export interface BasicLayoutProps extends BasicLayoutComponentsProps, 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, location } = props;
何乐's avatar
何乐 committed
45 46 47
  /**
   * constructor
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48

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

61
  const handleMenuCollapse = (payload: boolean) =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
62 63 64 65 66
    dispatch!({
      type: 'global/changeLayoutCollapsed',
      payload,
    });

67
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    <BasicLayoutComponents
      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}
    </BasicLayoutComponents>
94 95 96
  );
};

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