add notification for new post in thread
This commit is contained in:
45
js/thread.js
45
js/thread.js
@ -35,4 +35,49 @@
|
||||
form.action = `/post/${postId}/delete`
|
||||
})
|
||||
}
|
||||
|
||||
let newPostSubscription = null;
|
||||
|
||||
function hideNotification() {
|
||||
const notification = document.getElementById('new-post-notification');
|
||||
notification.classList.add('hidden');
|
||||
}
|
||||
|
||||
function showNewPostNotification(url) {
|
||||
const notification = document.getElementById("new-post-notification");
|
||||
|
||||
notification.classList.remove("hidden");
|
||||
|
||||
document.getElementById("dismiss-new-post-button").onclick = () => {
|
||||
hideNotification();
|
||||
reconnectSSE();
|
||||
}
|
||||
|
||||
document.getElementById("go-to-new-post-button").href = url;
|
||||
|
||||
document.getElementById("unsub-new-post-button").onclick = () => {
|
||||
hideNotification()
|
||||
}
|
||||
}
|
||||
|
||||
function reconnectSSE() {
|
||||
if (newPostSubscription) newPostSubscription.close();
|
||||
|
||||
const threadEndpoint = document.getElementById("thread-subscribe-endpoint").value;
|
||||
newPostSubscription = new EventSource(threadEndpoint);
|
||||
newPostSubscription.onerror = (e) => {
|
||||
console.error(e);
|
||||
};
|
||||
newPostSubscription.addEventListener("new_post_url", (e) => {
|
||||
showNewPostNotification(e.data);
|
||||
newPostSubscription.close();
|
||||
})
|
||||
}
|
||||
window.addEventListener('beforeunload', () => {
|
||||
if(newPostSubscription)
|
||||
{
|
||||
newPostSubscription.close();
|
||||
}
|
||||
});
|
||||
reconnectSSE();
|
||||
}
|
||||
|
Reference in New Issue
Block a user