update to notify

This commit is contained in:
chris 2025-10-09 21:26:18 -04:00
parent 3e77f25b87
commit 0e22240d11

View File

@ -41,8 +41,9 @@ function urlBase64ToUint8Array(base64String) {
// This function handles the actual subscription process // This function handles the actual subscription process
async function subscribeToNotifications() { async function subscribeToNotifications() {
try { try {
// CORRECT // This key is not a secret, it's safe to have here.
const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfNh_zIiNumLgWFKSvD6pMhnRbjhXVY_pU'; const token = localStorage.getItem('authToken'); const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfNh_zIiNumLgWFKSvD6pMhnRbjhXVY_pU';
const token = localStorage.getItem('authToken');
if (!token || !publicVapidKey) { if (!token || !publicVapidKey) {
return console.error('Auth token or VAPID key is missing.'); return console.error('Auth token or VAPID key is missing.');
@ -54,8 +55,13 @@ const publicVapidKey = 'BI1mWxe0yAsMw_iDjmb4Te2ByWwKuHhWsLYFilk7prozsnCEbtNHEJfN
applicationServerKey: urlBase64ToUint8Array(publicVapidKey) applicationServerKey: urlBase64ToUint8Array(publicVapidKey)
}); });
await apiCall('/subscribe', 'POST', subscription); const res = await apiCall('/subscribe', 'POST', subscription);
showMessage('You are now subscribed to notifications!', 'success');
// 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) { } catch (error) {
console.error('Error subscribing to notifications:', error); console.error('Error subscribing to notifications:', error);
showMessage('Failed to subscribe. Please ensure notifications are allowed for this site.', '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 // NEW: This function creates the pop-up prompt for the user
function promptForNotifications() { function setupNotifications() {
// 1. Don't ask if permission is already granted or denied // Don't do anything if service workers aren't supported
if (Notification.permission !== 'default') { if (!('serviceWorker' in navigator)) return;
return;
} // Case 1: Permission is already granted
// 2. Don't ask if we've already prompted them before if (Notification.permission === 'granted') {
if (localStorage.getItem('notificationPrompted')) { // Only try to subscribe if we haven't already sent the subscription to the server
return; if (!localStorage.getItem('subscriptionSent')) {
}
// 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?")) {
subscribeToNotifications(); subscribeToNotifications();
} }
// Remember that we've prompted them, so we don't ask again return;
localStorage.setItem('notificationPrompted', 'true'); }
}, 2000);
// 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 --- // --- EVENT HANDLERS ---
@ -99,7 +117,7 @@ async function handleAuthSubmit(e) {
} }
function handleSignOut(message) { function handleSignOut(message) {
localStorage.clear(); localStorage.clear(); // This already clears everything, which is what we want.
authToken = null; authToken = null;
user = null; user = null;
if (message) { if (message) {
@ -414,7 +432,7 @@ function initializeApp() {
renderEmployeeDashboard(); renderEmployeeDashboard();
} }
// Ask for notification permission after user is logged in // Ask for notification permission after user is logged in
promptForNotifications(); setupNotifications();
} else { } else {
userControls.classList.add('hidden'); userControls.classList.add('hidden');
renderAuthView(); renderAuthView();