Merge pull request #924 from Retrospring/fix/webpush-multiple-init

Prevent WebPush init from attaching events multiple times
This commit is contained in:
Karina Kwiatek 2023-01-05 11:32:43 +00:00 committed by GitHub
commit 3180e10449
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 32 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 initWebpushSettingsButtons from 'retrospring/features/webpush/settingsButtons';
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', initWebpushSettingsButtons);
window['Stimulus'] = Application.start();
const context = require.context('../retrospring/controllers', true, /\.ts$/);

View File

@ -1,39 +1,9 @@
import registerEvents from 'retrospring/utilities/registerEvents';
import { enableHandler } from './enable';
import { dismissHandler } from "./dismiss";
import { unsubscribeHandler, checkSubscription } from "retrospring/features/webpush/unsubscribe";
let subscriptionChecked = false;
import { unsubscribeHandler } from "retrospring/features/webpush/unsubscribe";
export default (): void => {
const swCapable = 'serviceWorker' in navigator;
const notificationCapable = 'Notification' in window;
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;
}
return;
}
enableBtn?.classList.remove('d-none');
if (localStorage.getItem('dismiss-push-settings-prompt') == null) {
document.querySelector('.push-settings')?.classList.remove('d-none');
}
});
}
registerEvents([
{type: 'click', target: '[data-action="push-enable"]', handler: enableHandler, global: true},
{type: 'click', target: '[data-action="push-dismiss"]', handler: dismissHandler, global: true},

View File

@ -0,0 +1,34 @@
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;
return;
}
} else {
enableBtn?.classList.remove('d-none');
}
if (localStorage.getItem('dismiss-push-settings-prompt') == null) {
document.querySelector('.push-settings')?.classList.remove('d-none');
}
});
}
}