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

const { Item } = Menu;

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

@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
jim's avatar
jim committed
21
export default class Info extends Component {
陈帅's avatar
陈帅 committed
22 23 24 25 26 27 28
  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
29
      mode: 'inline',
陈帅's avatar
陈帅 committed
30 31
    };
  }
陈帅's avatar
陈帅 committed
32 33 34 35 36 37 38 39 40 41

  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
42 43 44 45 46 47 48 49 50
  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
51

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

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

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

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

陈帅's avatar
陈帅 committed
88 89 90 91 92
  render() {
    const { match, routerData, currentUser } = this.props;
    if (!currentUser.userid) {
      return '';
    }
陈帅's avatar
陈帅 committed
93
    const { mode, selectKey } = this.state;
陈帅's avatar
陈帅 committed
94
    return (
jim's avatar
jim committed
95
      <GridContent>
jim's avatar
jim committed
96 97
        <div
          className={styles.main}
jim's avatar
jim committed
98
          ref={ref => {
jim's avatar
jim committed
99 100 101
            this.main = ref;
          }}
        >
jim's avatar
jim committed
102
          <div className={styles.leftmenu}>
陈帅's avatar
陈帅 committed
103
            <Menu mode={mode} selectedKeys={[selectKey]} onClick={this.selectKey}>
jim's avatar
jim committed
104 105 106 107 108 109 110 111 112 113
              {this.getmenu()}
            </Menu>
          </div>
          <div className={styles.right}>
            <div className={styles.title}>{this.getRightTitle()}</div>
            <Switch>
              {getRoutes(match.path, routerData).map(item => (
                <Route
                  key={item.key}
                  path={item.path}
jim's avatar
jim committed
114
                  render={props => <item.component {...props} currentUser={currentUser} />}
jim's avatar
jim committed
115 116 117
                  exact={item.exact}
                />
              ))}
jim's avatar
jim committed
118
              <Redirect exact from="/account/settings" to="/account/settings/base" />
jim's avatar
jim committed
119 120 121
              <Redirect to="/exception/404" />
            </Switch>
          </div>
陈帅's avatar
陈帅 committed
122
        </div>
jim's avatar
jim committed
123
      </GridContent>
陈帅's avatar
陈帅 committed
124 125 126
    );
  }
}