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

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