RightContent.js 6.67 KB
Newer Older
jim's avatar
jim committed
1
import React, { PureComponent } from 'react';
2
import { FormattedMessage, formatMessage } from 'umi/locale';
3
import { Spin, Tag, Menu, Icon, Avatar, Tooltip, message } from 'antd';
jim's avatar
jim committed
4 5 6 7
import moment from 'moment';
import groupBy from 'lodash/groupBy';
import NoticeIcon from '../NoticeIcon';
import HeaderSearch from '../HeaderSearch';
8
import HeaderDropdown from '../HeaderDropdown';
9
import SelectLang from '../SelectLang';
jim's avatar
jim committed
10 11 12 13 14 15 16 17
import styles from './index.less';

export default class GlobalHeaderRight extends PureComponent {
  getNoticeData() {
    const { notices = [] } = this.props;
    if (notices.length === 0) {
      return {};
    }
jim's avatar
jim committed
18
    const newNotices = notices.map(notice => {
jim's avatar
jim committed
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
      const newNotice = { ...notice };
      if (newNotice.datetime) {
        newNotice.datetime = moment(notice.datetime).fromNow();
      }
      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');
  }
陈帅's avatar
陈帅 committed
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  getUnreadData = noticeData => {
    const unreadMsg = {};
    Object.entries(noticeData).forEach(([key, value]) => {
      if (!unreadMsg[key]) {
        unreadMsg[key] = 0;
      }
      if (Array.isArray(value)) {
        unreadMsg[key] = value.filter(item => !item.read).length;
      }
    });
    return unreadMsg;
  };

  changeReadState = clickedItem => {
    const { id } = clickedItem;
    const { dispatch } = this.props;
    dispatch({
      type: 'global/changeNoticeReadState',
      payload: id,
    });
  };

jim's avatar
jim committed
66 67 68 69 70 71 72
  render() {
    const {
      currentUser,
      fetchingNotices,
      onNoticeVisibleChange,
      onMenuClick,
      onNoticeClear,
陈帅's avatar
陈帅 committed
73
      theme,
jim's avatar
jim committed
74 75 76 77
    } = this.props;
    const menu = (
      <Menu className={styles.menu} selectedKeys={[]} onClick={onMenuClick}>
        <Menu.Item key="userCenter">
陈帅's avatar
陈帅 committed
78 79
          <Icon type="user" />
          <FormattedMessage id="menu.account.center" defaultMessage="account center" />
jim's avatar
jim committed
80 81
        </Menu.Item>
        <Menu.Item key="userinfo">
陈帅's avatar
陈帅 committed
82 83
          <Icon type="setting" />
          <FormattedMessage id="menu.account.settings" defaultMessage="account settings" />
jim's avatar
jim committed
84 85
        </Menu.Item>
        <Menu.Item key="triggerError">
陈帅's avatar
陈帅 committed
86 87
          <Icon type="close-circle" />
          <FormattedMessage id="menu.account.trigger" defaultMessage="Trigger Error" />
jim's avatar
jim committed
88 89 90
        </Menu.Item>
        <Menu.Divider />
        <Menu.Item key="logout">
陈帅's avatar
陈帅 committed
91
          <Icon type="logout" />
陈帅's avatar
陈帅 committed
92
          <FormattedMessage id="menu.account.logout" defaultMessage="logout" />
jim's avatar
jim committed
93 94 95 96
        </Menu.Item>
      </Menu>
    );
    const noticeData = this.getNoticeData();
97
    const unreadMsg = this.getUnreadData(noticeData);
jim's avatar
jim committed
98
    let className = styles.right;
陈帅's avatar
陈帅 committed
99
    if (theme === 'dark') {
jim's avatar
jim committed
100
      className = `${styles.right}  ${styles.dark}`;
jim's avatar
jim committed
101 102
    }
    return (
jim's avatar
jim committed
103
      <div className={className}>
jim's avatar
jim committed
104 105
        <HeaderSearch
          className={`${styles.action} ${styles.search}`}
106
          placeholder={formatMessage({ id: 'component.globalHeader.search' })}
107 108 109 110 111
          dataSource={[
            formatMessage({ id: 'component.globalHeader.search.example1' }),
            formatMessage({ id: 'component.globalHeader.search.example2' }),
            formatMessage({ id: 'component.globalHeader.search.example3' }),
          ]}
jim's avatar
jim committed
112
          onSearch={value => {
jim's avatar
jim committed
113 114
            console.log('input', value); // eslint-disable-line
          }}
jim's avatar
jim committed
115
          onPressEnter={value => {
jim's avatar
jim committed
116 117 118
            console.log('enter', value); // eslint-disable-line
          }}
        />
119
        <Tooltip title={formatMessage({ id: 'component.globalHeader.help' })}>
jim's avatar
jim committed
120 121
          <a
            target="_blank"
陈帅's avatar
陈帅 committed
122
            href="https://pro.ant.design/docs/getting-started"
jim's avatar
jim committed
123 124 125 126 127 128 129 130
            rel="noopener noreferrer"
            className={styles.action}
          >
            <Icon type="question-circle-o" />
          </a>
        </Tooltip>
        <NoticeIcon
          className={styles.action}
131
          count={currentUser.unreadCount}
jim's avatar
jim committed
132 133
          onItemClick={(item, tabProps) => {
            console.log(item, tabProps); // eslint-disable-line
134
            this.changeReadState(item, tabProps);
jim's avatar
jim committed
135
          }}
136
          loading={fetchingNotices}
陈帅's avatar
陈帅 committed
137 138 139
          locale={{
            emptyText: formatMessage({ id: 'component.noticeIcon.empty' }),
            clear: formatMessage({ id: 'component.noticeIcon.clear' }),
140 141 142 143
            viewMore: formatMessage({ id: 'component.noticeIcon.view-more' }),
            notification: formatMessage({ id: 'component.globalHeader.notification' }),
            message: formatMessage({ id: 'component.globalHeader.message' }),
            event: formatMessage({ id: 'component.globalHeader.event' }),
陈帅's avatar
陈帅 committed
144
          }}
jim's avatar
jim committed
145 146
          onClear={onNoticeClear}
          onPopupVisibleChange={onNoticeVisibleChange}
147
          onViewMore={() => message.info('Click on view more')}
wingsico's avatar
wingsico committed
148
          clearClose
jim's avatar
jim committed
149 150
        >
          <NoticeIcon.Tab
151
            count={unreadMsg.notification}
152
            list={noticeData.notification}
153
            title="notification"
154
            emptyText={formatMessage({ id: 'component.globalHeader.notification.empty' })}
jim's avatar
jim committed
155
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg"
156
            showViewMore
jim's avatar
jim committed
157 158
          />
          <NoticeIcon.Tab
159
            count={unreadMsg.message}
160
            list={noticeData.message}
161
            title="message"
162
            emptyText={formatMessage({ id: 'component.globalHeader.message.empty' })}
jim's avatar
jim committed
163
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg"
164
            showViewMore
jim's avatar
jim committed
165 166
          />
          <NoticeIcon.Tab
167
            count={unreadMsg.event}
168
            list={noticeData.event}
169
            title="event"
170
            emptyText={formatMessage({ id: 'component.globalHeader.event.empty' })}
jim's avatar
jim committed
171
            emptyImage="https://gw.alipayobjects.com/zos/rmsportal/HsIsxMZiWKrNUavQUXqx.svg"
172
            showViewMore
jim's avatar
jim committed
173 174 175
          />
        </NoticeIcon>
        {currentUser.name ? (
176
          <HeaderDropdown overlay={menu}>
jim's avatar
jim committed
177
            <span className={`${styles.action} ${styles.account}`}>
jim's avatar
jim committed
178 179 180 181 182 183
              <Avatar
                size="small"
                className={styles.avatar}
                src={currentUser.avatar}
                alt="avatar"
              />
jim's avatar
jim committed
184 185
              <span className={styles.name}>{currentUser.name}</span>
            </span>
186
          </HeaderDropdown>
jim's avatar
jim committed
187
        ) : (
陈帅's avatar
陈帅 committed
188
          <Spin size="small" style={{ marginLeft: 8, marginRight: 8 }} />
jim's avatar
jim committed
189
        )}
190
        <SelectLang className={styles.action} />
jim's avatar
jim committed
191 192 193 194
      </div>
    );
  }
}