Info.js 3.2 KB
Newer Older
J
jim 已提交
1
import React, { Component } from 'react';
陈帅 已提交
2
import { connect } from 'dva';
Z
zinkey 已提交
3
import router from 'umi/router';
陈帅 已提交
4
import { FormattedMessage } from 'umi/locale';
陈帅 已提交
5 6
import { Menu } from 'antd';
import styles from './Info.less';
7
import GridContent from '@/layouts/GridContent';
陈帅 已提交
8 9 10 11 12 13

const { Item } = Menu;

@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
J
jim 已提交
14
export default class Info extends Component {
陈帅 已提交
15 16 17 18
  constructor(props) {
    super(props);
    const { match, location } = props;
    this.state = {
J
jim 已提交
19
      mode: 'inline',
张秀玲 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
      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"
          />
        ),
      },
陈帅 已提交
35
    };
张秀玲 已提交
36 37 38 39 40 41
    let key = location.pathname.replace(`${match.path}/`, '');
    const { menuMap } = this.state;
    key = menuMap[key] ? key : 'base';
    this.setState({
      selectKey: key,
    });
陈帅 已提交
42
  }
陈帅 已提交
43 44 45 46 47 48 49 50 51 52

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

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

Q
qixian.cs@outlook.com 已提交
53 54 55
  static getDerivedStateFromProps(props, state) {
    const { match, location } = props;
    let selectKey = location.pathname.replace(`${match.path}/`, '');
张秀玲 已提交
56
    selectKey = state.menuMap[selectKey] ? selectKey : 'base';
Q
qixian.cs@outlook.com 已提交
57 58 59 60 61
    if (selectKey !== state.selectKey) {
      return { selectKey };
    }
    return null;
  }
陈帅 已提交
62

陈帅 已提交
63
  getmenu = () => {
张秀玲 已提交
64
    const { menuMap } = this.state;
J
jim 已提交
65
    return Object.keys(menuMap).map(item => <Item key={item}>{menuMap[item]}</Item>);
陈帅 已提交
66
  };
陈帅 已提交
67

陈帅 已提交
68
  getRightTitle = () => {
张秀玲 已提交
69
    const { selectKey, menuMap } = this.state;
陈帅 已提交
70
    return menuMap[selectKey];
陈帅 已提交
71
  };
陈帅 已提交
72

陈帅 已提交
73
  selectKey = ({ key }) => {
Z
zinkey 已提交
74
    router.push(`/account/settings/${key}`);
陈帅 已提交
75 76 77 78
    this.setState({
      selectKey: key,
    });
  };
陈帅 已提交
79

J
jim 已提交
80 81 82 83
  resize = () => {
    if (!this.main) {
      return;
    }
J
jim 已提交
84 85 86 87 88 89 90 91 92 93 94 95
    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,
      });
J
jim 已提交
96 97
    });
  };
陈帅 已提交
98

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