| | |
| | | /** |
| | | * FrontendInteractions - Unified class for frontend user interactions |
| | | * Handles: Favourites, Votes, and related user actions |
| | | */ |
| | | class UserInteractions { |
| | |
| | | return; // Don't initialize if not logged in |
| | | } |
| | | |
| | | this.stores = {}; |
| | | |
| | | // Initialize favourites store |
| | | this.favouritesStore = window.jvbStore.register( |
| | | this.stores['favourites'] = window.jvbStore.register( |
| | | 'favourites', |
| | | { |
| | | storeName: 'favourites', |
| | |
| | | ); |
| | | |
| | | // Initialize favourites lists store |
| | | this.listsStore = window.jvbStore.register( |
| | | this.stores['lists'] = window.jvbStore.register( |
| | | 'favourites_lists', |
| | | { |
| | | storeName: 'lists', |
| | |
| | | ); |
| | | |
| | | // Initialize votes store |
| | | this.votesStore = window.jvbStore.register( |
| | | this.stores['votes'] = window.jvbStore.register( |
| | | 'votes', |
| | | { |
| | | storeName: 'votes', |
| | |
| | | } |
| | | } |
| | | |
| | | // Lazy initialization using requestIdleCallback for better performance |
| | | |
| | | function initFrontendInteractions() { |
| | | if (window.auth.getUser()) { |
| | | window.jvbInteractions = new FrontendInteractions(); |
| | | } |
| | | } |
| | | |
| | | // Initialize after DOM is ready but without blocking render |
| | | if ('requestIdleCallback' in window) { |
| | | requestIdleCallback(async function() { |
| | | window.auth.subscribe((event) => { |
| | | if (event === 'auth-loaded') { |
| | | if (document.readyState === 'loading') { |
| | | document.addEventListener('DOMContentLoaded', initFrontendInteractions); |
| | | } else { |
| | | initFrontendInteractions(); |
| | | } |
| | | } |
| | | }); |
| | | }); |
| | | } else { |
| | | // Fallback for browsers without requestIdleCallback |
| | | if (document.readyState === 'loading') { |
| | | document.addEventListener('DOMContentLoaded', initFrontendInteractions); |
| | | } else { |
| | | setTimeout(initFrontendInteractions, 1); |
| | | window.jvbInteractions = new UserInteractions(); |
| | | } |
| | | } |
| | | |