2018-09-30 04:19:30 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { seafileAPI } from '../../utils/seafile-api';
|
2018-12-26 08:28:19 +00:00
|
|
|
import { gettext, siteRoot } from '../../utils/constants';
|
2019-03-25 02:24:49 +00:00
|
|
|
import NoticeItem from './notice-item';
|
2018-09-30 04:19:30 +00:00
|
|
|
|
|
|
|
class Notification extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
showNotice: false,
|
2018-12-25 10:25:16 +00:00
|
|
|
unseenCount: 0,
|
2019-03-25 02:24:49 +00:00
|
|
|
noticeList: [],
|
2018-10-16 10:19:51 +00:00
|
|
|
};
|
2018-09-30 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
2018-12-25 10:25:16 +00:00
|
|
|
componentDidMount() {
|
2018-12-25 12:29:52 +00:00
|
|
|
seafileAPI.getUnseenNotificationCount().then(res => {
|
2018-12-25 10:47:12 +00:00
|
|
|
this.setState({unseenCount: res.data.unseen_count});
|
|
|
|
});
|
2018-12-25 10:25:16 +00:00
|
|
|
}
|
|
|
|
|
2020-03-24 07:24:47 +00:00
|
|
|
onClick = (e) => {
|
|
|
|
e.preventDefault();
|
2018-09-30 04:19:30 +00:00
|
|
|
if (this.state.showNotice) {
|
2018-10-16 10:19:51 +00:00
|
|
|
seafileAPI.updateNotifications();
|
2018-12-25 10:47:12 +00:00
|
|
|
this.setState({
|
|
|
|
showNotice: false,
|
|
|
|
unseenCount: 0
|
2019-01-31 09:37:02 +00:00
|
|
|
});
|
2018-12-25 10:47:12 +00:00
|
|
|
} else {
|
|
|
|
this.loadNotices();
|
|
|
|
this.setState({showNotice: true});
|
2018-09-30 04:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
loadNotices = () => {
|
2019-03-25 02:24:49 +00:00
|
|
|
let page = 1;
|
|
|
|
let perPage = 5;
|
2020-03-24 07:24:47 +00:00
|
|
|
seafileAPI.listNotifications(page, perPage).then(res => {
|
2019-03-25 02:24:49 +00:00
|
|
|
let noticeList = res.data.notification_list;
|
|
|
|
this.setState({noticeList: noticeList});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-11-02 05:56:35 +00:00
|
|
|
onNoticeItemClick = (noticeItem) => {
|
2019-03-25 02:24:49 +00:00
|
|
|
let noticeList = this.state.noticeList.map(item => {
|
|
|
|
if (item.id === noticeItem.id) {
|
|
|
|
item.seen = true;
|
|
|
|
}
|
|
|
|
return item;
|
|
|
|
});
|
|
|
|
seafileAPI.markNoticeAsRead(noticeItem.id);
|
|
|
|
let unseenCount = this.state.unseenCount === 0 ? 0 : this.state.unseenCount - 1;
|
|
|
|
this.setState({
|
|
|
|
noticeList: noticeList,
|
2020-11-02 05:56:35 +00:00
|
|
|
unseenCount: unseenCount,
|
2018-10-16 10:19:51 +00:00
|
|
|
});
|
2020-11-02 05:56:35 +00:00
|
|
|
|
2018-09-30 04:19:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div id="notifications">
|
2020-03-24 07:24:47 +00:00
|
|
|
<a href="#" onClick={this.onClick} className="no-deco" id="notice-icon" title={gettext('Notifications')} aria-label={gettext('Notifications')}>
|
2018-09-30 04:19:30 +00:00
|
|
|
<span className="sf2-icon-bell"></span>
|
2018-12-25 10:25:16 +00:00
|
|
|
<span className={`num ${this.state.unseenCount ? '' : 'hide'}`}>{this.state.unseenCount}</span>
|
2018-09-30 04:19:30 +00:00
|
|
|
</a>
|
|
|
|
<div id="notice-popover" className={`sf-popover ${this.state.showNotice ? '': 'hide'}`}>
|
|
|
|
<div className="outer-caret up-outer-caret"><div className="inner-caret"></div></div>
|
2023-06-21 08:17:42 +00:00
|
|
|
<div className="sf-popover-hd h-7 d-flex align-items-center justify-content-center">
|
|
|
|
<h3 className="sf-popover-title title m-0">{gettext('Notifications')}</h3>
|
|
|
|
<a href="#" onClick={this.onClick} title={gettext('Close')} aria-label={gettext('Close')} className="sf-popover-close js-close sf2-icon-x1 action-icon m-0"></a>
|
2018-09-30 04:19:30 +00:00
|
|
|
</div>
|
|
|
|
<div className="sf-popover-con">
|
2023-06-21 08:17:42 +00:00
|
|
|
<ul className="notice-list list-unstyled">
|
2019-03-25 02:24:49 +00:00
|
|
|
{this.state.noticeList.map(item => {
|
|
|
|
return (<NoticeItem key={item.id} noticeItem={item} onNoticeItemClick={this.onNoticeItemClick}/>);
|
|
|
|
})}
|
|
|
|
</ul>
|
2023-06-20 08:47:20 +00:00
|
|
|
<a href={siteRoot + 'notice/list/'} className="view-all">{gettext('See All Notifications')}</a>
|
2018-09-30 04:19:30 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2018-10-16 10:19:51 +00:00
|
|
|
);
|
2018-09-30 04:19:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Notification;
|