Info.js 3.31 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
2
import { connect } from 'dva';
ζ„šι“'s avatar
ζ„šι“ committed
3
import { routerRedux } from 'dva/router';
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
4
import { FormattedMessage } from 'react-intl';
5 6
import { Menu } from 'antd';
import styles from './Info.less';
ζ„šι“'s avatar
ζ„šι“ committed
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
  constructor(props) {
    super(props);
    const { match, location } = props;
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
18 19 20
    const key = location.pathname.replace(`${match.path}/`, '');
    // let key = location.pathname.replace(`${match.path}/`, '');
    // key = this.state.menuMap[key] ? key : 'base';
21 22
    this.state = {
      selectKey: key,
jim's avatar
jim committed
23
      mode: 'inline',
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
      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"
          />
        ),
      },
39 40
    };
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
41 42 43 44 45 46 47 48 49 50

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

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

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

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

jim's avatar
jim committed
79 80 81 82
  resize = () => {
    if (!this.main) {
      return;
    }
jim's avatar
jim committed
83 84 85 86 87 88 89 90 91 92 93 94
    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
95 96
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
97

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