Info.js 2.65 KB
Newer Older
jim's avatar
jim committed
1
import React, { Component } from 'react';
2 3 4
import { connect } from 'dva';
import { Route, routerRedux, Switch, Redirect } from 'dva/router';
import { Menu } from 'antd';
jim's avatar
jim committed
5 6
import Debounce from 'lodash-decorators/debounce';
import Bind from 'lodash-decorators/bind';
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
import styles from './Info.less';
import { getRoutes } from '../../utils/utils';

const { Item } = Menu;

const menuMap = {
  base: 'ๅŸบๆœฌ่ฎพ็ฝฎ',
  safe: 'ๅฎ‰ๅ…จ่ฎพ็ฝฎ',
  account: '่ดฆๅท็ป‘ๅฎš',
  message: 'ๆ–ฐๆถˆๆฏ้€š็Ÿฅ',
};

@connect(({ user }) => ({
  currentUser: user.currentUser,
}))
jim's avatar
jim committed
22
export default class Info extends Component {
23 24 25 26 27 28 29
  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
30
      meunMode: 'inline',
31 32
    };
  }
jim's avatar
jim committed
33 34 35 36 37 38 39
  componentDidMount() {
    this.resize();
    window.addEventListener('resize', this.resize);
  }
  componentWillUnmount() {
    window.removeEventListener('resize', this.resize);
  }
40
  getmenu = () => {
jim's avatar
jim committed
41 42 43
    return Object.keys(menuMap).map(item => (
      <Item key={item}>{menuMap[item]}</Item>
    ));
44 45 46 47 48 49 50 51 52 53
  };
  getRightTitle = () => {
    return menuMap[this.state.selectKey];
  };
  selectKey = ({ key }) => {
    this.props.dispatch(routerRedux.push(`/userinfo/${key}`));
    this.setState({
      selectKey: key,
    });
  };
jim's avatar
jim committed
54 55 56 57 58 59 60 61 62 63 64 65 66
  @Bind()
  @Debounce(200)
  resize() {
    if (window.innerWidth > 768 || window.innerWidth < 454) {
      this.setState({
        meunMode: 'inline',
      });
      return;
    }
    this.setState({
      meunMode: 'horizontal',
    });
  }
67 68 69 70 71 72 73 74 75
  render() {
    const { match, routerData, currentUser } = this.props;
    if (!currentUser.userid) {
      return '';
    }
    return (
      <div className={styles.main}>
        <div className={styles.leftmenu}>
          <Menu
jim's avatar
jim committed
76
            mode={this.state.meunMode}
77 78 79 80 81 82 83 84 85
            selectedKeys={[this.state.selectKey]}
            onClick={this.selectKey}
          >
            {this.getmenu()}
          </Menu>
        </div>
        <div className={styles.right}>
          <div className={styles.title}>{this.getRightTitle()}</div>
          <Switch>
jim's avatar
jim committed
86 87 88 89 90 91 92 93 94 95
            {getRoutes(match.path, routerData).map(item => (
              <Route
                key={item.key}
                path={item.path}
                render={props => (
                  <item.component {...props} currentUser={currentUser} />
                )}
                exact={item.exact}
              />
            ))}
96 97 98 99 100 101 102 103
            <Redirect exact from="/userinfo" to="/userinfo/base" />
            <Redirect to="/exception/404" />
          </Switch>
        </div>
      </div>
    );
  }
}