index.js 5.19 KB
Newer Older
偏右's avatar
偏右 committed
1
import React, { PureComponent } from 'react';
afc163's avatar
afc163 committed
2
import { Menu, Icon, Spin, Tag, Dropdown, Avatar, Divider, Tooltip } from 'antd';
偏右's avatar
偏右 committed
3 4 5
import moment from 'moment';
import groupBy from 'lodash/groupBy';
import Debounce from 'lodash-decorators/debounce';
jiang's avatar
jiang committed
6
import { Link } from 'dva/router';
ddcat1115's avatar
ddcat1115 committed
7 8
import NoticeIcon from '../NoticeIcon';
import HeaderSearch from '../HeaderSearch';
偏右's avatar
偏右 committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
import styles from './index.less';

export default class GlobalHeader extends PureComponent {
  componentWillUnmount() {
    this.triggerResizeEvent.cancel();
  }
  getNoticeData() {
    const { notices = [] } = this.props;
    if (notices.length === 0) {
      return {};
    }
    const newNotices = notices.map((notice) => {
      const newNotice = { ...notice };
      if (newNotice.datetime) {
        newNotice.datetime = moment(notice.datetime).fromNow();
      }
      // transform id to item key
      if (newNotice.id) {
        newNotice.key = newNotice.id;
      }
      if (newNotice.extra && newNotice.status) {
        const color = ({
          todo: '',
          processing: 'blue',
          urgent: 'red',
          doing: 'gold',
        })[newNotice.status];
        newNotice.extra = <Tag color={color} style={{ marginRight: 0 }}>{newNotice.extra}</Tag>;
      }
      return newNotice;
    });
    return groupBy(newNotices, 'type');
  }
  toggle = () => {
43 44
    const { collapsed, onCollapse } = this.props;
    onCollapse(!collapsed);
偏右's avatar
偏右 committed
45 46 47 48 49 50 51 52 53 54
    this.triggerResizeEvent();
  }
  @Debounce(600)
  triggerResizeEvent() { // eslint-disable-line
    const event = document.createEvent('HTMLEvents');
    event.initEvent('resize', true, false);
    window.dispatchEvent(event);
  }
  render() {
    const {
55 56
      currentUser, collapsed, fetchingNotices, isMobile, logo,
      onNoticeVisibleChange, onMenuClick, onNoticeClear,
偏右's avatar
偏右 committed
57 58
    } = this.props;
    const menu = (
59
      <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
偏右's avatar
偏右 committed
60 61
        <Menu.Item disabled><Icon type="user" />个人中心</Menu.Item>
        <Menu.Item disabled><Icon type="setting" />设置</Menu.Item>
62
        <Menu.Item key="triggerError"><Icon type="close-circle" />触发报错</Menu.Item>
偏右's avatar
偏右 committed
63 64 65 66 67 68
        <Menu.Divider />
        <Menu.Item key="logout"><Icon type="logout" />退出登录</Menu.Item>
      </Menu>
    );
    const noticeData = this.getNoticeData();
    return (
69
      <div className={styles.header}>
jiang's avatar
jiang committed
70
        {isMobile && (
71 72 73 74 75 76
          [
            (
              <Link to="/" className={styles.logo} key="logo">
                <img src={logo} alt="logo" width="32" />
              </Link>
            ),
jiang's avatar
jiang committed
77 78 79
            <Divider type="vertical" key="line" />,
          ]
        )}
偏右's avatar
偏右 committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        <Icon
          className={styles.trigger}
          type={collapsed ? 'menu-unfold' : 'menu-fold'}
          onClick={this.toggle}
        />
        <div className={styles.right}>
          <HeaderSearch
            className={`${styles.action} ${styles.search}`}
            placeholder="站内搜索"
            dataSource={['搜索提示一', '搜索提示二', '搜索提示三']}
            onSearch={(value) => {
              console.log('input', value); // eslint-disable-line
            }}
            onPressEnter={(value) => {
              console.log('enter', value); // eslint-disable-line
            }}
          />
afc163's avatar
afc163 committed
97
          <Tooltip title="使用文档">
98
            <a
afc163's avatar
afc163 committed
99
              target="_blank"
100 101
              href="http://pro.ant.design/docs/getting-started"
              rel="noopener noreferrer"
afc163's avatar
afc163 committed
102 103 104
              className={styles.action}
            >
              <Icon type="question-circle-o" />
105
            </a >
afc163's avatar
afc163 committed
106
          </Tooltip>
偏右's avatar
偏右 committed
107 108 109 110 111 112
          <NoticeIcon
            className={styles.action}
            count={currentUser.notifyCount}
            onItemClick={(item, tabProps) => {
              console.log(item, tabProps); // eslint-disable-line
            }}
113 114
            onClear={onNoticeClear}
            onPopupVisibleChange={onNoticeVisibleChange}
偏右's avatar
偏右 committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
            loading={fetchingNotices}
            popupAlign={{ offset: [20, -16] }}
          >
            <NoticeIcon.Tab
              list={noticeData['通知']}
              title="通知"
              emptyText="你已查看所有通知"
              emptyImage="https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg"
            />
            <NoticeIcon.Tab
              list={noticeData['消息']}
              title="消息"
              emptyText="您已读完所有消息"
              emptyImage="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg"
            />
            <NoticeIcon.Tab
              list={noticeData['待办']}
              title="待办"
              emptyText="你已完成所有待办"
              emptyImage="https://gw.alipayobjects.com/zos/rmsportal/HsIsxMZiWKrNUavQUXqx.svg"
            />
          </NoticeIcon>
          {currentUser.name ? (
            <Dropdown overlay={menu}>
              <span className={`${styles.action} ${styles.account}`}>
                <Avatar size="small" className={styles.avatar} src={currentUser.avatar} />
jiang's avatar
jiang committed
141
                <span className={styles.name}>{currentUser.name}</span>
偏右's avatar
偏右 committed
142 143 144 145
              </span>
            </Dropdown>
          ) : <Spin size="small" style={{ marginLeft: 8 }} />}
        </div>
146
      </div>
偏右's avatar
偏右 committed
147 148 149
    );
  }
}