// Representatives Display Module class RepresentativesDisplay { constructor() { this.container = document.getElementById('representatives-container'); } displayRepresentatives(representatives) { if (!representatives || representatives.length === 0) { this.container.innerHTML = `

No Representatives Found

No representatives were found for this postal code. This might be due to:

Please try again later or verify your postal code.

`; return; } // Group representatives by level/type const grouped = this.groupRepresentatives(representatives); let html = ''; // Order of importance for display const displayOrder = [ 'Federal', 'Provincial', 'Municipal', 'School Board', 'Other' ]; displayOrder.forEach(level => { if (grouped[level] && grouped[level].length > 0) { html += this.renderRepresentativeCategory(level, grouped[level]); } }); this.container.innerHTML = html; this.attachEventListeners(); } groupRepresentatives(representatives) { const groups = { 'Federal': [], 'Provincial': [], 'Municipal': [], 'School Board': [], 'Other': [] }; representatives.forEach(rep => { const setName = rep.representative_set_name || ''; const office = rep.elected_office || ''; if (setName.toLowerCase().includes('house of commons') || setName.toLowerCase().includes('federal') || office.toLowerCase().includes('member of parliament') || office.toLowerCase().includes('mp')) { groups['Federal'].push(rep); } else if (setName.toLowerCase().includes('provincial') || setName.toLowerCase().includes('legislative assembly') || setName.toLowerCase().includes('mla') || office.toLowerCase().includes('mla')) { groups['Provincial'].push(rep); } else if (setName.toLowerCase().includes('municipal') || setName.toLowerCase().includes('city council') || setName.toLowerCase().includes('mayor') || office.toLowerCase().includes('councillor') || office.toLowerCase().includes('mayor')) { groups['Municipal'].push(rep); } else if (setName.toLowerCase().includes('school') || office.toLowerCase().includes('school') || office.toLowerCase().includes('trustee')) { groups['School Board'].push(rep); } else { groups['Other'].push(rep); } }); return groups; } renderRepresentativeCategory(categoryName, representatives) { const cards = representatives.map(rep => this.renderRepresentativeCard(rep)).join(''); return `

${categoryName} Representatives

${cards}
`; } renderRepresentativeCard(rep) { const name = rep.name || 'Name not available'; const email = rep.email || null; const office = rep.elected_office || 'Office not specified'; const district = rep.district_name || 'District not specified'; const party = rep.party_name || 'Party not specified'; const photoUrl = rep.photo_url || null; // Extract phone numbers from offices array const phoneNumbers = this.extractPhoneNumbers(rep.offices || []); const primaryPhone = phoneNumbers.length > 0 ? phoneNumbers[0] : null; const emailButton = email ? `` : 'No email available'; // Add call button if phone number is available const callButton = primaryPhone ? `` : ''; const profileUrl = rep.url ? `View Profile` : ''; // Generate initials for fallback const initials = name.split(' ') .map(word => word.charAt(0)) .join('') .toUpperCase() .slice(0, 2); const photoElement = photoUrl ? `
${name}
` : `
${initials}
`; return `
${photoElement}

${name}

Office: ${office}

District: ${district}

${party !== 'Party not specified' ? `

Party: ${party}

` : ''} ${email ? `

Email: ${email}

` : ''} ${primaryPhone ? `

Phone: ${primaryPhone.number} ${primaryPhone.type ? `(${primaryPhone.type})` : ''}

` : ''}
${emailButton} ${callButton} ${profileUrl}
`; } extractPhoneNumbers(offices) { const phoneNumbers = []; if (Array.isArray(offices)) { offices.forEach(office => { if (office.tel) { phoneNumbers.push({ number: office.tel, type: office.type || 'office' }); } }); } return phoneNumbers; } attachEventListeners() { // Add event listeners for compose email buttons const composeButtons = this.container.querySelectorAll('.compose-email'); composeButtons.forEach(button => { button.addEventListener('click', (e) => { const email = e.target.dataset.email; const name = e.target.dataset.name; const office = e.target.dataset.office; const district = e.target.dataset.district; window.emailComposer.openModal({ email, name, office, district }); }); }); // Add event listeners for call buttons const callButtons = this.container.querySelectorAll('.call-representative'); callButtons.forEach(button => { button.addEventListener('click', (e) => { const phone = e.target.dataset.phone; const name = e.target.dataset.name; const office = e.target.dataset.office; const officeType = e.target.dataset.officeType; this.handleCallClick(phone, name, office, officeType); }); }); } handleCallClick(phone, name, office, officeType) { // Clean the phone number for tel: link (remove spaces, dashes, parentheses) const cleanPhone = phone.replace(/[\s\-\(\)]/g, ''); // Create tel: link const telLink = `tel:${cleanPhone}`; // Show confirmation dialog with formatted information const officeInfo = officeType ? ` (${officeType} office)` : ''; const message = `Call ${name}${officeInfo}?\n\nPhone: ${phone}`; if (confirm(message)) { // Attempt to initiate the call window.location.href = telLink; } } } // Initialize when DOM is loaded document.addEventListener('DOMContentLoaded', () => { window.representativesDisplay = new RepresentativesDisplay(); });