AvatarDropdown.jsx 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { LogoutOutlined, SettingOutlined, UserOutlined } from '@ant-design/icons'
  2. import { Avatar, Menu, Spin } from 'antd'
  3. import React from 'react'
  4. import { history, connect } from 'umi'
  5. import HeaderDropdown from '../HeaderDropdown'
  6. import styles from './index.less'
  7. class AvatarDropdown extends React.Component {
  8. onMenuClick = event => {
  9. const { key } = event
  10. if (key === 'logout') {
  11. const { dispatch } = this.props
  12. if (dispatch) {
  13. dispatch({
  14. type: 'login/logout'
  15. })
  16. }
  17. return
  18. }
  19. history.push(`/account/${key}`)
  20. }
  21. render() {
  22. const {
  23. currentUser = {
  24. avatar: '',
  25. name: ''
  26. },
  27. menu
  28. } = this.props
  29. const menuHeaderDropdown = (
  30. <Menu className={styles.menu} selectedKeys={[]} onClick={this.onMenuClick}>
  31. {menu && (
  32. <Menu.Item key="center">
  33. <UserOutlined />
  34. 个人中心
  35. </Menu.Item>
  36. )}
  37. {menu && (
  38. <Menu.Item key="settings">
  39. <SettingOutlined />
  40. 个人设置
  41. </Menu.Item>
  42. )}
  43. {menu && <Menu.Divider />}
  44. <Menu.Item key="logout">
  45. <LogoutOutlined />
  46. 退出登录
  47. </Menu.Item>
  48. </Menu>
  49. )
  50. return currentUser && currentUser.name ? (
  51. <HeaderDropdown overlay={menuHeaderDropdown}>
  52. <span className={`${styles.action} ${styles.account}`}>
  53. <Avatar size="small" className={styles.avatar} src={currentUser.avatar} alt="avatar" />
  54. <span className={`${styles.name} anticon`}>{currentUser.name}</span>
  55. </span>
  56. </HeaderDropdown>
  57. ) : (
  58. <span className={`${styles.action} ${styles.account}`}>
  59. <Spin
  60. size="small"
  61. style={{
  62. marginLeft: 8,
  63. marginRight: 8
  64. }}
  65. />
  66. </span>
  67. )
  68. }
  69. }
  70. export default connect(({ user }) => ({
  71. currentUser: user.currentUser
  72. }))(AvatarDropdown)