Move subscription check logic into separate initializer

This commit is contained in:
Karina Kwiatek 2023-01-05 11:08:13 +01:00
parent e93531a4af
commit d288f6d6a3
3 changed files with 48 additions and 47 deletions

View File

@ -14,6 +14,7 @@ import initModeration from 'retrospring/features/moderation';
import initMemes from 'retrospring/features/memes';
import initFront from 'retrospring/features/front';
import initWebpush from 'retrospring/features/webpush';
import initWebpushSubscriptionCheck from 'retrospring/features/webpush/subscriptionCheck';
start();
document.addEventListener('turbo:load', initCapabilities);
@ -27,7 +28,8 @@ document.addEventListener('DOMContentLoaded', initQuestion);
document.addEventListener('DOMContentLoaded', initModeration);
document.addEventListener('DOMContentLoaded', initMemes);
document.addEventListener('turbo:load', initFront);
document.addEventListener('turbo:load', initWebpush);
document.addEventListener('DOMContentLoaded', initWebpush);
document.addEventListener('turbo:load', initWebpushSubscriptionCheck);
window['Stimulus'] = Application.start();
const context = require.context('../retrospring/controllers', true, /\.ts$/);

View File

@ -1,52 +1,18 @@
import registerEvents from 'retrospring/utilities/registerEvents';
import { enableHandler } from './enable';
import { dismissHandler } from "./dismiss";
import { unsubscribeHandler, checkSubscription } from "retrospring/features/webpush/unsubscribe";
let initialized = false;
import { unsubscribeHandler } from "retrospring/features/webpush/unsubscribe";
export default (): void => {
const swCapable = 'serviceWorker' in navigator;
const notificationCapable = 'Notification' in window;
// We want to adjust enable/disable buttons on every page load
// because the enable button appears on both the settings and inbox pages.
if (swCapable && notificationCapable) {
const enableBtn = document.querySelector('button[data-action="push-enable"]');
navigator.serviceWorker.getRegistration().then(async registration => {
const subscription = await registration?.pushManager.getSubscription();
if (subscription) {
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
if (!initialized) {
checkSubscription(subscription);
}
return;
}
enableBtn?.classList.remove('d-none');
if (localStorage.getItem('dismiss-push-settings-prompt') == null) {
document.querySelector('.push-settings')?.classList.remove('d-none');
}
});
}
if (!initialized) {
registerEvents([
{type: 'click', target: '[data-action="push-enable"]', handler: enableHandler, global: true},
{type: 'click', target: '[data-action="push-dismiss"]', handler: dismissHandler, global: true},
{type: 'click', target: '[data-action="push-disable"]', handler: unsubscribeHandler, global: true},
{
type: 'click',
target: '[data-action="push-remove-all"]',
handler: unsubscribeHandler,
global: true
},
]);
initialized = true;
}
registerEvents([
{type: 'click', target: '[data-action="push-enable"]', handler: enableHandler, global: true},
{type: 'click', target: '[data-action="push-dismiss"]', handler: dismissHandler, global: true},
{type: 'click', target: '[data-action="push-disable"]', handler: unsubscribeHandler, global: true},
{
type: 'click',
target: '[data-action="push-remove-all"]',
handler: unsubscribeHandler,
global: true
},
]);
}

View File

@ -0,0 +1,33 @@
import { checkSubscription } from "retrospring/features/webpush/unsubscribe";
let subscriptionChecked = false;
export default (): void => {
const swCapable = 'serviceWorker' in navigator;
const notificationCapable = 'Notification' in window;
// We want to adjust enable/disable buttons on every page load
// because the enable button appears on both the settings and inbox pages.
if (swCapable && notificationCapable) {
const enableBtn = document.querySelector('button[data-action="push-enable"]');
navigator.serviceWorker.getRegistration().then(async registration => {
const subscription = await registration?.pushManager.getSubscription();
if (subscription) {
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
if (!subscriptionChecked) {
checkSubscription(subscription);
subscriptionChecked = true;
}
}
enableBtn?.classList.remove('d-none');
if (localStorage.getItem('dismiss-push-settings-prompt') == null) {
document.querySelector('.push-settings')?.classList.remove('d-none');
}
});
}
}