@@ -3,15 +3,14 @@ const btn = document.getElementById("btn");
3
3
const apiKey = "e3a46268fdc2475cb63214712240202" ;
4
4
const cityName = document . getElementById ( "city-name" ) ;
5
5
const dateTime = document . getElementById ( "date-time" ) ;
6
- const condition = document . getElementById ( "condition" ) ;
7
6
const condition2 = document . getElementById ( "condition2" ) ;
8
7
const temp = document . getElementById ( "temp" ) ;
9
8
const humidity = document . getElementById ( "humidity" ) ;
10
9
const country = document . getElementById ( "country" ) ;
11
10
const locat = document . getElementById ( "getlocation" ) ;
12
11
const cities = document . getElementsByClassName ( "city" ) ;
13
12
const icon = document . getElementById ( "icon" ) ;
14
-
13
+ const body = document . querySelector ( ".weather-app" ) ;
15
14
const fetchData = async ( url ) => {
16
15
try {
17
16
const data = await fetch ( url ) ;
@@ -24,21 +23,69 @@ const fetchData = async (url) => {
24
23
throw error ;
25
24
}
26
25
} ;
26
+
27
27
const updateWeatherInfo = ( result ) => {
28
- cityName . innerText = `${ result . location . name } ` ;
29
- country . innerText = `${ result . location . country } ` ;
30
- dateTime . innerText = `${ result . location . localtime } ` ;
31
- temp . innerText = `${ result . current . temp_c } °C` ;
32
- humidity . innerText = `${ result . current . humidity } %` ;
33
- condition . innerText = `${ result . current . condition . text } ` ;
34
- condition2 . innerText = `${ result . current . condition . text } ` ;
35
- icon . src = `${ result . current . condition . icon } ` ; // Set the src attribute of the img tag with id "icon"
36
- } ;
37
- const getData = async ( cityName ) =>
38
- fetchData (
39
- `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no`
40
- ) ;
28
+ const { error, location, current } = result ;
29
+ if ( error ) {
30
+ cityName . innerText = "Error: " + error . message ;
31
+ [ country , dateTime , temp , humidity , condition2 , icon ] . forEach (
32
+ ( elem ) => ( elem . innerText = "" )
33
+ ) ;
34
+ icon . src = "" ;
35
+ } else {
36
+ const { name, country, localtime } = location ;
37
+ cityName . innerText = name ;
38
+ country . innerText = country ;
39
+ dateTime . innerText = localtime ;
40
+ temp . innerText = `${ current . temp_c } °C` ;
41
+ humidity . innerText = `${ current . humidity } %` ;
42
+ condition2 . innerText = current . condition . text ;
43
+ icon . src = current . condition . icon ;
41
44
45
+ const isDay = current . is_day == 1 ? "day" : "night" ;
46
+ const codes = [
47
+ [ 1000 , 10000 , 10001 , 1100 , 11001 , 11000 , 51190 , 60030 ] , // clear
48
+ [
49
+ 1101 , 11011 , 11010 , 11021 , 11020 , 1102 , 1001 , 10010 , 10011 , 1003 , 1150 ,
50
+ 1153 , 1168 , 1147 , 1135 , 1087 , 1003 , 1006 , 1207 , 1009 ,
51
+ ] , // cloudy
52
+ [
53
+ 1261 , 1273 , 1276 , 1274 , 1246 , 1243 , 1240 , 1201 , 1204 , 1198 , 1192 , 1195 ,
54
+ 1189 , 1186 , 1183 , 1180 , 1186 ,
55
+ ] , // rainy
56
+ [
57
+ 1030 , 1066 , 1168 , 1171 , 1210 , 1213 , 1216 , 1219 , 1222 , 1225 , 1237 , 1255 ,
58
+ 1258 , 1279 , 1282 ,
59
+ ] , // snowy
60
+ ] ;
61
+ const imageUrls = [
62
+ `./images/${ isDay } /clear.jpg` ,
63
+ `./images/${ isDay } /cloudy.jpg` ,
64
+ `./images/${ isDay } /rainy.jpg` ,
65
+ `./images/${ isDay } /snowy.jpg` ,
66
+ ] ;
67
+
68
+ for ( let i = 0 ; i < codes . length ; i ++ ) {
69
+ if ( codes [ i ] . includes ( current . condition . code ) ) {
70
+ body . style . backgroundImage = `url('${ imageUrls [ i ] } ')` ;
71
+ console . log ( imageUrls [ i ] ) ;
72
+ break ;
73
+ }
74
+ }
75
+ }
76
+ } ;
77
+ const getData = async ( cityName ) => {
78
+ try {
79
+ const result = await fetchData (
80
+ `http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ cityName } &aqi=no`
81
+ ) ;
82
+ return result ;
83
+ } catch ( error ) {
84
+ return {
85
+ error : { message : "Failed to fetch data. Please try again later." } ,
86
+ } ;
87
+ }
88
+ } ;
42
89
const getlocation = async ( lat , long ) =>
43
90
fetchData (
44
91
`http://api.weatherapi.com/v1/current.json?key=${ apiKey } &q=${ lat } ,${ long } &aqi=no`
@@ -50,22 +97,29 @@ const gotlocation = async (position) => {
50
97
position . coords . latitude ,
51
98
position . coords . longitude
52
99
) ;
100
+ console . log ( result ) ;
53
101
updateWeatherInfo ( result ) ;
54
102
} catch ( error ) {
55
103
cityName . innerText = "Error fetching weather based on location" ;
56
104
}
57
105
} ;
58
106
const failedlocation = ( ) => console . log ( "failed to locate location" ) ;
59
107
60
- btn . addEventListener ( "click" , async ( ) => {
108
+ btn . addEventListener ( "click" , async ( e ) => {
61
109
try {
62
- const { value } = input ;
63
- const result = await getData ( value ) ;
64
- updateWeatherInfo ( result ) ;
65
- console . log ( result ) ;
110
+ if ( input . value . length === 0 ) {
111
+ alert ( "Please type a city name" ) ;
112
+ } else {
113
+ const { value } = input ;
114
+ const result = await getData ( value ) ;
115
+ console . log ( result ) ;
116
+ updateWeatherInfo ( result ) ;
117
+ console . log ( result ) ;
118
+ }
66
119
} catch ( error ) {
67
120
cityName . innerText = "Error to fetch weather" ;
68
121
}
122
+ e . preventDefault ( ) ;
69
123
} ) ;
70
124
71
125
locat . addEventListener ( "click" , ( ) =>
@@ -74,8 +128,12 @@ locat.addEventListener("click", () =>
74
128
const citiesArray = [ ...cities ] ;
75
129
citiesArray . forEach ( ( element ) => {
76
130
element . addEventListener ( "click" , async ( ) => {
77
- const cityName = element . innerText ; // Extract city name from the clicked element
78
- const result = await getData ( cityName ) ; // Pass the city name to getData
131
+ const cityName = element . innerText ;
132
+ const result = await getData ( cityName ) ;
79
133
updateWeatherInfo ( result ) ;
80
134
} ) ;
81
135
} ) ;
136
+
137
+ window . addEventListener ( "load" , async ( ) => {
138
+ navigator . geolocation . getCurrentPosition ( gotlocation , failedlocation ) ;
139
+ } ) ;
0 commit comments