BasicLayout.tsx 3.23 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
 */

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7
import { ConnectState, ConnectProps } from '@/models/connect';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
8
import RightContent from '@/components/GlobalHeader/RightContent';
9
import { connect } from 'dva';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10
import React, { useState } from 'react';
11
import logo from '../assets/logo.svg';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
12 13
import Authorized from '@/utils/Authorized';
import { formatMessage } from 'umi-plugin-react/locale';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
14
import { isAntDesignProOrDev } from '@/utils/utils';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
15
import {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
16 17
  BasicLayout as ProLayoutComponents,
  BasicLayoutProps as ProLayoutComponentsProps,
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
18 19 20
  MenuDataItem,
  Settings,
} from '@ant-design/pro-layout';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
21
import Link from 'umi/link';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
22
export interface BasicLayoutProps extends ProLayoutComponentsProps, ConnectProps {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
23 24 25
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
26
  settings: Settings;
27
}
28
export type BasicLayoutContext = { [K in 'location']: BasicLayoutProps[K] } & {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
29 30 31
  breadcrumbNameMap: {
    [path: string]: MenuDataItem;
  };
32
};
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
33
/**
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
34
 * use Authorized check all menu item
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
35
 */
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
36 37

const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
38
  return menuList.map(item => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
39
    const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40 41 42 43
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });
};

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => {
  if (!isAntDesignProOrDev()) {
    return defaultDom;
  }
  return (
    <>
      {defaultDom}
      <div
        style={{
          textAlign: 'center',
          padding: '0px 20px 20px',
        }}
      >
        <a href="https://www.netlify.com">
          <img
            src="https://www.netlify.com/img/global/badges/netlify-dark.svg"
            alt="netlify logo"
          />
        </a>
      </div>
    </>
  );
};

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

74
  useState(() => {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75 76 77 78 79 80 81 82
    if (dispatch) {
      dispatch({
        type: 'user/fetchCurrent',
      });
      dispatch({
        type: 'settings/getSetting',
      });
    }
83
  });
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
84

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

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

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