AvatarDropdown.tsx 2.74 KB
Newer Older
1 2 3 4
import { Avatar, Icon, Menu, Spin } from 'antd';
import { ClickParam } from 'antd/es/menu';
import { FormattedMessage } from 'umi-plugin-react/locale';
import React from 'react';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
5 6
import { connect } from 'dva';
import router from 'umi/router';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
7 8 9

import { ConnectProps, ConnectState } from '@/models/connect';
import { CurrentUser } from '@/models/user';
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23
import HeaderDropdown from '../HeaderDropdown';
import styles from './index.less';

export interface GlobalHeaderRightProps extends ConnectProps {
  currentUser?: CurrentUser;
  menu?: boolean;
}

class AvatarDropdown extends React.Component<GlobalHeaderRightProps> {
  onMenuClick = (event: ClickParam) => {
    const { key } = event;

    if (key === 'logout') {
      const { dispatch } = this.props;
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
24 25 26 27 28 29
      if (dispatch) {
        dispatch({
          type: 'login/logout',
        });
      }

ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
30 31 32 33
      return;
    }
    router.push(`/account/${key}`);
  };
34

duanledexianxianxian's avatar
duanledexianxianxian committed
35 36 37 38
  getUserName = (userName: string) => {
    return userName.length > 10 ? `${userName.substr(0, 10)}...` : userName;
  };

39
  render(): React.ReactNode {
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
40 41 42 43 44
    const { currentUser = {}, menu } = this.props;
    if (!menu) {
      return (
        <span className={`${styles.action} ${styles.account}`}>
          <Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
duanledexianxianxian's avatar
duanledexianxianxian committed
45
          <span className={styles.name}>{this.getUserName(currentUser.userName || '')}</span>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
        </span>
      );
    }
    const menuHeaderDropdown = (
      <Menu className={styles.menu} selectedKeys={[]} onClick={this.onMenuClick}>
        <Menu.Item key="center">
          <Icon type="user" />
          <FormattedMessage id="menu.account.center" defaultMessage="account center" />
        </Menu.Item>
        <Menu.Item key="settings">
          <Icon type="setting" />
          <FormattedMessage id="menu.account.settings" defaultMessage="account settings" />
        </Menu.Item>
        <Menu.Divider />
        <Menu.Item key="logout">
          <Icon type="logout" />
          <FormattedMessage id="menu.account.logout" defaultMessage="logout" />
        </Menu.Item>
      </Menu>
    );

duanledexianxianxian's avatar
duanledexianxianxian committed
67 68
    console.log(currentUser.avatar);
    return currentUser && currentUser.userName ? (
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
69 70 71
      <HeaderDropdown overlay={menuHeaderDropdown}>
        <span className={`${styles.action} ${styles.account}`}>
          <Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
duanledexianxianxian's avatar
duanledexianxianxian committed
72 73 74
          <span className={styles.name} title={currentUser.userName}>
            {this.getUserName(currentUser.userName || '')}
          </span>
ι™ˆεΈ…'s avatar
ι™ˆεΈ… committed
75 76 77 78 79 80 81 82 83 84
        </span>
      </HeaderDropdown>
    ) : (
      <Spin size="small" style={{ marginLeft: 8, marginRight: 8 }} />
    );
  }
}
export default connect(({ user }: ConnectState) => ({
  currentUser: user.currentUser,
}))(AvatarDropdown);