// User Dashboard JavaScript class UserDashboard { constructor() { this.user = null; this.campaigns = []; this.analytics = {}; this.authManager = null; this.currentCampaign = null; // Track campaign being edited } async init() { // Check authentication first if (typeof authManager !== 'undefined') { this.authManager = authManager; const isAuth = await this.authManager.checkSession(); if (!isAuth) { window.location.href = '/login.html'; return; } this.user = this.authManager.user; this.setupUserInterface(); } else { // Fallback if authManager not loaded window.location.href = '/login.html'; return; } this.setupEventListeners(); this.loadUserCampaigns(); this.loadAnalytics(); } setupUserInterface() { if (!this.user) return; // Update user info display const userNameEl = document.getElementById('user-name'); const userEmailEl = document.getElementById('user-email'); const userRoleBadge = document.getElementById('user-role-badge'); if (userNameEl) userNameEl.textContent = this.user.name || this.user.email; if (userEmailEl) userEmailEl.textContent = this.user.email; if (userRoleBadge) { const userType = this.user.userType || 'user'; userRoleBadge.className = `user-badge ${userType}`; userRoleBadge.textContent = userType === 'admin' ? 'Administrator' : userType === 'temp' ? 'Temporary User' : 'User'; } // Update account form this.populateAccountForm(); // Show admin link if user is admin const createBtn = document.getElementById('create-campaign-btn'); if (createBtn && this.user.isAdmin) { createBtn.textContent = 'Admin Panel'; } else if (createBtn) { createBtn.style.display = 'none'; } } populateAccountForm() { if (!this.user) return; const nameInput = document.getElementById('account-name'); const emailInput = document.getElementById('account-email'); const phoneInput = document.getElementById('account-phone'); const roleInput = document.getElementById('account-role'); const expiresInput = document.getElementById('account-expires'); const expirationGroup = document.getElementById('account-expiration'); if (nameInput) nameInput.value = this.user.name || ''; if (emailInput) emailInput.value = this.user.email || ''; if (phoneInput) phoneInput.value = this.user.phone || ''; if (roleInput) { const roleText = this.user.isAdmin ? 'Administrator' : this.user.userType === 'temp' ? 'Temporary User' : 'Standard User'; roleInput.value = roleText; } // Show expiration info for temp users if (this.user.userType === 'temp' && expiresInput && expirationGroup) { if (this.user.expiresAt) { expiresInput.value = new Date(this.user.expiresAt).toLocaleDateString(); expirationGroup.style.display = 'block'; } } } setupEventListeners() { // Tab navigation document.querySelectorAll('.nav-btn').forEach(btn => { btn.addEventListener('click', (e) => { const tab = e.target.dataset.tab; this.switchTab(tab); }); }); // Logout button const logoutBtn = document.getElementById('logout-btn'); if (logoutBtn) { logoutBtn.addEventListener('click', () => { this.authManager.logout(); }); } // Form submissions const createForm = document.getElementById('create-campaign-form'); if (createForm) { createForm.addEventListener('submit', (e) => { this.handleCreateCampaign(e); }); } const editForm = document.getElementById('edit-campaign-form'); if (editForm) { editForm.addEventListener('submit', (e) => { this.handleUpdateCampaign(e); }); } // Campaign actions using event delegation document.addEventListener('click', (e) => { if (e.target.matches('[data-action="view-campaign"]')) { this.viewCampaign(e.target.dataset.campaignSlug); } if (e.target.matches('[data-action="edit-campaign"]')) { this.editCampaign(e.target.dataset.campaignId); } if (e.target.matches('[data-action="view-analytics"]')) { this.viewCampaignAnalytics(e.target.dataset.campaignId); } if (e.target.matches('[data-action="cancel-create"]')) { this.switchTab('campaigns'); } if (e.target.matches('[data-action="cancel-edit"]')) { this.switchTab('campaigns'); } if (e.target.matches('[data-action="go-to-create"]')) { this.switchTab('create'); } }); // Setup campaign selector dropdowns this.setupCampaignSelectors(); } setupCampaignSelectors() { // Setup Create Campaign Selector const createSelector = document.getElementById('create-campaign-selector'); const createDropdown = document.getElementById('create-dropdown-menu'); if (createSelector && createDropdown) { this.setupDropdown(createSelector, createDropdown, 'create'); } // Setup Edit Campaign Selector const editSelector = document.getElementById('edit-campaign-selector'); const editDropdown = document.getElementById('edit-dropdown-menu'); if (editSelector && editDropdown) { this.setupDropdown(editSelector, editDropdown, 'edit'); } } setupDropdown(input, dropdown, type) { // Show dropdown on focus input.addEventListener('focus', () => { this.populateDropdown(dropdown, type); dropdown.classList.add('show'); }); // Hide dropdown when clicking outside document.addEventListener('click', (e) => { if (!input.contains(e.target) && !dropdown.contains(e.target)) { dropdown.classList.remove('show'); } }); // Filter campaigns on input input.addEventListener('input', () => { this.filterDropdown(input, dropdown, type); }); // Handle dropdown item selection dropdown.addEventListener('click', (e) => { if (e.target.classList.contains('dropdown-item')) { const campaignId = e.target.dataset.campaignId; const campaignTitle = e.target.textContent; input.value = campaignTitle; dropdown.classList.remove('show'); if (type === 'create' && campaignId !== 'new') { this.populateCreateFormFromCampaign(campaignId); } else if (type === 'edit' && campaignId) { this.loadCampaignForEdit(campaignId); } else if (type === 'create' && campaignId === 'new') { this.clearCreateForm(); } } }); } populateDropdown(dropdown, type) { dropdown.innerHTML = ''; if (type === 'create') { dropdown.innerHTML = '
You haven't created any campaigns yet.