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

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

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

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

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

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

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