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

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
31
  return menuList.map(item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32
    const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33 34 35 36
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });
};

何乐's avatar
何乐 committed
37
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
commit  
ι™ˆεΈ… committed
38
  const { dispatch, children, settings, location } = props;
何乐's avatar
何乐 committed
39 40 41
  /**
   * constructor
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
42

43
  useState(() => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44 45 46 47 48 49
    dispatch!({
      type: 'user/fetchCurrent',
    });
    dispatch!({
      type: 'settings/getSetting',
    });
50
  });
何乐's avatar
何乐 committed
51 52 53
  /**
   * init variables
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
54

55
  const handleMenuCollapse = (payload: boolean) =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
56 57 58 59
    dispatch!({
      type: 'global/changeLayoutCollapsed',
      payload,
    });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
60

61
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    <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>
88 89 90
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
91
export default connect(({ global, settings }: ConnectState) => ({
92
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
93
  settings,
94
}))(BasicLayout);