adt-js-components 1.11.0 → 1.11.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adt-js-components",
3
- "version": "1.11.0",
3
+ "version": "1.11.1",
4
4
  "description": "JavaScript components for Nette framework",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -1,7 +1,21 @@
1
- import { getToken } from "firebase/messaging";
1
+ import { getToken, deleteToken } from "firebase/messaging";
2
2
 
3
3
  const run = (config) => {
4
- $('[data-adt-notifications]').on('click', function () {
4
+ const $enableBtn = $('[data-adt-notifications-enable]');
5
+ const $disableBtn = $('[data-adt-notifications-disable]');
6
+
7
+ const updateButtons = () => {
8
+ if (Notification.permission === 'granted') {
9
+ $enableBtn.hide();
10
+ $disableBtn.show();
11
+ } else {
12
+ $enableBtn.show();
13
+ $disableBtn.hide();
14
+ }
15
+ };
16
+
17
+ // Enable notifications
18
+ $enableBtn.on('click', function () {
5
19
  if (window.messaging) {
6
20
  Notification.requestPermission().then(function (permission) {
7
21
  if (permission !== 'granted') {
@@ -12,9 +26,11 @@ const run = (config) => {
12
26
  getToken(window.messaging, { vapidKey: config.vapidKey })
13
27
  .then(function (currentToken) {
14
28
  if (currentToken) {
29
+ window.adtNotificationsToken = currentToken;
15
30
  $.nette.ajax({
16
31
  url: config.setFirebaseTokenLink.replace('__firebaseToken__', currentToken)
17
32
  });
33
+ updateButtons();
18
34
  } else {
19
35
  alert(_('appJs.firebase.error.notificationsPermissionError'));
20
36
  }
@@ -28,6 +44,50 @@ const run = (config) => {
28
44
  alert(_('appJs.firebase.error.notificationsNotSupported'));
29
45
  }
30
46
  });
47
+
48
+ // Disable notifications
49
+ $disableBtn.on('click', function () {
50
+ if (window.messaging) {
51
+ deleteToken(window.messaging)
52
+ .then(function () {
53
+ // Unregister service worker
54
+ return navigator.serviceWorker.getRegistrations();
55
+ })
56
+ .then(function (registrations) {
57
+ registrations.forEach(function (registration) {
58
+ if (registration.active && registration.active.scriptURL.includes('firebase-messaging-sw')) {
59
+ registration.unregister();
60
+ }
61
+ });
62
+ })
63
+ .catch(function (err) {
64
+ console.error(err);
65
+ });
66
+ }
67
+
68
+ $.nette.ajax({
69
+ url: config.removeAllFirebaseTokensLink
70
+ });
71
+
72
+ window.adtNotificationsToken = null;
73
+ updateButtons();
74
+ });
75
+
76
+ // Initial state: check if notifications are already enabled
77
+ if (Notification.permission === 'granted' && window.messaging) {
78
+ getToken(window.messaging, { vapidKey: config.vapidKey })
79
+ .then(function (currentToken) {
80
+ if (currentToken) {
81
+ window.adtNotificationsToken = currentToken;
82
+ }
83
+ updateButtons();
84
+ })
85
+ .catch(function () {
86
+ updateButtons();
87
+ });
88
+ } else {
89
+ updateButtons();
90
+ }
31
91
  }
32
92
 
33
93
  export default { run };