@@ -28,7 +28,6 @@ const countryList = document.getElementById("countryList");
2828document . addEventListener ( "DOMContentLoaded" , async ( ) => {
2929
3030 try {
31- populationData = await fetchPopulationData ( ) ;
3231 populateYearSelect ( ) ;
3332 renderCountrySelection ( ) ;
3433 await updateCalendar ( ) ;
@@ -60,12 +59,6 @@ const tooltipElement = document.createElement("div");
6059tooltipElement . className = "tooltip" ;
6160document . body . appendChild ( tooltipElement ) ;
6261
63- async function fetchPopulationData ( ) {
64- const res = await fetch ( "population.json" ) ;
65- if ( ! res . ok ) throw new Error ( "Fehler beim Laden der Bevölkerungsdaten" ) ;
66- return await res . json ( ) ;
67- }
68-
6962// ------------------------------------------------------------
7063// Dropdown für Jahr/Zeitraum vorbereiten
7164function populateYearSelect ( ) {
@@ -111,28 +104,37 @@ function renderCountrySelection() {
111104 } ) ;
112105}
113106
107+ async function fetchPopulationData ( ) {
108+ const res = await fetch ( "population.json" ) ;
109+ if ( ! res . ok ) throw new Error ( "Fehler beim Laden der Bevölkerungsdaten" ) ;
110+ populationData = await res . json ( ) ;
111+ }
112+
114113// ------------------------------------------------------------
115114// Hole Ferien- und Feiertagsdaten aus der API
116115async function fetchCountryData ( year , countryCode ) {
117- const urlFeiertage = `${ API_BASE } /PublicHolidays?countryIsoCode=${ countryCode } &validFrom=${ year } -01-01&validTo=${ year } -12-31&languageIsoCode=DE` ;
118- const urlFerien = `${ API_BASE } /SchoolHolidays?countryIsoCode=${ countryCode } &validFrom=${ year } -01-01&validTo=${ year } -12-31&languageIsoCode=DE` ;
119-
120- const [ holidaysRes , schoolRes ] = await Promise . all ( [ fetch ( urlFeiertage ) , fetch ( urlFerien ) ] ) ;
121- if ( ! holidaysRes . ok || ! schoolRes . ok )
122- throw new Error ( `Fehler beim Abrufen der Daten für ${ countryCode } ` ) ;
116+ const requests = [
117+ fetch ( `${ API_BASE } /PublicHolidays?countryIsoCode=${ countryCode } &validFrom=${ year } -01-01&validTo=${ year } -12-31&languageIsoCode=DE` ) ,
118+ fetch ( `${ API_BASE } /SchoolHolidays?countryIsoCode=${ countryCode } &validFrom=${ year } -01-01&validTo=${ year } -12-31&languageIsoCode=DE` )
119+ ] ;
120+
121+ const responses = await Promise . all ( requests ) ;
122+ if ( responses . some ( r => ! r . ok ) ) {
123+ throw new Error ( `Fehler beim Abrufen der Daten für ${ countryCode } für ${ year } ` ) ;
124+ }
125+ const [ holidays , schoolHolidays ] = await Promise . all ( responses . map ( r => r . json ( ) ) ) ;
123126
124- const holidays = await holidaysRes . json ( ) ;
125- const schoolHolidays = await schoolRes . json ( ) ;
126127 if ( ! ( year in cachedData ) ) cachedData [ year ] = { } ;
127128 cachedData [ year ] [ countryCode ] = { holidays, schoolHolidays} ;
128129
129- if ( ! ( countryCode in cachedData . Regions ) ) {
130- const RegionRes = await fetch ( `${ API_BASE } /Subdivisions?countryIsoCode=${ countryCode } &languageIsoCode=DE` ) ;
131- cachedData . Regions [ countryCode ] = await RegionRes . json ( ) ;
132- }
130+ }
133131
132+ async function fetchRegionData ( countryCode ) {
133+ const RegionRes = await fetch ( `${ API_BASE } /Subdivisions?countryIsoCode=${ countryCode } &languageIsoCode=DE` ) ;
134+ cachedData . Regions [ countryCode ] = await RegionRes . json ( ) ;
134135}
135136
137+
136138// ------------------------------------------------------------
137139// Aktualisiere Kalender
138140async function updateCalendar ( ) {
@@ -141,13 +143,15 @@ async function updateCalendar() {
141143 const toDate = new Date ( toStr ) ;
142144
143145 // Lade Daten, falls noch nicht vorhanden
144- for ( year of [ fromDate . getFullYear ( ) , toDate . getFullYear ( ) ] ) {
145- await Promise . all (
146- selectedCountries . map ( async ( c ) => {
147- if ( ! cachedData [ year ] || ! cachedData [ year ] [ c ] ) await fetchCountryData ( year , c ) ;
148- } )
149- ) ;
146+ const fetch = [ ] ;
147+ if ( ! populationData ) fetch . push ( fetchPopulationData ( ) ) ;
148+ for ( let country of selectedCountries ) {
149+ if ( ! cachedData . Regions [ country ] ) fetch . push ( fetchRegionData ( country ) ) ;
150+ for ( let year of [ ...new Set ( [ fromDate . getFullYear ( ) , toDate . getFullYear ( ) ] ) ] ) {
151+ if ( ! cachedData [ year ] || ! cachedData [ year ] [ country ] ) fetch . push ( fetchCountryData ( year , country ) ) ;
152+ }
150153 }
154+ await Promise . all ( fetch ) ;
151155
152156 // Daten aggregieren
153157 const dayStats = calculateDayStatistics ( fromDate , toDate ) ;
0 commit comments