|
| 1 | +const input = document.getElementById('input'); |
| 2 | +const btn = document.getElementById('btn'); |
| 3 | +const apiKey = 'e3a46268fdc2475cb63214712240202'; |
| 4 | +const cityName = document.getElementById('city-name'); |
| 5 | +const dateTime = document.getElementById('date-time'); |
| 6 | +const condition2 = document.getElementById('condition2'); |
| 7 | +const temp = document.getElementById('temp'); |
| 8 | +const humidity = document.getElementById('humidity'); |
| 9 | +const country = document.getElementById('country'); |
| 10 | +const locat = document.getElementById('getlocation'); |
| 11 | +const cities = document.getElementsByClassName('city'); |
| 12 | +const icon = document.getElementById('icon'); |
| 13 | +const body = document.querySelector('.weather-app'); |
| 14 | +const fetchData = async (url) => { |
| 15 | + try { |
| 16 | + const data = await fetch(url); |
| 17 | + if (!data.ok) { |
| 18 | + throw new Error(data.statusText); |
| 19 | + } |
| 20 | + return data.json(); |
| 21 | + } catch (error) { |
| 22 | + console.log(error); |
| 23 | + throw error; |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +const updateWeatherInfo = (result) => { |
| 28 | + const { error, location, current } = result; |
| 29 | + if (error) { |
| 30 | + cityName.innerText = `Error: ${error.message}`; |
| 31 | + [country, dateTime, temp, humidity, condition2, icon].forEach((elem) => { |
| 32 | + 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; |
| 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 += 1) { |
| 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 | +}; |
| 89 | +const getlocation = async (lat, long) => fetchData( |
| 90 | + `http://api.weatherapi.com/v1/current.json?key=${apiKey}&q=${lat},${long}&aqi=no`, |
| 91 | +); |
| 92 | + |
| 93 | +const gotlocation = async (position) => { |
| 94 | + try { |
| 95 | + const result = await getlocation( |
| 96 | + position.coords.latitude, |
| 97 | + position.coords.longitude, |
| 98 | + ); |
| 99 | + console.log(result); |
| 100 | + updateWeatherInfo(result); |
| 101 | + } catch (error) { |
| 102 | + cityName.innerText = 'Error fetching weather based on location'; |
| 103 | + } |
| 104 | +}; |
| 105 | +const failedlocation = () => console.log('failed to locate location'); |
| 106 | + |
| 107 | +btn.addEventListener('click', async (e) => { |
| 108 | + try { |
| 109 | + if (input.value.length === 0) { |
| 110 | + alert('Please type a city name'); |
| 111 | + } else { |
| 112 | + const { value } = input; |
| 113 | + const result = await getData(value); |
| 114 | + console.log(result); |
| 115 | + updateWeatherInfo(result); |
| 116 | + console.log(result); |
| 117 | + } |
| 118 | + } catch (error) { |
| 119 | + cityName.innerText = 'Error to fetch weather'; |
| 120 | + } |
| 121 | + e.preventDefault(); |
| 122 | +}); |
| 123 | + |
| 124 | +locat.addEventListener('click', () => navigator.geolocation.getCurrentPosition(gotlocation, failedlocation)); |
| 125 | +const citiesArray = [...cities]; |
| 126 | +citiesArray.forEach((element) => { |
| 127 | + element.addEventListener('click', async () => { |
| 128 | + const cityName = element.innerText; |
| 129 | + const result = await getData(cityName); |
| 130 | + updateWeatherInfo(result); |
| 131 | + }); |
| 132 | +}); |
| 133 | + |
| 134 | +window.addEventListener('load', async () => { |
| 135 | + navigator.geolocation.getCurrentPosition(gotlocation, failedlocation); |
| 136 | +}); |
0 commit comments