BasicLayout.tsx 3.72 KB
Newer Older
zombieJ's avatar
zombieJ committed
1 2 3 4 5
/**
 * 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
 */
6
import { ConnectProps, ConnectState } from '@/models/connect';
7
import ProLayout, {
8
  MenuDataItem,
9
  BasicLayoutProps as ProLayoutProps,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
10
  Settings,
duanledexianxianxian's avatar
duanledexianxianxian committed
11
  SettingDrawer,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
12
} from '@ant-design/pro-layout';
13 14
import React, { useState } from 'react';
import Authorized from '@/utils/Authorized';
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
15
import Link from 'umi/link';
16 17 18 19 20 21
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';

22
export interface BasicLayoutProps extends ProLayoutProps, Omit<ConnectProps, 'location'> {
้™ˆๅธ…'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
 */
duanledexianxianxian's avatar
duanledexianxianxian committed
36

37 38
const menuDataRender = (menuList: MenuDataItem[]): MenuDataItem[] =>
  menuList.map(item => {
duanledexianxianxian's avatar
duanledexianxianxian committed
39
    // ้ๅŽ†็ป™ๆ‰€ๆœ‰็š„childrenๅŠ ไธŠไบ†[]
duanledexianxianxian's avatar
duanledexianxianxian committed
40
    const localItem = { ...item, children: item.children ? menuDataRender(item.children) : [] };
duanledexianxianxian's avatar
duanledexianxianxian committed
41
    // ๆฃ€ๆŸฅ่œๅ•้กนๆ˜ฏๅฆๆœ‰ๆƒ้™
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
42 43 44
    return Authorized.check(item.authority, localItem, null) as MenuDataItem;
  });

duanledexianxianxian's avatar
duanledexianxianxian committed
45
// footerๆธฒๆŸ“
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
46
const footerRender: BasicLayoutProps['footerRender'] = (_, defaultDom) => {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
47
  if (!isAntDesignPro()) {
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
48 49
    return defaultDom;
  }
duanledexianxianxian's avatar
duanledexianxianxian committed
50

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
51 52 53 54 55
  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 89 90
  /**
   * init variables
   */
duanledexianxianxian's avatar
duanledexianxianxian committed
91

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 (
duanledexianxianxian's avatar
duanledexianxianxian committed
100
    <div className="kim-layout">
duanledexianxianxian's avatar
duanledexianxianxian committed
101
      <ProLayout
duanledexianxianxian's avatar
duanledexianxianxian committed
102 103 104
        siderWidth={200}
        logo={false}
        navTheme="dark"
duanledexianxianxian's avatar
duanledexianxianxian committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118
        onCollapse={handleMenuCollapse}
        menuItemRender={(menuItemProps, defaultDom) => (
          <Link to={menuItemProps.path}>{defaultDom}</Link>
        )}
        breadcrumbRender={(routers = []) => [
          {
            path: '/',
            breadcrumbName: formatMessage({
              id: 'menu.home',
              defaultMessage: 'Home',
            }),
          },
          ...routers,
        ]}
duanledexianxianxian's avatar
duanledexianxianxian committed
119
        footerRender={false} // ไธๆ˜พ็คบfooter
duanledexianxianxian's avatar
duanledexianxianxian committed
120
        menuDataRender={menuDataRender} // ๆธฒๆŸ“ๅฏผ่ˆช่œๅ•ๅˆ—่กจ
duanledexianxianxian's avatar
duanledexianxianxian committed
121 122 123 124 125 126 127
        formatMessage={formatMessage}
        rightContentRender={rightProps => <RightContent {...rightProps} />}
        {...props}
        {...settings}
      >
        {children}
      </ProLayout>
duanledexianxianxian's avatar
duanledexianxianxian committed
128
      {/* ไธป้ข˜่ฎพ็ฝฎๆŠฝๅฑ‰ */}
duanledexianxianxian's avatar
duanledexianxianxian committed
129 130 131 132 133 134 135 136 137
      <SettingDrawer
        settings={settings}
        onSettingChange={config =>
          dispatch({
            type: 'settings/changeSetting',
            payload: config,
          })
        }
      />
duanledexianxianxian's avatar
duanledexianxianxian committed
138
    </div>
139 140 141
  );
};

้™ˆๅธ…'s avatar
้™ˆๅธ… committed
142
export default connect(({ global, settings }: ConnectState) => ({
143
  collapsed: global.collapsed,
้™ˆๅธ…'s avatar
้™ˆๅธ… committed
144
  settings,
145
}))(BasicLayout);