BasicLayout.tsx 1.91 KB
Newer Older
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
1
import { ConnectState, ConnectProps } from '@/models/connect';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
2
import RightContent from '@/components/GlobalHeader/RightContent';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
3
import { formatMessage } from 'umi-plugin-react/locale';
4
import { connect } from 'dva';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
5
import React, { useState } from 'react';
6 7
import logo from '../assets/logo.svg';

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8 9 10 11 12 13
import {
  BasicLayout as BasicLayoutComponents,
  BasicLayoutProps as BasicLayoutComponentsProps,
  MenuDataItem,
  Settings,
} from '@ant-design/pro-layout';
14

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

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

何乐's avatar
何乐 committed
24
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
25
  const { dispatch, children, route } = props;
26
  const { routes, authority } = route!;
何乐's avatar
何乐 committed
27 28 29
  /**
   * constructor
   */
30
  useState(() => {
何乐's avatar
何乐 committed
31
    dispatch!({ type: 'user/fetchCurrent' });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
32
    dispatch!({ type: 'settings/getSetting' });
何乐's avatar
何乐 committed
33
    dispatch!({ type: 'menu/getMenuData', payload: { routes, authority } });
34
  });
何乐's avatar
何乐 committed
35 36 37
  /**
   * init variables
   */
38
  const handleMenuCollapse = (payload: boolean) =>
何乐's avatar
何乐 committed
39
    dispatch!({ type: 'global/changeLayoutCollapsed', payload });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40

41
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
42 43 44 45 46 47 48 49 50 51
    <BasicLayoutComponents
      formatMessage={formatMessage}
      logo={logo}
      onChangeSetting={settings =>
        dispatch!({
          type: 'settings/changeSetting',
          payload: settings,
        })
      }
      onChangeLayoutCollapsed={handleMenuCollapse}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
52
      renderRightContent={RightProps => <RightContent {...RightProps} />}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
53 54 55 56
      {...props}
    >
      {children}
    </BasicLayoutComponents>
57 58 59
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
60
export default connect(({ global, settings, menu: menuModel }: ConnectState) => ({
61
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
62
  settings,
63 64 65
  menuData: menuModel.menuData,
  breadcrumbNameMap: menuModel.breadcrumbNameMap,
}))(BasicLayout);