BasicLayout.tsx 1.89 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 8 9 10
import {
  BasicLayout as BasicLayoutComponents,
  BasicLayoutProps as BasicLayoutComponentsProps,
  MenuDataItem,
  Settings,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
11
  SettingDrawer,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12
} from '@ant-design/pro-layout';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
13
import Link from 'umi/link';
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, settings } = props;
何乐'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' });
32
  });
何乐's avatar
何乐 committed
33 34 35
  /**
   * init variables
   */
36
  const handleMenuCollapse = (payload: boolean) =>
何乐's avatar
何乐 committed
37
    dispatch!({ type: 'global/changeLayoutCollapsed', payload });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38

39
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40 41 42 43
    <>
      <BasicLayoutComponents
        logo={logo}
        onCollapse={handleMenuCollapse}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44 45 46 47
        menuItemRender={(menuItemProps, defaultDom) => {
          return <Link to={menuItemProps.path}>{defaultDom}</Link>;
        }}
        rightContentRender={rightProps => <RightContent {...rightProps} />}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48
        {...props}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
49
        {...settings}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
50 51 52 53
      >
        {children}
      </BasicLayoutComponents>
      <SettingDrawer
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
54 55
        settings={settings}
        onSettingChange={config =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
56 57
          dispatch!({
            type: 'settings/changeSetting',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
58
            payload: config,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59 60 61 62
          })
        }
      />
    </>
63 64 65
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
66
export default connect(({ global, settings }: ConnectState) => ({
67
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
68
  settings,
69
}))(BasicLayout);