{"version":3,"file":"web_push_notifications_controller-Dt9zX62A.js","sources":["../../../app/javascript/controllers/web_push_notifications_controller.js"],"sourcesContent":["import { Controller } from \"@hotwired/stimulus\"\nimport { post } from \"@rails/request.js\"\n\nexport default class extends Controller {\n static targets = [\"notice\"]\n\n async connect() {\n await this.checkSubscriptionAndSendIfExists()\n }\n\n async requestPermission() {\n if (\"Notification\" in window) {\n const permission = await Notification.requestPermission()\n if (permission === \"granted\") {\n await this.createSubscriptionAndSend()\n this.hideNotice()\n }\n } else {\n console.warn(\"Push notifications not supported.\")\n }\n }\n\n async checkSubscriptionAndSendIfExists() {\n const subscription = await this.getSubscription()\n\n if (subscription) {\n await this.sendSubscriptionToServer(subscription)\n this.hideNotice()\n } else {\n this.showNotice()\n }\n }\n\n async createSubscriptionAndSend() {\n let subscription = await this.getSubscription()\n\n if (!subscription) {\n subscription = await this.subscribeToPushManager()\n }\n\n if (subscription) {\n await this.sendSubscriptionToServer(subscription)\n }\n }\n\n async getSubscription() {\n if ('serviceWorker' in navigator) {\n const sw = await navigator.serviceWorker.ready\n return await sw.pushManager.getSubscription()\n }\n return null\n }\n\n async subscribeToPushManager() {\n if ('serviceWorker' in navigator) {\n const sw = await navigator.serviceWorker.ready\n return await sw.pushManager.subscribe({\n userVisibleOnly: true,\n applicationServerKey: window.vapidPublicKey\n })\n }\n return null\n }\n\n async sendSubscriptionToServer(subscription) {\n const { endpoint } = subscription\n\n const p256dh = btoa(\n String.fromCharCode.apply(\n null,\n new Uint8Array(subscription.getKey('p256dh'))\n )\n );\n\n const auth = btoa(\n String.fromCharCode.apply(\n null,\n new Uint8Array(subscription.getKey('auth'))\n )\n );\n\n try {\n await post('/push_subscriptions', {\n body: {\n subscription: {\n endpoint,\n p256dh,\n auth\n }\n },\n })\n } catch (error) {\n console.error('Failed to send subscription to server:', error)\n }\n }\n\n noticeTargetConnected() {\n if(typeof Notification === 'undefined') return\n\n if (Notification.permission === \"denied\") {\n this.showNotice()\n }\n }\n\n showNotice() {\n if(this.hasNoticeTarget) {\n this.noticeTarget.classList.remove(\"hidden\")\n }\n }\n\n hideNotice() {\n if(this.hasNoticeTarget) {\n this.noticeTarget.classList.add(\"hidden\")\n }\n }\n}\n"],"names":["web_push_notifications_controller","Controller","subscription","endpoint","p256dh","auth","post","error","__publicField"],"mappings":"+PAGe,MAAKA,UAASC,CAAW,CAGtC,MAAM,SAAU,CACd,MAAM,KAAK,iCAAgC,CAC/C,CAEE,MAAM,mBAAoB,CACpB,iBAAkB,OACD,MAAM,aAAa,kBAAiB,IACpC,YACjB,MAAM,KAAK,0BAAyB,EACpC,KAAK,WAAU,GAGjB,QAAQ,KAAK,mCAAmC,CAEtD,CAEE,MAAM,kCAAmC,CACvC,MAAMC,EAAe,MAAM,KAAK,gBAAe,EAE3CA,GACF,MAAM,KAAK,yBAAyBA,CAAY,EAChD,KAAK,WAAU,GAEf,KAAK,WAAU,CAErB,CAEE,MAAM,2BAA4B,CAChC,IAAIA,EAAe,MAAM,KAAK,gBAAe,EAExCA,IACHA,EAAe,MAAM,KAAK,uBAAsB,GAG9CA,GACF,MAAM,KAAK,yBAAyBA,CAAY,CAEtD,CAEE,MAAM,iBAAkB,CACtB,MAAI,kBAAmB,UAEd,MADI,MAAM,UAAU,cAAc,OACzB,YAAY,gBAAe,EAEtC,IACX,CAEE,MAAM,wBAAyB,CAC7B,MAAI,kBAAmB,UAEd,MADI,MAAM,UAAU,cAAc,OACzB,YAAY,UAAU,CACpC,gBAAiB,GACjB,qBAAsB,OAAO,cAC9B,CAAA,EAEI,IACX,CAEE,MAAM,yBAAyBA,EAAc,CAC3C,KAAM,CAAE,SAAAC,CAAQ,EAAKD,EAEfE,EAAS,KACb,OAAO,aAAa,MAClB,KACA,IAAI,WAAWF,EAAa,OAAO,QAAQ,CAAC,CACpD,CACK,EAEKG,EAAO,KACX,OAAO,aAAa,MAClB,KACA,IAAI,WAAWH,EAAa,OAAO,MAAM,CAAC,CAClD,CACK,EAED,GAAI,CACF,MAAMI,EAA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