update to notify
This commit is contained in:
parent
3e77f25b87
commit
0e22240d11
@ -41,8 +41,9 @@ function urlBase64ToUint8Array(base64String) {
|
||||
// This function handles the actual subscription process
|
||||
async function subscribeToNotifications() {
|
||||
try {
|
||||
// CORRECT
|
||||
const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfNh_zIiNumLgWFKSvD6pMhnRbjhXVY_pU'; const token = localStorage.getItem('authToken');
|
||||
// This key is not a secret, it's safe to have here.
|
||||
const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfNh_zIiNumLgWFKSvD6pMhnRbjhXVY_pU';
|
||||
const token = localStorage.getItem('authToken');
|
||||
|
||||
if (!token || !publicVapidKey) {
|
||||
return console.error('Auth token or VAPID key is missing.');
|
||||
@ -54,8 +55,13 @@ const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfN
|
||||
applicationServerKey: urlBase64ToUint8Array(publicVapidKey)
|
||||
});
|
||||
|
||||
await apiCall('/subscribe', 'POST', subscription);
|
||||
showMessage('You are now subscribed to notifications!', 'success');
|
||||
const res = await apiCall('/subscribe', 'POST', subscription);
|
||||
|
||||
// If the subscription is saved successfully, set a flag so we don't do it again
|
||||
if (res.success) {
|
||||
localStorage.setItem('subscriptionSent', 'true');
|
||||
showMessage('You are now subscribed to notifications!', 'success');
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('Error subscribing to notifications:', error);
|
||||
showMessage('Failed to subscribe. Please ensure notifications are allowed for this site.', 'error');
|
||||
@ -63,23 +69,35 @@ const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfN
|
||||
}
|
||||
|
||||
// NEW: This function creates the pop-up prompt for the user
|
||||
function promptForNotifications() {
|
||||
// 1. Don't ask if permission is already granted or denied
|
||||
if (Notification.permission !== 'default') {
|
||||
return;
|
||||
}
|
||||
// 2. Don't ask if we've already prompted them before
|
||||
if (localStorage.getItem('notificationPrompted')) {
|
||||
return;
|
||||
}
|
||||
// 3. Wait a couple of seconds after login to ask
|
||||
setTimeout(() => {
|
||||
if (confirm("Enable notifications to receive important updates about your time-off requests and notes?")) {
|
||||
function setupNotifications() {
|
||||
// Don't do anything if service workers aren't supported
|
||||
if (!('serviceWorker' in navigator)) return;
|
||||
|
||||
// Case 1: Permission is already granted
|
||||
if (Notification.permission === 'granted') {
|
||||
// Only try to subscribe if we haven't already sent the subscription to the server
|
||||
if (!localStorage.getItem('subscriptionSent')) {
|
||||
subscribeToNotifications();
|
||||
}
|
||||
// Remember that we've prompted them, so we don't ask again
|
||||
localStorage.setItem('notificationPrompted', 'true');
|
||||
}, 2000);
|
||||
return;
|
||||
}
|
||||
|
||||
// Case 2: Permission has been denied, so we don't ask again
|
||||
if (Notification.permission === 'denied') {
|
||||
return;
|
||||
}
|
||||
|
||||
// Case 3: Permission is 'default' (user hasn't chosen yet)
|
||||
// Only ask if we haven't prompted them before
|
||||
if (!localStorage.getItem('notificationPrompted')) {
|
||||
setTimeout(() => {
|
||||
if (confirm("Enable notifications to receive important updates?")) {
|
||||
subscribeToNotifications();
|
||||
}
|
||||
// Remember that we've prompted them so we don't ask again on the next login
|
||||
localStorage.setItem('notificationPrompted', 'true');
|
||||
}, 2000);
|
||||
}
|
||||
}
|
||||
|
||||
// --- EVENT HANDLERS ---
|
||||
@ -99,7 +117,7 @@ async function handleAuthSubmit(e) {
|
||||
}
|
||||
|
||||
function handleSignOut(message) {
|
||||
localStorage.clear();
|
||||
localStorage.clear(); // This already clears everything, which is what we want.
|
||||
authToken = null;
|
||||
user = null;
|
||||
if (message) {
|
||||
@ -414,7 +432,7 @@ function initializeApp() {
|
||||
renderEmployeeDashboard();
|
||||
}
|
||||
// Ask for notification permission after user is logged in
|
||||
promptForNotifications();
|
||||
setupNotifications();
|
||||
} else {
|
||||
userControls.classList.add('hidden');
|
||||
renderAuthView();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user