BasicLayout.tsx 2.59 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
/**
 * default menuLocal
 */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
29
const filterMenuData = (menuList: MenuDataItem[], locale: boolean): MenuDataItem[] => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30 31 32
  return menuList.map(item => {
    const localItem = {
      ...item,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33 34 35 36
      name:
        item.locale && locale
          ? formatMessage({ id: item.locale, defaultMessage: item.name })
          : item.name,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
      children: item.children ? filterMenuData(item.children, locale) : [],
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38
    };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39

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

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

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