Info.js 2.72 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
陈帅's avatar
陈帅 committed
2
import { connect } from 'dva';
愚道's avatar
愚道 committed
3
import { routerRedux } from 'dva/router';
陈帅's avatar
陈帅 committed
4 5
import { Menu } from 'antd';
import styles from './Info.less';
愚道's avatar
愚道 committed
6
import GridContent from '../../layouts/GridContent';
陈帅's avatar
陈帅 committed
7 8 9 10 11

const { Item } = Menu;

const menuMap = {
  base: '基本设置',
ddcat1115's avatar
ddcat1115 committed
12 13 14
  security: '安全设置',
  binding: '账号绑定',
  notification: '新消息通知',
陈帅's avatar
陈帅 committed
15 16 17 18 19
};

@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
jim's avatar
jim committed
20
export default class Info extends Component {
陈帅's avatar
陈帅 committed
21 22 23 24 25 26 27
  constructor(props) {
    super(props);
    const { match, location } = props;
    let key = location.pathname.replace(`${match.path}/`, '');
    key = menuMap[key] ? key : 'base';
    this.state = {
      selectKey: key,
jim's avatar
jim committed
28
      mode: 'inline',
陈帅's avatar
陈帅 committed
29 30
    };
  }
陈帅's avatar
陈帅 committed
31 32 33 34 35 36 37 38 39 40

  componentDidMount() {
    window.addEventListener('resize', this.resize);
    this.resize();
  }

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

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

陈帅's avatar
陈帅 committed
51
  getmenu = () => {
jim's avatar
jim committed
52
    return Object.keys(menuMap).map(item => <Item key={item}>{menuMap[item]}</Item>);
陈帅's avatar
陈帅 committed
53
  };
陈帅's avatar
陈帅 committed
54

陈帅's avatar
陈帅 committed
55
  getRightTitle = () => {
陈帅's avatar
陈帅 committed
56 57
    const { selectKey } = this.state;
    return menuMap[selectKey];
陈帅's avatar
陈帅 committed
58
  };
陈帅's avatar
陈帅 committed
59

陈帅's avatar
陈帅 committed
60
  selectKey = ({ key }) => {
陈帅's avatar
陈帅 committed
61 62
    const { dispatch } = this.props;
    dispatch(routerRedux.push(`/account/settings/${key}`));
陈帅's avatar
陈帅 committed
63 64 65 66
    this.setState({
      selectKey: key,
    });
  };
陈帅's avatar
陈帅 committed
67

jim's avatar
jim committed
68 69 70 71
  resize = () => {
    if (!this.main) {
      return;
    }
jim's avatar
jim committed
72 73 74 75 76 77 78 79 80 81 82 83
    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
84 85
    });
  };
陈帅's avatar
陈帅 committed
86

陈帅's avatar
陈帅 committed
87
  render() {
愚道's avatar
愚道 committed
88
    const { children, currentUser } = this.props;
陈帅's avatar
陈帅 committed
89 90 91
    if (!currentUser.userid) {
      return '';
    }
陈帅's avatar
陈帅 committed
92
    const { mode, selectKey } = this.state;
陈帅's avatar
陈帅 committed
93
    return (
jim's avatar
jim committed
94
      <GridContent>
jim's avatar
jim committed
95 96
        <div
          className={styles.main}
jim's avatar
jim committed
97
          ref={ref => {
jim's avatar
jim committed
98 99 100
            this.main = ref;
          }}
        >
jim's avatar
jim committed
101
          <div className={styles.leftmenu}>
陈帅's avatar
陈帅 committed
102
            <Menu mode={mode} selectedKeys={[selectKey]} onClick={this.selectKey}>
jim's avatar
jim committed
103 104 105 106 107
              {this.getmenu()}
            </Menu>
          </div>
          <div className={styles.right}>
            <div className={styles.title}>{this.getRightTitle()}</div>
愚道's avatar
愚道 committed
108
            {children}
jim's avatar
jim committed
109
          </div>
陈帅's avatar
陈帅 committed
110
        </div>
jim's avatar
jim committed
111
      </GridContent>
陈帅's avatar
陈帅 committed
112 113 114
    );
  }
}