BasicLayout.tsx 3.27 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
const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => {
  if (!isAntDesignProOrDev()) {
    return defaultDom;
  }
  return (
    <>
      {defaultDom}
      <div
        style={{
          textAlign: 'center',
          padding: '0px 20px 20px',
        }}
      >
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
57
        <a href="https://www.netlify.com" target="_blank">
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
58
          <img
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
59 60
            src="https://www.netlify.com/img/global/badges/netlify-color-bg.svg"
            width={82}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
61 62 63 64 65 66 67 68
            alt="netlify logo"
          />
        </a>
      </div>
    </>
  );
};

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

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

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

96
  return (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
97
    <ProLayoutComponents
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
      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
115
      footerRender={footerRender}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
116 117 118 119 120 121 122
      menuDataRender={menuDataRender}
      formatMessage={formatMessage}
      rightContentRender={rightProps => <RightContent {...rightProps} />}
      {...props}
      {...settings}
    >
      {children}
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
123
    </ProLayoutComponents>
124 125 126
  );
};

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