BasicLayout.tsx 3.21 KB
Newer Older
zombieJ's avatar
zombieJ committed
1 2 3 4 5 6
/**
 * Ant Design Pro v4 use `@ant-design/pro-layout` to handle Layout.
 * You can view component api by:
 * https://github.com/ant-design/ant-design-pro-layout
 */

7
import ProLayout, {
8
  MenuDataItem,
9
  BasicLayoutProps as ProLayoutProps,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10 11
  Settings,
} from '@ant-design/pro-layout';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12
import React, { useEffect } from 'react';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
13
import Link from 'umi/link';
14 15
import { connect } from 'dva';
import { formatMessage } from 'umi-plugin-react/locale';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
16 17 18 19

import Authorized from '@/utils/Authorized';
import RightContent from '@/components/GlobalHeader/RightContent';
import { ConnectProps, ConnectState } from '@/models/connect';
20 21 22
import { isAntDesignPro } from '@/utils/utils';
import logo from '../assets/logo.svg';

23
export interface BasicLayoutProps extends ProLayoutProps, Omit<ConnectProps, 'location'> {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
24 25 26
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
27
  settings: Settings;
28
}
29
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30 31 32
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
33
};
34

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35
/**
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36
 * use Authorized check all menu item
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
37
 */
38 39 40 41 42 43
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  menuList.map(item => {
    const localItem = {
      ...item,
      children: item.children ? menuDataRender(item.children) : [],
    };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44 45 46
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
47
const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
48
  if (!isAntDesignPro()) {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
49 50 51 52 53 54 55
    return defaultDom;
  }
  return (
    <>
      {defaultDom}
      <div
        style={{
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
56
          padding: '0px 24px 24px',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
57
          textAlign: 'center',
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
58 59
        }}
      >
60
        <a href="https://www.netlify.com" target="_blank" rel="noopener noreferrer">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
61
          <img
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
62
            src="https://www.netlify.com/img/global/badges/netlify-color-bg.svg"
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
63
            width="82px"
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
64 65 66 67 68 69 70 71
            alt="netlify logo"
          />
        </a>
      </div>
    </>
  );
};

何乐's avatar
何乐 committed
72
const BasicLayout: React.FC<BasicLayoutProps> = props => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
73
  const { dispatch, children, settings } = props;
何乐's avatar
何乐 committed
74 75 76
  /**
   * constructor
   */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
78
  useEffect(() => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
79 80 81 82 83 84 85 86
    if (dispatch) {
      dispatch({
        type: 'user/fetchCurrent',
      });
      dispatch({
        type: 'settings/getSetting',
      });
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
87
  }, []);
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
88

何乐's avatar
何乐 committed
89 90 91
  /**
   * init variables
   */
92
  const handleMenuCollapse = (payload: boolean): void =>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
93 94
    dispatch &&
    dispatch({
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
95 96 97 98
      type: 'global/changeLayoutCollapsed',
      payload,
    });

99
  return (
100
    <ProLayout
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
101 102
      logo={logo}
      onCollapse={handleMenuCollapse}
103 104 105 106 107 108 109 110 111 112 113 114 115
      menuItemRender={(menuItemProps, defaultDom) => (
        <Link to={menuItemProps.path}>{defaultDom}</Link>
      )}
      breadcrumbRender={(routers = []) => [
        {
          path: '/',
          breadcrumbName: formatMessage({
            id: 'menu.home',
            defaultMessage: 'Home',
          }),
        },
        ...routers,
      ]}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
116
      footerRender={footerRender}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
117 118 119 120 121 122 123
      menuDataRender={menuDataRender}
      formatMessage={formatMessage}
      rightContentRender={rightProps => <RightContent {...rightProps} />}
      {...props}
      {...settings}
    >
      {children}
124
    </ProLayout>
125 126 127
  );
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
128
export default connect(({ global, settings }: ConnectState) => ({
129
  collapsed: global.collapsed,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
130
  settings,
131
}))(BasicLayout);