2020-07-23 16:22:57 +02:00
|
|
|
// @flow
|
2020-08-11 22:32:03 +02:00
|
|
|
import * as ICONS from 'constants/icons';
|
2020-07-23 16:22:57 +02:00
|
|
|
import React from 'react';
|
|
|
|
import Page from 'component/page';
|
|
|
|
import Spinner from 'component/spinner';
|
2021-03-05 05:02:20 +01:00
|
|
|
import { FormField } from 'component/common/form';
|
2020-07-23 16:22:57 +02:00
|
|
|
import Notification from 'component/notification';
|
2020-08-11 22:32:03 +02:00
|
|
|
import Button from 'component/button';
|
2021-03-05 05:02:20 +01:00
|
|
|
import usePersistedState from 'effects/use-persisted-state';
|
2020-08-21 21:44:54 +02:00
|
|
|
import Yrbl from 'component/yrbl';
|
2021-03-05 05:02:20 +01:00
|
|
|
import * as NOTIFICATIONS from 'constants/notifications';
|
2020-07-23 16:22:57 +02:00
|
|
|
|
|
|
|
type Props = {
|
2020-08-21 21:44:54 +02:00
|
|
|
notifications: Array<Notification>,
|
2020-07-23 16:22:57 +02:00
|
|
|
fetching: boolean,
|
2020-08-10 22:47:39 +02:00
|
|
|
unreadCount: number,
|
2020-08-21 21:44:54 +02:00
|
|
|
unseenCount: number,
|
|
|
|
doSeeAllNotifications: () => void,
|
2020-08-10 22:47:39 +02:00
|
|
|
doReadNotifications: () => void,
|
2020-07-23 16:22:57 +02:00
|
|
|
};
|
|
|
|
|
2021-03-05 05:02:20 +01:00
|
|
|
const ALL_NOTIFICATIONS = 'all';
|
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
export default function NotificationsPage(props: Props) {
|
2020-08-21 21:44:54 +02:00
|
|
|
const { notifications, fetching, unreadCount, unseenCount, doSeeAllNotifications, doReadNotifications } = props;
|
|
|
|
const hasNotifications = notifications.length > 0;
|
2021-03-05 05:02:20 +01:00
|
|
|
const [filterBy, setFilterBy] = usePersistedState('notifications--filter-by', 'all');
|
|
|
|
const [filteredNotifications, setFilteredNotifications] = React.useState(notifications);
|
|
|
|
const isFiltered = filterBy !== ALL_NOTIFICATIONS;
|
|
|
|
|
|
|
|
const NOTIFICATION_FILTER_TYPES = [
|
|
|
|
ALL_NOTIFICATIONS,
|
|
|
|
NOTIFICATIONS.NOTIFICATION_CREATOR_SUBSCRIBER,
|
|
|
|
NOTIFICATIONS.NOTIFICATION_COMMENT,
|
|
|
|
NOTIFICATIONS.NOTIFICATION_REPLY,
|
|
|
|
NOTIFICATIONS.DAILY_WATCH_AVAILABLE,
|
|
|
|
NOTIFICATIONS.DAILY_WATCH_REMIND,
|
|
|
|
NOTIFICATIONS.NEW_CONTENT,
|
|
|
|
];
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (unseenCount > 0 || unreadCount > 0) {
|
|
|
|
// If there are unread notifications when entering the page, reset to All.
|
|
|
|
setFilterBy(ALL_NOTIFICATIONS);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
if (notifications && filterBy !== ALL_NOTIFICATIONS) {
|
|
|
|
setFilteredNotifications(notifications.filter((n) => n.notification_rule === filterBy));
|
|
|
|
} else {
|
|
|
|
setFilteredNotifications(notifications);
|
|
|
|
}
|
|
|
|
}, [notifications, filterBy]);
|
2020-07-30 22:14:12 +02:00
|
|
|
|
2020-08-10 22:47:39 +02:00
|
|
|
React.useEffect(() => {
|
2020-12-14 19:52:17 +01:00
|
|
|
if (unseenCount > 0) {
|
|
|
|
doSeeAllNotifications();
|
2020-08-10 22:47:39 +02:00
|
|
|
}
|
2020-12-14 19:52:17 +01:00
|
|
|
}, [unseenCount, doSeeAllNotifications]);
|
2020-08-10 22:47:39 +02:00
|
|
|
|
2020-07-23 16:22:57 +02:00
|
|
|
return (
|
|
|
|
<Page>
|
2020-08-21 21:44:54 +02:00
|
|
|
{fetching && !hasNotifications && (
|
2020-07-23 16:22:57 +02:00
|
|
|
<div className="main--empty">
|
|
|
|
<Spinner delayed />
|
|
|
|
</div>
|
|
|
|
)}
|
2020-08-26 23:47:43 +02:00
|
|
|
{!fetching && (
|
2020-08-21 21:44:54 +02:00
|
|
|
<>
|
2021-03-10 05:11:45 +01:00
|
|
|
<div className="claim-list__header">
|
|
|
|
<h1 className="card__title">{__('Notifications')}</h1>
|
|
|
|
<div className="claim-list__alt-controls--wrap">
|
|
|
|
{fetching && <Spinner type="small" />}
|
2021-03-11 23:18:26 +01:00
|
|
|
|
2021-03-10 05:11:45 +01:00
|
|
|
{unreadCount > 0 && (
|
2021-03-05 05:02:20 +01:00
|
|
|
<Button
|
|
|
|
icon={ICONS.EYE}
|
|
|
|
onClick={doReadNotifications}
|
|
|
|
button="secondary"
|
|
|
|
label={__('Mark all as read')}
|
|
|
|
/>
|
2021-03-10 05:11:45 +01:00
|
|
|
)}
|
2021-03-11 23:18:26 +01:00
|
|
|
<FormField
|
|
|
|
className="notification__filter"
|
|
|
|
type="select"
|
|
|
|
name="filter"
|
|
|
|
value={filterBy || ALL_NOTIFICATIONS}
|
|
|
|
onChange={(e) => setFilterBy(e.target.value)}
|
|
|
|
>
|
|
|
|
{NOTIFICATION_FILTER_TYPES.map((type) => (
|
|
|
|
<option key={type} value={type}>
|
|
|
|
{/* i18fixme */}
|
|
|
|
{type === ALL_NOTIFICATIONS && __('All')}
|
|
|
|
{type === NOTIFICATIONS.NOTIFICATION_CREATOR_SUBSCRIBER && __('Followers')}
|
|
|
|
{type === NOTIFICATIONS.NOTIFICATION_COMMENT && __('Comments')}
|
|
|
|
{type === NOTIFICATIONS.NOTIFICATION_REPLY && __('Comment replies')}
|
|
|
|
{type === NOTIFICATIONS.DAILY_WATCH_AVAILABLE && __('Daily watch availability')}
|
|
|
|
{type === NOTIFICATIONS.DAILY_WATCH_REMIND && __('Daily watch reminders')}
|
|
|
|
{type === NOTIFICATIONS.NEW_CONTENT && __('New content')}
|
|
|
|
</option>
|
|
|
|
))}
|
|
|
|
</FormField>
|
2021-03-10 05:11:45 +01:00
|
|
|
</div>
|
|
|
|
</div>
|
2021-03-11 23:18:26 +01:00
|
|
|
{filteredNotifications && filteredNotifications.length > 0 ? (
|
|
|
|
<div className="card">
|
|
|
|
<div className="notification_list">
|
|
|
|
{filteredNotifications.map((notification, index) => {
|
|
|
|
return <Notification key={notification.id} notification={notification} />;
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<div className="main--empty">
|
|
|
|
<Yrbl
|
|
|
|
title={__('No notifications')}
|
|
|
|
subtitle={
|
|
|
|
<p>
|
|
|
|
{isFiltered
|
|
|
|
? __('Try selecting another filter.')
|
|
|
|
: __("You don't have any notifications yet, but they will be here when you do!")}
|
|
|
|
</p>
|
|
|
|
}
|
|
|
|
actions={
|
|
|
|
<div className="section__actions">
|
|
|
|
<Button button="primary" icon={ICONS.HOME} label={__('Go Home')} navigate="/" />
|
2021-03-05 05:02:20 +01:00
|
|
|
</div>
|
2021-03-11 23:18:26 +01:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-08-21 21:44:54 +02:00
|
|
|
</>
|
2020-07-23 16:22:57 +02:00
|
|
|
)}
|
|
|
|
</Page>
|
|
|
|
);
|
|
|
|
}
|