philomena/assets/js/notifications.js

82 lines
2.3 KiB
JavaScript
Raw Normal View History

2019-10-05 02:09:52 +02:00
/**
* Notifications
*/
import { fetchJson, handleError } from './utils/requests';
import { $, $$, hideEl, toggleEl } from './utils/dom';
2019-11-17 18:50:42 +01:00
import { delegate } from './utils/events';
2019-10-05 02:09:52 +02:00
import store from './utils/store';
const NOTIFICATION_INTERVAL = 600000,
NOTIFICATION_EXPIRES = 300000;
function makeRequest(verb, action, body) {
return fetchJson(verb, `${window.booru.apiEndpoint}notifications/${action}.json`, body).then(handleError);
}
2019-11-17 18:50:42 +01:00
function bindSubscriptionLinks() {
delegate(document, 'fetchcomplete', {
'.js-subscription-link': event => {
const target = $("#js-subscription-target");
event.detail.text().then(text => {
target.outerHTML = text;
});
}
});
2019-10-05 02:09:52 +02:00
}
function markRead(data) {
const notificationId = data.value;
const notification = $(`.js-notification-id-${notificationId}`);
makeRequest('PUT', 'mark_read', { id: notificationId })
.then(() => hideEl(notification))
.catch(() => data.el.textContent = 'Error!');
}
function getNewNotifications() {
if (document.hidden || !store.hasExpired('notificationCount')) {
return;
}
makeRequest('GET', 'unread').then(response => response.json()).then(({ data }) => {
updateNotificationTicker(data.length);
storeNotificationCount(data.length);
});
}
function updateNotificationTicker(notificationCount) {
const ticker = $('.js-notification-ticker');
const parsedNotificationCount = Number(notificationCount);
ticker.dataset.notificationCount = parsedNotificationCount;
ticker.textContent = parsedNotificationCount;
}
function storeNotificationCount(notificationCount) {
// The current number of notifications are stored along with the time when the data expires
store.setWithExpireTime('notificationCount', notificationCount, NOTIFICATION_EXPIRES);
}
function setupNotifications() {
if (!window.booru.userIsSignedIn) return;
// Fetch notifications from the server at a regular interval
setInterval(getNewNotifications, NOTIFICATION_INTERVAL);
// Update the current number of notifications based on the latest page load
storeNotificationCount($('.js-notification-ticker').dataset.notificationCount);
// Update ticker when the stored value changes - this will occur in all open tabs
store.watch('notificationCount', updateNotificationTicker);
2019-11-17 18:50:42 +01:00
bindSubscriptionLinks();
2019-10-05 02:09:52 +02:00
}
2019-11-17 18:50:42 +01:00
export { setupNotifications, markRead };