Info.js 3.17 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
import { Menu } from 'antd';
6
import GridContent from '@/components/PageHeaderWrapper/GridContent';
ζ„šι“'s avatar
ζ„šι“ committed
7
import styles from './Info.less';
8 9 10

const { Item } = Menu;

afc163's avatar
afc163 committed
11
export default
12 13 14
@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
afc163's avatar
afc163 committed
15
class Info extends Component {
16 17 18
  constructor(props) {
    super(props);
    const { match, location } = props;
afc163's avatar
afc163 committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
    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}/`, '');
35
    this.state = {
jim's avatar
jim committed
36
      mode: 'inline',
afc163's avatar
afc163 committed
37 38
      menuMap,
      selectKey: menuMap[key] ? key : 'base',
39 40
    };
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
41

qixian.cs@outlook.com's avatar
qixian.cs@outlook.com committed
42 43 44
  static getDerivedStateFromProps(props, state) {
    const { match, location } = props;
    let selectKey = location.pathname.replace(`${match.path}/`, '');
εΌ η§€ηŽ²'s avatar
εΌ η§€ηŽ² committed
45
    selectKey = state.menuMap[selectKey] ? selectKey : 'base';
qixian.cs@outlook.com's avatar
qixian.cs@outlook.com committed
46 47 48 49 50
    if (selectKey !== state.selectKey) {
      return { selectKey };
    }
    return null;
  }
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
51

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

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

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 }) => {
zinkey's avatar
zinkey committed
72
    router.push(`/account/settings/${key}`);
73 74 75 76
    this.setState({
      selectKey: key,
    });
  };
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
77

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

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