Commit 3fffc83d authored by Anyuxuan's avatar Anyuxuan Committed by 陈帅

feat: add count prop for NoticeIconTab component (#2862)

* feat: add count prop for NoticeIconTab component.(ant-design-pro#2216)
prop count is not required.

* feat: unread notice message demo
parent 3aa50319
...@@ -36,6 +36,7 @@ export default { ...@@ -36,6 +36,7 @@ export default {
}, },
], ],
notifyCount: 12, notifyCount: 12,
unreadCount: 11,
country: 'China', country: 'China',
geographic: { geographic: {
province: { province: {
......
...@@ -40,6 +40,28 @@ export default class GlobalHeaderRight extends PureComponent { ...@@ -40,6 +40,28 @@ export default class GlobalHeaderRight extends PureComponent {
return groupBy(newNotices, 'type'); return groupBy(newNotices, 'type');
} }
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,
});
};
render() { render() {
const { const {
currentUser, currentUser,
...@@ -71,6 +93,7 @@ export default class GlobalHeaderRight extends PureComponent { ...@@ -71,6 +93,7 @@ export default class GlobalHeaderRight extends PureComponent {
</Menu> </Menu>
); );
const noticeData = this.getNoticeData(); const noticeData = this.getNoticeData();
const unreadMsg = this.getUnreadData(noticeData);
let className = styles.right; let className = styles.right;
if (theme === 'dark') { if (theme === 'dark') {
className = `${styles.right} ${styles.dark}`; className = `${styles.right} ${styles.dark}`;
...@@ -104,9 +127,10 @@ export default class GlobalHeaderRight extends PureComponent { ...@@ -104,9 +127,10 @@ export default class GlobalHeaderRight extends PureComponent {
</Tooltip> </Tooltip>
<NoticeIcon <NoticeIcon
className={styles.action} className={styles.action}
count={currentUser.notifyCount} count={currentUser.unreadCount}
onItemClick={(item, tabProps) => { onItemClick={(item, tabProps) => {
console.log(item, tabProps); // eslint-disable-line console.log(item, tabProps); // eslint-disable-line
this.changeReadState(item, tabProps);
}} }}
locale={{ locale={{
emptyText: formatMessage({ id: 'component.noticeIcon.empty' }), emptyText: formatMessage({ id: 'component.noticeIcon.empty' }),
...@@ -119,6 +143,7 @@ export default class GlobalHeaderRight extends PureComponent { ...@@ -119,6 +143,7 @@ export default class GlobalHeaderRight extends PureComponent {
clearClose clearClose
> >
<NoticeIcon.Tab <NoticeIcon.Tab
count={unreadMsg.notification}
list={noticeData.notification} list={noticeData.notification}
title={formatMessage({ id: 'component.globalHeader.notification' })} title={formatMessage({ id: 'component.globalHeader.notification' })}
name="notification" name="notification"
...@@ -126,6 +151,7 @@ export default class GlobalHeaderRight extends PureComponent { ...@@ -126,6 +151,7 @@ export default class GlobalHeaderRight extends PureComponent {
emptyImage="https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg" emptyImage="https://gw.alipayobjects.com/zos/rmsportal/wAhyIChODzsoKIOBHcBk.svg"
/> />
<NoticeIcon.Tab <NoticeIcon.Tab
count={unreadMsg.message}
list={noticeData.message} list={noticeData.message}
title={formatMessage({ id: 'component.globalHeader.message' })} title={formatMessage({ id: 'component.globalHeader.message' })}
name="message" name="message"
...@@ -133,6 +159,7 @@ export default class GlobalHeaderRight extends PureComponent { ...@@ -133,6 +159,7 @@ export default class GlobalHeaderRight extends PureComponent {
emptyImage="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg" emptyImage="https://gw.alipayobjects.com/zos/rmsportal/sAuJeJzSKbUmHfBQRzmZ.svg"
/> />
<NoticeIcon.Tab <NoticeIcon.Tab
count={unreadMsg.event}
list={noticeData.event} list={noticeData.event}
title={formatMessage({ id: 'component.globalHeader.event' })} title={formatMessage({ id: 'component.globalHeader.event' })}
name="event" name="event"
......
...@@ -10,6 +10,7 @@ export interface INoticeIconData { ...@@ -10,6 +10,7 @@ export interface INoticeIconData {
export interface INoticeIconTabProps { export interface INoticeIconTabProps {
list?: INoticeIconData[]; list?: INoticeIconData[];
count?: number;
title?: string; title?: string;
name?: string; name?: string;
emptyText?: React.ReactNode; emptyText?: React.ReactNode;
......
...@@ -33,13 +33,13 @@ export default class NoticeIcon extends PureComponent { ...@@ -33,13 +33,13 @@ export default class NoticeIcon extends PureComponent {
} }
}; };
onClear = (name) => { onClear = name => {
const { onClear, clearClose } = this.props; const { onClear, clearClose } = this.props;
onClear(name) onClear(name);
if (clearClose) { if (clearClose) {
this.popover.click(); this.popover.click();
} }
} };
onTabChange = tabType => { onTabChange = tabType => {
const { onTabChange } = this.props; const { onTabChange } = this.props;
...@@ -52,18 +52,18 @@ export default class NoticeIcon extends PureComponent { ...@@ -52,18 +52,18 @@ export default class NoticeIcon extends PureComponent {
return null; return null;
} }
const panes = React.Children.map(children, child => { const panes = React.Children.map(children, child => {
const title = const { list, title, name, count } = child.props;
child.props.list && child.props.list.length > 0 const len = list && list.length ? list.length : 0;
? `${child.props.title} (${child.props.list.length})` const msgCount = count || count === 0 ? count : len;
: child.props.title; const tabTitle = msgCount > 0 ? `${title} (${msgCount})` : title;
return ( return (
<TabPane tab={title} key={child.props.name}> <TabPane tab={tabTitle} key={name}>
<List <List
{...child.props} {...child.props}
data={child.props.list} data={list}
onClick={item => this.onItemClick(item, child.props)} onClick={item => this.onItemClick(item, child.props)}
onClear={() => this.onClear(child.props.name)} onClear={() => this.onClear(name)}
title={child.props.title} title={title}
locale={locale} locale={locale}
/> />
</TabPane> </TabPane>
...@@ -107,7 +107,7 @@ export default class NoticeIcon extends PureComponent { ...@@ -107,7 +107,7 @@ export default class NoticeIcon extends PureComponent {
popupAlign={popupAlign} popupAlign={popupAlign}
onVisibleChange={onPopupVisibleChange} onVisibleChange={onPopupVisibleChange}
{...popoverProps} {...popoverProps}
ref={node => { this.popover = ReactDOM.findDOMNode(node)}} // eslint-disable-line ref={node => (this.popover = ReactDOM.findDOMNode(node))} // eslint-disable-line
> >
{trigger} {trigger}
</Popover> </Popover>
......
...@@ -9,15 +9,21 @@ export default { ...@@ -9,15 +9,21 @@ export default {
}, },
effects: { effects: {
*fetchNotices(_, { call, put }) { *fetchNotices(_, { call, put, select }) {
const data = yield call(queryNotices); const data = yield call(queryNotices);
yield put({ yield put({
type: 'saveNotices', type: 'saveNotices',
payload: data, payload: data,
}); });
const unreadCount = yield select(
state => state.global.notices.filter(item => !item.read).length
);
yield put({ yield put({
type: 'user/changeNotifyCount', type: 'user/changeNotifyCount',
payload: data.length, payload: {
totalCount: data.length,
unreadCount,
},
}); });
}, },
*clearNotices({ payload }, { put, select }) { *clearNotices({ payload }, { put, select }) {
...@@ -26,9 +32,37 @@ export default { ...@@ -26,9 +32,37 @@ export default {
payload, payload,
}); });
const count = yield select(state => state.global.notices.length); const count = yield select(state => state.global.notices.length);
const unreadCount = yield select(
state => state.global.notices.filter(item => !item.read).length
);
yield put({ yield put({
type: 'user/changeNotifyCount', type: 'user/changeNotifyCount',
payload: count, payload: {
totalCount: count,
unreadCount,
},
});
},
*changeNoticeReadState({ payload }, { put, select }) {
const notices = yield select(state =>
state.global.notices.map(item => {
const notice = { ...item };
if (notice.id === payload) {
notice.read = true;
}
return notice;
})
);
yield put({
type: 'saveNotices',
payload: notices,
});
yield put({
type: 'user/changeNotifyCount',
payload: {
totalCount: notices.length,
unreadCount: notices.filter(item => !item.read).length,
},
}); });
}, },
}, },
......
...@@ -43,7 +43,8 @@ export default { ...@@ -43,7 +43,8 @@ export default {
...state, ...state,
currentUser: { currentUser: {
...state.currentUser, ...state.currentUser,
notifyCount: action.payload, notifyCount: action.payload.totalCount,
unreadCount: action.payload.unreadCount,
}, },
}; };
}, },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment