NoticeIconView.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import React, { Component } from 'react';
  2. import { Tag, message } from 'antd';
  3. import { connect } from 'dva';
  4. import groupBy from 'lodash/groupBy';
  5. import moment from 'moment';
  6. import { NoticeItem } from '@/models/global';
  7. import { CurrentUser } from '@/models/user';
  8. import { ConnectProps, ConnectState } from '@/models/connect';
  9. import NoticeIcon from '../NoticeIcon';
  10. import styles from './index.less';
  11. export interface GlobalHeaderRightProps extends ConnectProps {
  12. notices?: NoticeItem[];
  13. currentUser?: CurrentUser;
  14. fetchingNotices?: boolean;
  15. onNoticeVisibleChange?: (visible: boolean) => void;
  16. onNoticeClear?: (tabName?: string) => void;
  17. }
  18. class GlobalHeaderRight extends Component<GlobalHeaderRightProps> {
  19. componentDidMount() {
  20. const { dispatch } = this.props;
  21. if (dispatch) {
  22. dispatch({
  23. type: 'global/fetchNotices',
  24. });
  25. }
  26. }
  27. changeReadState = (clickedItem: NoticeItem): void => {
  28. const { id } = clickedItem;
  29. const { dispatch } = this.props;
  30. if (dispatch) {
  31. dispatch({
  32. type: 'global/changeNoticeReadState',
  33. payload: id,
  34. });
  35. }
  36. };
  37. handleNoticeClear = (title: string, key: string) => {
  38. const { dispatch } = this.props;
  39. message.success(`${'清空了'} ${title}`);
  40. if (dispatch) {
  41. dispatch({
  42. type: 'global/clearNotices',
  43. payload: key,
  44. });
  45. }
  46. };
  47. getNoticeData = (): {
  48. [key: string]: NoticeItem[];
  49. } => {
  50. const { notices = [] } = this.props;
  51. if (!notices || notices.length === 0) {
  52. return {};
  53. }
  54. const newNotices = notices.map(notice => {
  55. const newNotice = { ...notice };
  56. if (newNotice.datetime) {
  57. newNotice.datetime = moment(notice.datetime as string).fromNow();
  58. }
  59. if (newNotice.id) {
  60. newNotice.key = newNotice.id;
  61. }
  62. if (newNotice.extra && newNotice.status) {
  63. const color = {
  64. todo: '',
  65. processing: 'blue',
  66. urgent: 'red',
  67. doing: 'gold',
  68. }[newNotice.status];
  69. newNotice.extra = (
  70. <Tag
  71. color={color}
  72. style={{
  73. marginRight: 0,
  74. }}
  75. >
  76. {newNotice.extra}
  77. </Tag>
  78. );
  79. }
  80. return newNotice;
  81. });
  82. return groupBy(newNotices, 'type');
  83. };
  84. getUnreadData = (noticeData: { [key: string]: NoticeItem[] }) => {
  85. const unreadMsg: {
  86. [key: string]: number;
  87. } = {};
  88. Object.keys(noticeData).forEach(key => {
  89. const value = noticeData[key];
  90. if (!unreadMsg[key]) {
  91. unreadMsg[key] = 0;
  92. }
  93. if (Array.isArray(value)) {
  94. unreadMsg[key] = value.filter(item => !item.read).length;
  95. }
  96. });
  97. return unreadMsg;
  98. };
  99. render() {
  100. const { currentUser, fetchingNotices, onNoticeVisibleChange } = this.props;
  101. const noticeData = this.getNoticeData();
  102. const unreadMsg = this.getUnreadData(noticeData);
  103. return (
  104. <NoticeIcon
  105. className={styles.action}
  106. count={currentUser && currentUser.unreadCount}
  107. onItemClick={item => {
  108. this.changeReadState(item as NoticeItem);
  109. }}
  110. loading={fetchingNotices}
  111. clearText="清空"
  112. viewMoreText="查看更多"
  113. onClear={this.handleNoticeClear}
  114. onPopupVisibleChange={onNoticeVisibleChange}
  115. onViewMore={() => message.info('Click on view more')}
  116. clearClose
  117. >
  118. <NoticeIcon.Tab
  119. tabKey="notification"
  120. count={unreadMsg.notification}
  121. list={noticeData.notification}
  122. title="通知"
  123. emptyText="你已查看所有通知"
  124. showViewMore
  125. />
  126. <NoticeIcon.Tab
  127. tabKey="message"
  128. count={unreadMsg.message}
  129. list={noticeData.message}
  130. title="消息"
  131. emptyText="您已读完所有消息"
  132. showViewMore
  133. />
  134. <NoticeIcon.Tab
  135. tabKey="event"
  136. title="待办"
  137. emptyText="你已完成所有待办"
  138. count={unreadMsg.event}
  139. list={noticeData.event}
  140. showViewMore
  141. />
  142. </NoticeIcon>
  143. );
  144. }
  145. }
  146. export default connect(({ user, global, loading }: ConnectState) => ({
  147. currentUser: user.currentUser,
  148. collapsed: global.collapsed,
  149. fetchingMoreNotices: loading.effects['global/fetchMoreNotices'],
  150. fetchingNotices: loading.effects['global/fetchNotices'],
  151. notices: global.notices,
  152. }))(GlobalHeaderRight);