From c0f9733c1003d169bd06543fcbc8bcbae840190a Mon Sep 17 00:00:00 2001 From: David Gwilliam Date: Fri, 27 Mar 2026 13:23:32 -0700 Subject: [PATCH] feat: Add JavaScript for bottom nav, offline support, and local storage --- app/assets/js/app.js | 117 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) diff --git a/app/assets/js/app.js b/app/assets/js/app.js index 0692b84..aa99c23 100644 --- a/app/assets/js/app.js +++ b/app/assets/js/app.js @@ -1 +1,118 @@ import "../css/app.css"; + +// EuchreCamp Application JavaScript + +// Initialize the application +document.addEventListener('DOMContentLoaded', () => { + initBottomNav(); + initOfflineSupport(); + initThemePreference(); +}); + +// Bottom Navigation Active State +function initBottomNav() { + const currentPath = window.location.pathname; + const navItems = document.querySelectorAll('.bottom-nav-item'); + + navItems.forEach(item => { + if (item.getAttribute('href') === currentPath) { + item.classList.add('active'); + } + }); +} + +// Offline Support with Local Storage +function initOfflineSupport() { + // Cache recent data for offline access + if ('caches' in window) { + // Listen for online/offline events + window.addEventListener('online', handleOnline); + window.addEventListener('offline', handleOffline); + + // Check initial state + if (!navigator.onLine) { + handleOffline(); + } + } +} + +function handleOnline() { + console.log('App is online'); + // Clear any offline indicators + document.body.classList.remove('offline'); + + // Sync any pending changes + syncPendingChanges(); +} + +function handleOffline() { + console.log('App is offline'); + // Add offline indicator to body + document.body.classList.add('offline'); + + // Show offline notification + showOfflineNotification(); +} + +function showOfflineNotification() { + // Create or update offline notification + let notification = document.getElementById('offline-notification'); + if (!notification) { + notification = document.createElement('div'); + notification.id = 'offline-notification'; + notification.className = 'offline-notification'; + notification.innerHTML = ` + + + + Offline Mode - Some features may be limited + `; + document.body.appendChild(notification); + } +} + +function syncPendingChanges() { + // Implement sync logic here + // This would sync any locally stored changes to the server + console.log('Syncing pending changes...'); +} + +// Theme Preference +function initThemePreference() { + // Check for saved theme preference + const savedTheme = localStorage.getItem('euchrecamp-theme'); + if (savedTheme) { + document.documentElement.setAttribute('data-theme', savedTheme); + } +} + +// Utility Functions +function saveToLocalStorage(key, data) { + try { + localStorage.setItem(`euchrecamp-${key}`, JSON.stringify(data)); + return true; + } catch (e) { + console.error('Error saving to localStorage:', e); + return false; + } +} + +function getFromLocalStorage(key) { + try { + const data = localStorage.getItem(`euchrecamp-${key}`); + return data ? JSON.parse(data) : null; + } catch (e) { + console.error('Error reading from localStorage:', e); + return null; + } +} + +function clearLocalStorage(key) { + try { + localStorage.removeItem(`euchrecamp-${key}`); + return true; + } catch (e) { + console.error('Error clearing localStorage:', e); + return false; + } +}