Info.js 3.23 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
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';
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,
}))
jim's avatar
jim committed
14
export default class Info extends Component {
15 16 17 18
  constructor(props) {
    super(props);
    const { match, location } = props;
    this.state = {
jim's avatar
jim committed
19
      mode: 'inline',
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
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
    };
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
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
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
43 44 45 46 47 48 49 50 51 52

  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
53 54 55
  static getDerivedStateFromProps(props, state) {
    const { match, location } = props;
    let selectKey = location.pathname.replace(`${match.path}/`, '');
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
56
    selectKey = state.menuMap[selectKey] ? selectKey : 'base';
qixian.cs@outlook.com's avatar
qixian.cs@outlook.com committed
57 58 59 60 61
    if (selectKey !== state.selectKey) {
      return { selectKey };
    }
    return null;
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
62

63
  getmenu = () => {
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
64
    const { menuMap } = this.state;
jim's avatar
jim committed
65
    return Object.keys(menuMap).map(item => <Item key={item}>{menuMap[item]}</Item>);
66
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
67

68
  getRightTitle = () => {
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
69
    const { selectKey, menuMap } = this.state;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
70
    return menuMap[selectKey];
71
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
72

73
  selectKey = ({ key }) => {
zinkey's avatar
zinkey committed
74
    router.push(`/account/settings/${key}`);
75 76 77 78
    this.setState({
      selectKey: key,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
79

jim's avatar
jim committed
80 81 82 83
  resize = () => {
    if (!this.main) {
      return;
    }
jim's avatar
jim committed
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,
      });
jim's avatar
jim committed
96 97
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
98

99
  render() {
ζ„šι“'s avatar
ζ„šι“ committed
100
    const { children, currentUser } = this.props;
101 102 103
    if (!currentUser.userid) {
      return '';
    }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
104
    const { mode, selectKey } = this.state;
105
    return (
jim's avatar
jim committed
106
      <GridContent>
jim's avatar
jim committed
107 108
        <div
          className={styles.main}
jim's avatar
jim committed
109
          ref={ref => {
jim's avatar
jim committed
110 111 112
            this.main = ref;
          }}
        >
jim's avatar
jim committed
113
          <div className={styles.leftmenu}>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
114
            <Menu mode={mode} selectedKeys={[selectKey]} onClick={this.selectKey}>
jim's avatar
jim committed
115 116 117 118 119
              {this.getmenu()}
            </Menu>
          </div>
          <div className={styles.right}>
            <div className={styles.title}>{this.getRightTitle()}</div>
ζ„šι“'s avatar
ζ„šι“ committed
120
            {children}
jim's avatar
jim committed
121
          </div>
122
        </div>
jim's avatar
jim committed
123
      </GridContent>
124 125 126
    );
  }
}