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 { ConnectProps, ConnectState } from '@/models/connect';
8
import ProLayout, {
9
  MenuDataItem,
10
  BasicLayoutProps as ProLayoutProps,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
11 12
  Settings,
} from '@ant-design/pro-layout';
13 14 15
import React, { useState } from 'react';

import Authorized from '@/utils/Authorized';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
16
import Link from 'umi/link';
17 18 19 20 21 22
import RightContent from '@/components/GlobalHeader/RightContent';
import { connect } from 'dva';
import { formatMessage } from 'umi-plugin-react/locale';
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

78
  useState(() => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
79 80 81 82 83 84 85 86
    if (dispatch) {
      dispatch({
        type: 'user/fetchCurrent',
      });
      dispatch({
        type: 'settings/getSetting',
      });
    }
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);