BasicLayout.tsx 2.61 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
import {
  BasicLayout as BasicLayoutComponents,
  BasicLayoutProps as BasicLayoutComponentsProps,
  MenuDataItem,
  Settings,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
19
  SettingDrawer,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
20
} from '@ant-design/pro-layout';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21
import Link from 'umi/link';
22

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
23
export interface BasicLayoutProps extends BasicLayoutComponentsProps, ConnectProps {
何乐's avatar
何乐 committed
24
  breadcrumbNameMap: { [path: string]: MenuDataItem };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25
  settings: Settings;
26 27
}

28
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
何乐's avatar
何乐 committed
29
  breadcrumbNameMap: { [path: string]: MenuDataItem };
30
};
31

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32 33 34 35 36 37 38 39 40 41 42 43 44
/**
 * default menuLocal
 */
const filterMenuData = (menuList: MenuDataItem[], locale: boolean) => {
  return menuList.map(item => {
    const localItem = {
      ...item,
      name: item.locale && locale ? formatMessage({ id: item.locale }) : item.name,
    };
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });
};

何乐's avatar
何乐 committed
45
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46
  const { dispatch, children, settings } = props;
何乐's avatar
何乐 committed
47 48 49
  /**
   * constructor
   */
50
  useState(() => {
何乐's avatar
何乐 committed
51
    dispatch!({ type: 'user/fetchCurrent' });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52
    dispatch!({ type: 'settings/getSetting' });
53
  });
何乐's avatar
何乐 committed
54 55 56
  /**
   * init variables
   */
57
  const handleMenuCollapse = (payload: boolean) =>
何乐's avatar
何乐 committed
58
    dispatch!({ type: 'global/changeLayoutCollapsed', payload });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59 60 61
  const {
    menu: { locale },
  } = settings;
62
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63 64 65 66
    <>
      <BasicLayoutComponents
        logo={logo}
        onCollapse={handleMenuCollapse}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67 68 69
        menuItemRender={(menuItemProps, defaultDom) => {
          return <Link to={menuItemProps.path}>{defaultDom}</Link>;
        }}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
70
        filterMenuData={menuList => filterMenuData(menuList, locale)}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
71
        rightContentRender={rightProps => <RightContent {...rightProps} />}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
72
        {...props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73
        {...settings}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
74 75 76 77
      >
        {children}
      </BasicLayoutComponents>
      <SettingDrawer
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
78 79
        settings={settings}
        onSettingChange={config =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
80 81
          dispatch!({
            type: 'settings/changeSetting',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
82
            payload: config,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
83 84 85 86
          })
        }
      />
    </>
87 88 89
  );
};

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