Info.js 3.18 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
2
import { connect } from 'dva';
zinkey's avatar
zinkey committed
3
import router from 'umi/router';
陈帅's avatar
陈帅 committed
4
import { FormattedMessage } from 'umi/locale';
陈帅's avatar
陈帅 committed
5
import { Menu } from 'antd';
6
import GridContent from '@/components/PageHeaderWrapper/GridContent';
愚道's avatar
愚道 committed
7
import styles from './Info.less';
陈帅's avatar
陈帅 committed
8 9 10 11 12 13

const { Item } = Menu;

@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
afc163's avatar
afc163 committed
14
class Info extends Component {
陈帅's avatar
陈帅 committed
15 16 17
  constructor(props) {
    super(props);
    const { match, location } = props;
afc163's avatar
afc163 committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
    const menuMap = {
      base: <FormattedMessage id="app.settings.menuMap.basic" defaultMessage="Basic Settings" />,
      security: (
        <FormattedMessage id="app.settings.menuMap.security" defaultMessage="Security Settings" />
      ),
      binding: (
        <FormattedMessage id="app.settings.menuMap.binding" defaultMessage="Account Binding" />
      ),
      notification: (
        <FormattedMessage
          id="app.settings.menuMap.notification"
          defaultMessage="New Message Notification"
        />
      ),
    };
    const key = location.pathname.replace(`${match.path}/`, '');
陈帅's avatar
陈帅 committed
34
    this.state = {
jim's avatar
jim committed
35
      mode: 'inline',
afc163's avatar
afc163 committed
36 37
      menuMap,
      selectKey: menuMap[key] ? key : 'base',
陈帅's avatar
陈帅 committed
38 39
    };
  }
陈帅's avatar
陈帅 committed
40

qixian.cs@outlook.com's avatar
qixian.cs@outlook.com committed
41 42 43
  static getDerivedStateFromProps(props, state) {
    const { match, location } = props;
    let selectKey = location.pathname.replace(`${match.path}/`, '');
张秀玲's avatar
张秀玲 committed
44
    selectKey = state.menuMap[selectKey] ? selectKey : 'base';
qixian.cs@outlook.com's avatar
qixian.cs@outlook.com committed
45 46 47 48 49
    if (selectKey !== state.selectKey) {
      return { selectKey };
    }
    return null;
  }
陈帅's avatar
陈帅 committed
50

51 52 53 54 55 56 57 58 59
  componentDidMount() {
    window.addEventListener('resize', this.resize);
    this.resize();
  }

  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  }

陈帅's avatar
陈帅 committed
60
  getmenu = () => {
张秀玲's avatar
张秀玲 committed
61
    const { menuMap } = this.state;
jim's avatar
jim committed
62
    return Object.keys(menuMap).map(item => <Item key={item}>{menuMap[item]}</Item>);
陈帅's avatar
陈帅 committed
63
  };
陈帅's avatar
陈帅 committed
64

陈帅's avatar
陈帅 committed
65
  getRightTitle = () => {
张秀玲's avatar
张秀玲 committed
66
    const { selectKey, menuMap } = this.state;
陈帅's avatar
陈帅 committed
67
    return menuMap[selectKey];
陈帅's avatar
陈帅 committed
68
  };
陈帅's avatar
陈帅 committed
69

陈帅's avatar
陈帅 committed
70
  selectKey = ({ key }) => {
zinkey's avatar
zinkey committed
71
    router.push(`/account/settings/${key}`);
陈帅's avatar
陈帅 committed
72 73 74 75
    this.setState({
      selectKey: key,
    });
  };
陈帅's avatar
陈帅 committed
76

jim's avatar
jim committed
77 78 79 80
  resize = () => {
    if (!this.main) {
      return;
    }
jim's avatar
jim committed
81 82 83 84 85 86 87 88 89 90 91 92
    requestAnimationFrame(() => {
      let mode = 'inline';
      const { offsetWidth } = this.main;
      if (this.main.offsetWidth < 641 && offsetWidth > 400) {
        mode = 'horizontal';
      }
      if (window.innerWidth < 768 && offsetWidth > 400) {
        mode = 'horizontal';
      }
      this.setState({
        mode,
      });
jim's avatar
jim committed
93 94
    });
  };
陈帅's avatar
陈帅 committed
95

陈帅's avatar
陈帅 committed
96
  render() {
愚道's avatar
愚道 committed
97
    const { children, currentUser } = this.props;
陈帅's avatar
陈帅 committed
98 99 100
    if (!currentUser.userid) {
      return '';
    }
陈帅's avatar
陈帅 committed
101
    const { mode, selectKey } = this.state;
陈帅's avatar
陈帅 committed
102
    return (
jim's avatar
jim committed
103
      <GridContent>
jim's avatar
jim committed
104 105
        <div
          className={styles.main}
jim's avatar
jim committed
106
          ref={ref => {
jim's avatar
jim committed
107 108 109
            this.main = ref;
          }}
        >
jim's avatar
jim committed
110
          <div className={styles.leftmenu}>
陈帅's avatar
陈帅 committed
111
            <Menu mode={mode} selectedKeys={[selectKey]} onClick={this.selectKey}>
jim's avatar
jim committed
112 113 114 115 116
              {this.getmenu()}
            </Menu>
          </div>
          <div className={styles.right}>
            <div className={styles.title}>{this.getRightTitle()}</div>
愚道's avatar
愚道 committed
117
            {children}
jim's avatar
jim committed
118
          </div>
陈帅's avatar
陈帅 committed
119
        </div>
jim's avatar
jim committed
120
      </GridContent>
陈帅's avatar
陈帅 committed
121 122 123
    );
  }
}
lijiehua's avatar
lijiehua committed
124 125

export default Info;