// 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; const emailButton = email ? `` : 'No email available'; 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}

` : ''}
${emailButton} ${profileUrl}
`; } 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 }); }); }); } } // Initialize when DOM is loaded document.addEventListener('DOMContentLoaded', () => { window.representativesDisplay = new RepresentativesDisplay(); });