Skip to content

Commit

Permalink
test value
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Apr 19, 2024
1 parent 03db686 commit 98af8ee
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/packs/entrypoints/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// appropriate layout file, like app/views/layouts/application.html.erb

// Default Rails javascript and turbolinks
import Rails from 'rails-ujs';
Rails.start();
// import Rails from 'rails-ujs';
// Rails.start();

import "chartkick/chart.js"
import "highcharts"
Expand Down
60 changes: 31 additions & 29 deletions app/packs/entrypoints/controllers/profile_webpush_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,37 +23,39 @@ export default class extends Controller {
setupPushNotifications() {
const applicationServerKey = this.urlBase64ToUint8Array(this.vapidPublicValue);

navigator.serviceWorker.register("/service-worker.js", {scope: "./" }).then((registration) => {
registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}).then((subscription) => {

const endpoint = subscription.endpoint;
const p256dh = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh'))));
const auth = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth'))));

fetch('/push_subscriptions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify({
subscription: {
endpoint: endpoint,
p256dh: p256dh,
auth: auth
}
})
if (window.isSecureContext) {
navigator.serviceWorker.register("/service-worker.js", {scope: "./" }).then((registration) => {
registration.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey
}).then((subscription) => {

const endpoint = subscription.endpoint;
const p256dh = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('p256dh'))));
const auth = btoa(String.fromCharCode.apply(null, new Uint8Array(subscription.getKey('auth'))));

fetch('/push_subscriptions', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-CSRF-Token': document.querySelector('meta[name="csrf-token"]').getAttribute('content')
},
body: JSON.stringify({
subscription: {
endpoint: endpoint,
p256dh: p256dh,
auth: auth
}
})
});

localStorage.setItem('block-webpush-modal', 'true');
document.querySelector('.webpush-modal').style.display = 'none';
this.buttonTarget.style.disabled = true;
this.buttonTarget.classList.add('disabled');
});

localStorage.setItem('block-webpush-modal', 'true');
document.querySelector('.webpush-modal').style.display = 'none';
this.buttonTarget.style.disabled = true;
this.buttonTarget.classList.add('disabled');
});
});
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export default class extends Controller {
};

connect() {
console.log('Push Notification Controller connected');

if (!this.userLoginValue) return;

let subscribed = localStorage.getItem('block-webpush-modal');
Expand Down
2 changes: 1 addition & 1 deletion app/packs/entrypoints/controllers/wishlist_controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Controller } from "@hotwired/stimulus"
// import Rails from 'rails-ujs';
import Rails from 'rails-ujs';

export default class extends Controller {
connect() {
Expand Down
3 changes: 3 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<%= csp_meta_tag %>
<%= javascript_pack_tag 'application', 'data-turbo-track': 'reload' %>
<%= stylesheet_pack_tag 'application', "data-turbo-track": 'reload' %>

<style>
table thead {
background-color: black !important;
Expand Down Expand Up @@ -36,6 +37,7 @@
}
}
</style>

</head>
<body data-controller="push-notification"
data-push-notification-vapid-public-value="<%= Rails.configuration.customization[:vapid_public] %>"
Expand Down Expand Up @@ -74,5 +76,6 @@
<%= render 'common/footer' %>
<%= render 'google_analytics', tracking_id: google_analytics.tracking_id if google_analytics.enabled? && show_google_analytics? %>
</div>

</body>
</html>

0 comments on commit 98af8ee

Please sign in to comment.