1
0
mirror of https://github.com/haiwen/seahub.git synced 2025-09-29 08:37:55 +00:00
Files
seahub/frontend/src/components/common/notification.js

231 lines
8.0 KiB
JavaScript
Raw Normal View History

import React from 'react';
import NotificationPopover from './notification-popover';
import { seafileAPI } from '../../utils/seafile-api';
import { gettext } from '../../utils/constants';
import NoticeItem from './notice-item';
import UserNotificationsDialog from '../../user-notifications';
import { Utils } from '../../utils/utils';
import IconBtn from '../icon-btn';
import '../../css/notification.css';
class Notification extends React.Component {
constructor(props) {
super(props);
this.state = {
showNotice: false,
totalUnseenCount: 0,
2024-11-22 11:18:42 +08:00
generalNoticeList: [],
discussionNoticeList: [],
2024-11-21 10:59:13 +08:00
currentTab: 'general',
isShowNotificationDialog: this.getInitDialogState(),
2018-10-16 18:19:51 +08:00
};
}
2018-12-25 18:25:16 +08:00
componentDidMount() {
2024-11-22 11:18:42 +08:00
seafileAPI.listAllNotifications().then(res => {
this.setState({
totalUnseenCount: res.data.total_unseen_count,
generalNoticeListUnseen: res.data.general.unseen_count,
discussionNoticeListUnseen: res.data.discussion.unseen_count
});
2018-12-25 18:47:12 +08:00
});
2018-12-25 18:25:16 +08:00
}
onClick = (e) => {
e.preventDefault();
if (this.state.showNotice) {
2018-10-16 18:19:51 +08:00
seafileAPI.updateNotifications();
2018-12-25 18:47:12 +08:00
this.setState({
showNotice: false,
totalUnseenCount: 0
});
2018-12-25 18:47:12 +08:00
} else {
this.loadNotices();
2024-07-18 11:58:42 +08:00
this.setState({ showNotice: true });
}
};
2024-11-21 17:11:02 +08:00
2024-11-21 10:59:13 +08:00
tabItemClick = (tab) => {
const { currentTab } = this.state;
if (currentTab === tab) return;
2024-11-21 17:11:02 +08:00
this.setState({
2024-11-21 10:59:13 +08:00
showNotice: true,
currentTab: tab
2024-11-21 17:11:02 +08:00
});
2024-11-21 10:59:13 +08:00
};
loadNotices = () => {
let page = 1;
let perPage = 25;
2024-11-22 11:18:42 +08:00
seafileAPI.listAllNotifications(page, perPage).then(res => {
2024-11-27 15:06:30 +08:00
let generalNoticeList = res.data.general.notification_list;
let discussionNoticeList = res.data.discussion.notification_list;
let generalNoticeListUnseen = res.data.general.unseen_count;
let discussionNoticeListUnseen = res.data.discussion.unseen_count;
2024-11-22 11:18:42 +08:00
this.setState({
generalNoticeList: generalNoticeList,
discussionNoticeList: discussionNoticeList,
generalNoticeListUnseen: generalNoticeListUnseen,
discussionNoticeListUnseen: discussionNoticeListUnseen
2024-11-21 10:59:13 +08:00
});
2024-11-22 11:18:42 +08:00
});
};
onNoticeItemClick = (noticeItem) => {
2024-11-21 17:11:02 +08:00
if (this.state.currentTab === 'general') {
2024-11-22 11:18:42 +08:00
let noticeList = this.state.generalNoticeList.map(item => {
if (item.id === noticeItem.id) {
item.seen = true;
}
return item;
});
let totalUnseenCount = this.state.totalUnseenCount === 0 ? 0 : this.state.totalUnseenCount - 1;
let generalNoticeListUnseen = this.state.generalNoticeListUnseen === 0 ? 0 : this.state.generalNoticeListUnseen - 1;
2024-11-22 11:18:42 +08:00
this.setState({
generalNoticeList: noticeList,
totalUnseenCount: totalUnseenCount,
generalNoticeListUnseen: generalNoticeListUnseen
2024-11-22 11:18:42 +08:00
});
2024-11-21 17:11:02 +08:00
seafileAPI.markNoticeAsRead(noticeItem.id);
}
if (this.state.currentTab === 'discussion') {
2024-11-22 11:18:42 +08:00
let noticeList = this.state.discussionNoticeList.map(item => {
if (item.id === noticeItem.id) {
item.seen = true;
}
return item;
});
let totalUnseenCount = this.state.totalUnseenCount === 0 ? 0 : this.state.totalUnseenCount - 1;
let discussionNoticeListUnseen = this.state.discussionNoticeListUnseen === 0 ? 0 : this.state.discussionNoticeListUnseen - 1;
2024-11-22 11:18:42 +08:00
this.setState({
discussionNoticeList: noticeList,
totalUnseenCount: totalUnseenCount,
discussionNoticeListUnseen: discussionNoticeListUnseen
2024-11-22 11:18:42 +08:00
});
2024-11-21 17:11:02 +08:00
seafileAPI.markSdocNoticeAsRead(noticeItem.id);
}
};
getInitDialogState = () => {
const searchParams = Utils.getUrlSearches();
return searchParams.notifications === 'all';
};
onNotificationDialogToggle = () => {
let newSearch = this.state.isShowNotificationDialog ? null : 'all';
Utils.updateSearchParameter('notifications', newSearch);
2024-07-18 11:58:42 +08:00
this.setState({ isShowNotificationDialog: !this.state.isShowNotificationDialog });
};
onNotificationListToggle = () => {
2024-07-18 11:58:42 +08:00
this.setState({ showNotice: false });
};
onMarkAllNotifications = () => {
let generalNoticeListUnseen = this.state.generalNoticeListUnseen;
let discussionNoticeListUnseen = this.state.discussionNoticeListUnseen;
if (this.state.currentTab === 'general') {
seafileAPI.updateNotifications().then((res) => {
this.setState({
generalNoticeList: this.state.generalNoticeList.map(item => {
item.seen = true;
return item;
}),
generalNoticeListUnseen: 0,
totalUnseenCount: discussionNoticeListUnseen
});
}).catch((error) => {
this.setState({
errorMsg: Utils.getErrorMsg(error, true) // true: show login tip if 403
});
});
} else if (this.state.currentTab === 'discussion') {
seafileAPI.updateSdocNotifications().then((res) => {
this.setState({
discussionNoticeList: this.state.discussionNoticeList.map(item => {
item.seen = true;
return item;
}),
discussionNoticeListUnseen: 0,
totalUnseenCount: generalNoticeListUnseen
});
}).catch((error) => {
this.setState({
errorMsg: Utils.getErrorMsg(error, true) // true: show login tip if 403
});
});
}
};
updateTotalUnseenCount = (noticeType) => {
if (noticeType === 'general') {
this.setState({
generalNoticeListUnseen: 0,
totalUnseenCount: this.state.discussionNoticeListUnseen
});
} else if (noticeType === 'discussion') {
this.setState({
discussionNoticeListUnseen: 0,
totalUnseenCount: this.state.generalNoticeListUnseen
});
}
};
render() {
const { totalUnseenCount, currentTab, generalNoticeList, discussionNoticeList, generalNoticeListUnseen, discussionNoticeListUnseen } = this.state;
return (
<div id="notifications">
<a href="#" onClick={this.onClick} className="no-deco" id="notice-icon" title={gettext('Notifications')} aria-label={gettext('Notifications')}>
<IconBtn id="notification-popover" symbol="notification" size={32} className="sf-icon-bell" />
<span className={`num ${totalUnseenCount ? '' : 'hide'}`}>{totalUnseenCount}</span>
</a>
{this.state.showNotice &&
<NotificationPopover
headerText={gettext('Notification')}
bodyText={gettext('Mark all as read')}
footerText={gettext('View all notifications')}
2024-11-21 10:59:13 +08:00
currentTab={currentTab}
onNotificationListToggle={this.onNotificationListToggle}
onNotificationDialogToggle={this.onNotificationDialogToggle}
onMarkAllNotifications={this.onMarkAllNotifications}
2024-11-21 10:59:13 +08:00
tabItemClick={this.tabItemClick}
2024-12-02 10:55:29 +08:00
generalNoticeListUnseen={generalNoticeListUnseen}
discussionNoticeListUnseen={discussionNoticeListUnseen}
>
2024-12-02 10:55:29 +08:00
{currentTab === 'general' &&
2024-11-22 11:18:42 +08:00
<ul className="notice-list list-unstyled" id="notice-popover">
2024-12-02 10:55:29 +08:00
{generalNoticeList.map(item => {
return (
<NoticeItem key={item.id} noticeItem={item} onNoticeItemClick={this.onNoticeItemClick}/>
);
2024-11-22 11:18:42 +08:00
})}
</ul>
}
2024-12-02 10:55:29 +08:00
{currentTab === 'discussion' &&
2024-11-22 11:18:42 +08:00
<ul className="notice-list list-unstyled" id="notice-popover">
2024-12-02 10:55:29 +08:00
{discussionNoticeList.map(item => {
return (
<NoticeItem key={item.id} noticeItem={item} onNoticeItemClick={this.onNoticeItemClick}/>
);
2024-11-22 11:18:42 +08:00
})}
</ul>
}
</NotificationPopover>
}
{this.state.isShowNotificationDialog &&
2024-12-02 10:55:29 +08:00
<UserNotificationsDialog
onNotificationDialogToggle={this.onNotificationDialogToggle}
generalNoticeListUnseen={generalNoticeListUnseen}
discussionNoticeListUnseen={discussionNoticeListUnseen}
updateTotalUnseenCount={this.updateTotalUnseenCount}
2024-12-02 10:55:29 +08:00
/>
}
</div>
2018-10-16 18:19:51 +08:00
);
}
}
export default Notification;