1
+ "use strict" ;
1
2
var __awaiter = ( this && this . __awaiter ) || function ( thisArg , _arguments , P , generator ) {
2
3
function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
3
4
return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
@@ -7,21 +8,19 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
7
8
step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
8
9
} ) ;
9
10
} ;
10
- // Define global variables
11
11
let LAT ;
12
12
let LON ;
13
13
let APIKEY ;
14
14
let API_BASE_URL ;
15
15
let apiWeatherURL ;
16
16
let apiForecastURL ;
17
17
const ONE_DAY = 24 * 60 * 60 * 1000 ;
18
- // **Initialize Configuration from API**
19
18
function initConfig ( ) {
20
19
return __awaiter ( this , void 0 , void 0 , function * ( ) {
21
20
try {
22
21
const response = yield fetch ( '/api/config' ) ;
23
22
if ( ! response . ok ) {
24
- const errorText = yield response . text ( ) ; // Get error details from server
23
+ const errorText = yield response . text ( ) ;
25
24
throw new Error ( `Failed to load configuration: ${ response . status } - ${ errorText } ` ) ;
26
25
}
27
26
const config = yield response . json ( ) ;
@@ -31,40 +30,40 @@ function initConfig() {
31
30
API_BASE_URL = config . API_BASE_URL ;
32
31
apiWeatherURL = `${ API_BASE_URL } /weather?lat=${ LAT } &lon=${ LON } &appid=${ APIKEY } &units=imperial` ;
33
32
apiForecastURL = `${ API_BASE_URL } /forecast?lat=${ LAT } &lon=${ LON } &appid=${ APIKEY } &units=imperial` ;
34
- yield Promise . all ( [ fetchWeather ( ) , fetchForecast ( ) ] ) ; // Fetch after config
33
+ // Fetch both weather and forecast concurrently *after* config is loaded
34
+ yield Promise . all ( [ fetchWeather ( ) , fetchForecast ( ) ] ) ;
35
35
}
36
36
catch ( error ) {
37
37
console . error ( 'Error loading configuration:' , error ) ;
38
- // Handle the error gracefully, maybe display a message to the user
39
- const errorElement = document . getElementById ( 'error-message' ) ; // Example error display
38
+ const errorElement = document . getElementById ( 'error-message' ) ;
40
39
if ( errorElement ) {
41
40
errorElement . textContent = "Error loading weather data. Please try again later." ;
42
41
}
43
42
}
44
43
} ) ;
45
44
}
46
- // **Fetch current weather data**
47
45
function fetchWeather ( ) {
48
46
return __awaiter ( this , void 0 , void 0 , function * ( ) {
49
47
try {
50
48
const response = yield fetch ( apiWeatherURL ) ;
51
- if ( ! response . ok )
49
+ if ( ! response . ok ) {
52
50
throw new Error ( yield response . text ( ) ) ;
53
- const data = yield response . json ( ) ;
51
+ }
52
+ const data = yield response . json ( ) ; // Type the response
54
53
displayWeather ( data ) ;
55
54
}
56
55
catch ( error ) {
57
56
console . error ( "Error fetching weather:" , error ) ;
58
57
}
59
58
} ) ;
60
59
}
61
- // **Fetch forecast data**
62
60
function fetchForecast ( ) {
63
61
return __awaiter ( this , void 0 , void 0 , function * ( ) {
64
62
try {
65
63
const response = yield fetch ( apiForecastURL ) ;
66
- if ( ! response . ok )
64
+ if ( ! response . ok ) {
67
65
throw new Error ( yield response . text ( ) ) ;
66
+ }
68
67
const data = yield response . json ( ) ;
69
68
displayForecast ( data . list ) ;
70
69
}
@@ -73,7 +72,6 @@ function fetchForecast() {
73
72
}
74
73
} ) ;
75
74
}
76
- // **Display current weather**
77
75
function displayWeather ( data ) {
78
76
const iconsrc = `https://openweathermap.org/img/wn/${ data . weather [ 0 ] . icon } .png` ;
79
77
const desc = data . weather [ 0 ] . description ;
@@ -93,7 +91,6 @@ function displayWeather(data) {
93
91
console . error ( "One or more weather elements not found in the DOM." ) ;
94
92
}
95
93
}
96
- // **Display 3-day forecast**
97
94
function displayForecast ( forecasts ) {
98
95
let dates = [ ] ;
99
96
let mydate = new Date ( ) ;
@@ -110,7 +107,7 @@ function displayForecast(forecasts) {
110
107
. map ( x => x . weather [ 0 ] . description ) [ 0 ] ) ;
111
108
const weatherElt = document . querySelector ( ".forecast" ) ;
112
109
if ( weatherElt ) {
113
- weatherElt . innerHTML = '' ; // Clear existing forecast
110
+ weatherElt . innerHTML = '' ;
114
111
for ( let i = 0 ; i < 3 ; i ++ ) {
115
112
let newSection = document . createElement ( "div" ) ;
116
113
newSection . innerHTML = `
@@ -125,9 +122,7 @@ function displayForecast(forecasts) {
125
122
console . error ( "Forecast container element not found in the DOM." ) ;
126
123
}
127
124
}
128
- export function getTheForecast ( ) {
129
- return __awaiter ( this , void 0 , void 0 , function * ( ) {
130
- yield initConfig ( ) ; // Call initConfig to start the process
131
- } ) ;
132
- }
133
- document . addEventListener ( 'DOMContentLoaded' , getTheForecast ) ;
125
+ // Initialize and fetch data when the DOM is ready
126
+ document . addEventListener ( 'DOMContentLoaded' , ( ) => __awaiter ( void 0 , void 0 , void 0 , function * ( ) {
127
+ yield initConfig ( ) ; // Call initConfig here
128
+ } ) ) ;
0 commit comments