iCal Export

Click the button to download a .ics file for the season schedule. Use ?team=TEAM_ID to export one team.

import { initializeApp } from 'https://www.gstatic.com/firebasejs/10.12.0/firebase-app.js'; import { getFirestore, getDocs } from 'https://www.gstatic.com/firebasejs/10.12.0/firebase-firestore.js'; import { firebaseConfig } from '/firebase-config.js'; import { colRef } from '/db-helpers.js'; const app=initializeApp(firebaseConfig); const db=getFirestore(app); const team=new URL(location.href).searchParams.get('team'); const s=document.getElementById('status'); document.getElementById('dl').onclick=async ()=>{ s.textContent='Building…'; const snap=await getDocs(colRef(db,'games')); const games=snap.docs.map(d=>({id:d.id, ...d.data()})) .filter(g=> !team || g.home===team || g.away===team) .sort((a,b)=>(a.start_ts||0)-(b.start_ts||0)); const lines=[ 'BEGIN:VCALENDAR','VERSION:2.0','PRODID:-//PGBHL//Schedule//EN' ]; for (const g of games){ const dt = g.start_ts ? new Date(g.start_ts) : null; const end= g.end_ts ? new Date(g.end_ts) : (dt? new Date(dt.getTime()+90*60000):null); const fmt = (d)=> d? d.toISOString().replace(/[-:]/g,'').replace(/\.\d{3}Z/,'Z'): ''; lines.push('BEGIN:VEVENT'); lines.push(`UID:${g.id}@pgbhl`); if (dt) lines.push(`DTSTART:${fmt(dt)}`); if (end) lines.push(`DTEND:${fmt(end)}`); lines.push(`SUMMARY:${g.home_name||g.home} vs ${g.away_name||g.away}`); if (g.venue) lines.push(`LOCATION:${g.venue}`); lines.push('END:VEVENT'); } lines.push('END:VCALENDAR'); const blob=new Blob([lines.join('\r\n')], {type:'text/calendar'}); const a=document.createElement('a'); a.href=URL.createObjectURL(blob); a.download = team ? `pgbhl-${team}-schedule.ics` : 'pgbhl-schedule.ics'; a.click(); s.textContent='Downloaded.'; };