index.js 5.38 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
import styles from './index.less';

export default class GlobalHeader extends PureComponent {
  componentWillUnmount() {
    this.triggerResizeEvent.cancel();
  }
陈帅's avatar
陈帅 committed
15

偏右's avatar
偏右 committed
16
  getNoticeData() {
陈帅's avatar
陈帅 committed
17
    const { notices } = this.props;
Amumu's avatar
Amumu committed
18
    if (notices == null || notices.length === 0) {
偏右's avatar
偏右 committed
19 20
      return {};
    }
jim's avatar
jim committed
21
    const newNotices = notices.map(notice => {
偏右's avatar
偏右 committed
22 23 24 25 26 27 28 29 30
      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) {
jim's avatar
jim committed
31
        const color = {
偏右's avatar
偏右 committed
32 33 34 35
          todo: '',
          processing: 'blue',
          urgent: 'red',
          doing: 'gold',
jim's avatar
jim committed
36 37 38 39 40 41
        }[newNotice.status];
        newNotice.extra = (
          <Tag color={color} style={{ marginRight: 0 }}>
            {newNotice.extra}
          </Tag>
        );
偏右's avatar
偏右 committed
42 43 44 45 46
      }
      return newNotice;
    });
    return groupBy(newNotices, 'type');
  }
陈帅's avatar
陈帅 committed
47

偏右's avatar
偏右 committed
48
  toggle = () => {
49 50
    const { collapsed, onCollapse } = this.props;
    onCollapse(!collapsed);
偏右's avatar
偏右 committed
51
    this.triggerResizeEvent();
jim's avatar
jim committed
52
  };
53
  /* eslint-disable*/
偏右's avatar
偏右 committed
54
  @Debounce(600)
55
  triggerResizeEvent() {
偏右's avatar
偏右 committed
56 57 58
    const event = document.createEvent('HTMLEvents');
    event.initEvent('resize', true, false);
    window.dispatchEvent(event);
59
  }
偏右's avatar
偏右 committed
60 61
  render() {
    const {
afc163's avatar
afc163 committed
62
      currentUser = {},
jim's avatar
jim committed
63 64 65 66 67 68 69
      collapsed,
      fetchingNotices,
      isMobile,
      logo,
      onNoticeVisibleChange,
      onMenuClick,
      onNoticeClear,
偏右's avatar
偏右 committed
70
    } = this.props;
Amumu's avatar
Amumu committed
71
    if (currentUser == null) {
陈帅's avatar
陈帅 committed
72
      currentUser = {};
Amumu's avatar
Amumu committed
73
    }
偏右's avatar
偏右 committed
74
    const menu = (
75
      <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
jim's avatar
jim committed
76 77 78 79 80 81 82 83 84
        <Menu.Item disabled>
          <Icon type="user" />个人中心
        </Menu.Item>
        <Menu.Item disabled>
          <Icon type="setting" />设置
        </Menu.Item>
        <Menu.Item key="triggerError">
          <Icon type="close-circle" />触发报错
        </Menu.Item>
偏右's avatar
偏右 committed
85
        <Menu.Divider />
jim's avatar
jim committed
86 87 88
        <Menu.Item key="logout">
          <Icon type="logout" />退出登录
        </Menu.Item>
偏右's avatar
偏右 committed
89 90 91 92
      </Menu>
    );
    const noticeData = this.getNoticeData();
    return (
93
      <div className={styles.header}>
jim's avatar
jim committed
94 95 96 97 98 99
        {isMobile && [
          <Link to="/" className={styles.logo} key="logo">
            <img src={logo} alt="logo" width="32" />
          </Link>,
          <Divider type="vertical" key="line" />,
        ]}
偏右's avatar
偏右 committed
100 101 102 103 104 105 106 107 108 109
        <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={['搜索提示一', '搜索提示二', '搜索提示三']}
jim's avatar
jim committed
110
            onSearch={value => {
偏右's avatar
偏右 committed
111 112
              console.log('input', value); // eslint-disable-line
            }}
jim's avatar
jim committed
113
            onPressEnter={value => {
偏右's avatar
偏右 committed
114 115 116
              console.log('enter', value); // eslint-disable-line
            }}
          />
afc163's avatar
afc163 committed
117
          <Tooltip title="使用文档">
118
            <a
afc163's avatar
afc163 committed
119
              target="_blank"
120 121
              href="http://pro.ant.design/docs/getting-started"
              rel="noopener noreferrer"
afc163's avatar
afc163 committed
122 123 124
              className={styles.action}
            >
              <Icon type="question-circle-o" />
jim's avatar
jim committed
125
            </a>
afc163's avatar
afc163 committed
126
          </Tooltip>
偏右's avatar
偏右 committed
127 128 129 130 131 132
          <NoticeIcon
            className={styles.action}
            count={currentUser.notifyCount}
            onItemClick={(item, tabProps) => {
              console.log(item, tabProps); // eslint-disable-line
            }}
133 134
            onClear={onNoticeClear}
            onPopupVisibleChange={onNoticeVisibleChange}
偏右's avatar
偏右 committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
            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
161
                <span className={styles.name}>{currentUser.name}</span>
偏右's avatar
偏右 committed
162 163
              </span>
            </Dropdown>
jim's avatar
jim committed
164 165 166
          ) : (
            <Spin size="small" style={{ marginLeft: 8 }} />
          )}
偏右's avatar
偏右 committed
167
        </div>
168
      </div>
偏右's avatar
偏右 committed
169 170 171
    );
  }
}