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

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
17
export interface BasicLayoutProps extends BasicLayoutComponentsProps, ConnectProps {
何乐's avatar
何乐 committed
18
  breadcrumbNameMap: { [path: string]: MenuDataItem };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
19
  settings: Settings;
20 21
}

22
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
何乐's avatar
何乐 committed
23
  breadcrumbNameMap: { [path: string]: MenuDataItem };
24
};
25

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
26 27 28 29 30 31 32 33
/**
 * 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,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34
      children: item.children ? filterMenuData(item.children, locale) : [],
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35
    };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37 38 39 40
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });
};

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

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
86
export default connect(({ global, settings }: ConnectState) => ({
87
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
88
  settings,
89
}))(BasicLayout);