2022-09-11 11:42:40 -07:00
|
|
|
import registerEvents from 'retrospring/utilities/registerEvents';
|
|
|
|
import { enableHandler } from './enable';
|
|
|
|
import { dismissHandler } from "./dismiss";
|
2023-01-01 13:06:01 -08:00
|
|
|
import { unsubscribeHandler, checkSubscription } from "retrospring/features/webpush/unsubscribe";
|
|
|
|
|
|
|
|
let subscriptionChecked = false;
|
2022-09-11 11:42:40 -07:00
|
|
|
|
|
|
|
export default (): void => {
|
2023-01-01 12:28:48 -08:00
|
|
|
const swCapable = 'serviceWorker' in navigator;
|
|
|
|
const notificationCapable = 'Notification' in window;
|
2022-09-11 11:42:40 -07:00
|
|
|
|
|
|
|
if (swCapable && notificationCapable) {
|
2022-12-22 14:48:03 -08:00
|
|
|
const enableBtn = document.querySelector('button[data-action="push-enable"]');
|
2022-09-11 11:42:40 -07:00
|
|
|
|
2022-12-22 14:48:03 -08:00
|
|
|
navigator.serviceWorker.getRegistration().then(registration =>
|
|
|
|
registration?.pushManager.getSubscription().then(subscription => {
|
|
|
|
if (subscription) {
|
2023-01-01 12:28:48 -08:00
|
|
|
document.querySelector('button[data-action="push-enable"]')?.classList.add('d-none');
|
|
|
|
document.querySelector('[data-action="push-disable"]')?.classList.remove('d-none');
|
2023-01-01 13:06:01 -08:00
|
|
|
|
|
|
|
if (!subscriptionChecked) {
|
|
|
|
checkSubscription(subscription);
|
|
|
|
subscriptionChecked = true;
|
|
|
|
}
|
2023-01-01 12:28:48 -08:00
|
|
|
} else {
|
|
|
|
enableBtn?.classList.remove('d-none');
|
|
|
|
|
2022-10-23 07:00:21 -07:00
|
|
|
if (localStorage.getItem('dismiss-push-settings-prompt') == null) {
|
|
|
|
document.querySelector('.push-settings')?.classList.remove('d-none');
|
|
|
|
}
|
|
|
|
}
|
2022-12-22 14:48:03 -08:00
|
|
|
}));
|
2022-09-11 11:42:40 -07:00
|
|
|
}
|
2022-10-23 07:00:21 -07:00
|
|
|
|
|
|
|
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
|
|
|
|
},
|
|
|
|
]);
|
2022-09-11 11:42:40 -07:00
|
|
|
}
|