diff --git a/app/javascript/packs/application.ts b/app/javascript/packs/application.ts index d0389539..f4832317 100644 --- a/app/javascript/packs/application.ts +++ b/app/javascript/packs/application.ts @@ -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$/); diff --git a/app/javascript/retrospring/features/webpush/index.ts b/app/javascript/retrospring/features/webpush/index.ts index 792f0fd8..6821c98e 100644 --- a/app/javascript/retrospring/features/webpush/index.ts +++ b/app/javascript/retrospring/features/webpush/index.ts @@ -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}, diff --git a/app/javascript/retrospring/features/webpush/settingsButtons.ts b/app/javascript/retrospring/features/webpush/settingsButtons.ts new file mode 100644 index 00000000..6ef01a24 --- /dev/null +++ b/app/javascript/retrospring/features/webpush/settingsButtons.ts @@ -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'); + } + }); + } +}