// Campaign Page Management Module class CampaignPage { constructor() { this.campaign = null; this.representatives = []; this.userInfo = {}; this.currentStep = 1; this.init(); } init() { // Get campaign slug from URL const pathParts = window.location.pathname.split('/'); this.campaignSlug = pathParts[pathParts.length - 1]; // Set up form handlers document.getElementById('user-info-form').addEventListener('submit', (e) => { this.handleUserInfoSubmit(e); }); // Postal code formatting document.getElementById('user-postal-code').addEventListener('input', (e) => { this.formatPostalCode(e); }); // Load campaign data this.loadCampaign(); } async loadCampaign() { this.showLoading('Loading campaign...'); try { const response = await fetch(`/api/campaigns/${this.campaignSlug}`); const data = await response.json(); if (!data.success) { throw new Error(data.error || 'Failed to load campaign'); } this.campaign = data.campaign; this.renderCampaign(); } catch (error) { this.showError('Failed to load campaign: ' + error.message); } finally { this.hideLoading(); } } renderCampaign() { // Update page title and header document.title = `${this.campaign.title} - Alberta Influence Tool`; document.getElementById('page-title').textContent = `${this.campaign.title} - Alberta Influence Tool`; document.getElementById('campaign-title').textContent = this.campaign.title; document.getElementById('campaign-description').textContent = this.campaign.description; // Show email count if enabled if (this.campaign.show_email_count && this.campaign.emailCount !== null) { document.getElementById('email-count').textContent = this.campaign.emailCount; document.getElementById('campaign-stats').style.display = 'block'; } // Show call to action if (this.campaign.call_to_action) { document.getElementById('call-to-action').innerHTML = `
${this.campaign.call_to_action}
`; document.getElementById('call-to-action').style.display = 'block'; } // Show email preview document.getElementById('preview-subject').textContent = this.campaign.email_subject; document.getElementById('preview-body').textContent = this.campaign.email_body; document.getElementById('email-preview').style.display = 'block'; // Set up email method options this.setupEmailMethodOptions(); // Set initial step this.setStep(1); } setupEmailMethodOptions() { const emailMethodSection = document.getElementById('email-method-selection'); const allowSMTP = this.campaign.allow_smtp_email; const allowMailto = this.campaign.allow_mailto_link; if (!emailMethodSection) { console.warn('Email method selection element not found'); return; } // Configure existing radio buttons instead of replacing HTML const smtpRadio = document.getElementById('method-smtp'); const mailtoRadio = document.getElementById('method-mailto'); if (allowSMTP && allowMailto) { // Both methods allowed - keep default setup smtpRadio.disabled = false; mailtoRadio.disabled = false; smtpRadio.checked = true; } else if (allowSMTP && !allowMailto) { // Only SMTP allowed smtpRadio.disabled = false; mailtoRadio.disabled = true; smtpRadio.checked = true; } else if (!allowSMTP && allowMailto) { // Only mailto allowed smtpRadio.disabled = true; mailtoRadio.disabled = false; mailtoRadio.checked = true; } else { // Neither allowed - hide the section emailMethodSection.style.display = 'none'; } } formatPostalCode(e) { let value = e.target.value.replace(/\s/g, '').toUpperCase(); if (value.length > 3) { value = value.substring(0, 3) + ' ' + value.substring(3, 6); } e.target.value = value; } async handleUserInfoSubmit(e) { e.preventDefault(); const formData = new FormData(e.target); this.userInfo = { postalCode: formData.get('postalCode').replace(/\s/g, '').toUpperCase(), userName: formData.get('userName') || '', userEmail: formData.get('userEmail') || '' }; await this.loadRepresentatives(); } async loadRepresentatives() { this.showLoading('Finding your representatives...'); try { const response = await fetch(`/api/campaigns/${this.campaignSlug}/representatives/${this.userInfo.postalCode}`); const data = await response.json(); if (!data.success) { throw new Error(data.error || 'Failed to load representatives'); } this.representatives = data.representatives; this.renderRepresentatives(); this.setStep(2); // Scroll to representatives section document.getElementById('representatives-section').scrollIntoView({ behavior: 'smooth' }); } catch (error) { this.showError('Failed to load representatives: ' + error.message); } finally { this.hideLoading(); } } renderRepresentatives() { const list = document.getElementById('representatives-list'); if (this.representatives.length === 0) { list.innerHTML = 'No representatives found for your area. Please check your postal code.
'; return; } list.innerHTML = this.representatives.map(rep => `${rep.elected_office || 'Representative'}
${rep.party_name || ''}
${rep.email ? `📧 ${rep.email}
` : ''}No email available
'}