-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
132 lines (124 loc) · 4.72 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const temperatureScaleToggler = document.getElementById(
'temperature-scale-toggler'
);
const temperatureTogglerInfo = document.getElementById(
'temperature-toggler-info'
);
const searchStatus = document.getElementById('search-status');
const searchButton = document.getElementById('search-button');
const searchBar = document.getElementById('search-bar');
const locationName = document.getElementById('location-name');
const locationRegion = document.getElementById('location-region');
const locationCountry = document.getElementById('location-country');
const condition = document.getElementById('condition');
const conditionIcon = document.getElementById('condition-icon');
const temperature = document.getElementById('temperature');
const humidity = document.getElementById('humidity');
const feelsLike = document.getElementById('feels-like');
const windSpeed = document.getElementById('wind-speed');
const locationTitle = document.getElementById('location-title');
const humidityTitle = document.getElementById('humidity-title');
const windTitle = document.getElementById('wind-title');
const feelsLikeTitle = document.getElementById('feels-like-title');
const conditionTitle = document.getElementById('condition-title');
let temperatureScale = 'c';
let weatherData;
let temperatureSource = 'weatherData.current.temp_c';
let feelsLikeSource = 'weatherData.current.feelslike_c';
let wasDataReceived = false;
let oneSuccessfulSearch = false;
let locationNameTempValue;
let locationRegionTempValue;
let locationCountryTempValue;
let conditionTempValue;
let conditionIconTempValue;
let temperatureTempValueC;
let temperatureTempValueF;
let humidityTempValue;
let feelsLikeTempValueC;
let feelsLikeTempValueF;
let windKPHTempValue;
let windMPHTempValue;
function toggleTemperatureScale() {
if (temperatureScale === 'c') {
temperatureScaleToggler.style.justifyContent = 'flex-end';
temperatureTogglerInfo.textContent = 'F°';
temperatureScale = 'f';
temperatureSource = 'temperatureTempValueF';
feelsLikeSource = 'feelsLikeTempValueF';
} else {
temperatureScaleToggler.style.justifyContent = 'flex-start';
temperatureTogglerInfo.textContent = 'C°';
temperatureScale = 'c';
temperatureSource = 'temperatureTempValueC';
feelsLikeSource = 'feelsLikeTempValueC';
}
setWeather();
}
async function getWeather() {
if (!searchBar.value) {
return null;
}
try {
const response = await fetch(
`https://api.weatherapi.com/v1/current.json?key=13e369673c374816a78184619241302&q=${searchBar.value}`
);
weatherData = await response.json();
wasDataReceived = true;
storeWeatherValues();
cleanUpFields();
oneSuccessfulSearch = true;
setWeather();
conditionIcon.style.display = 'block';
searchStatus.textContent = null;
} catch (e) {
wasDataReceived = false;
searchStatus.textContent = `Sorry, we couldn't find the weather information for '${searchBar.value}'`;
}
}
function storeWeatherValues() {
locationNameTempValue = weatherData.location.name;
locationRegionTempValue = weatherData.location.region;
locationCountryTempValue = weatherData.location.country;
conditionTempValue = weatherData.current.condition.text;
conditionIconTempValue = `https://${weatherData.current.condition.icon}`;
temperatureTempValueC = weatherData.current.temp_c;
temperatureTempValueF = weatherData.current.temp_f;
humidityTempValue = weatherData.current.humidity;
feelsLikeTempValueC = weatherData.current.feelslike_c;
feelsLikeTempValueF = weatherData.current.feelslike_f;
windKPHTempValue = weatherData.current.wind_kph;
windMPHTempValue = weatherData.current.wind_mph;
}
function setWeather() {
if (oneSuccessfulSearch == true) {
locationName.innerText = `${locationNameTempValue}, ${locationRegionTempValue}, ${locationCountryTempValue}`;
condition.innerText = conditionTempValue;
conditionIcon.src = conditionIconTempValue;
temperature.innerText =
eval(temperatureSource) + ` ${temperatureScale.toUpperCase()}°`;
humidity.innerText = humidityTempValue;
feelsLike.innerText =
eval(feelsLikeSource) + ` ${temperatureScale.toUpperCase()}°`;
windTitle.innerText = 'Wind:';
windSpeed.innerText = `${windKPHTempValue} kph / ${windMPHTempValue} mph`;
locationTitle.innerText = 'Location:';
humidityTitle.innerText = 'Humidity:';
windTitle.innerText = 'Wind:';
feelsLikeTitle.innerText = 'Feels like:';
conditionTitle.innerText = 'Condition:';
} else {
/* Do nothing */
}
}
function handleSearchEnter(e) {
if (e.keyCode === 13) {
getWeather();
}
}
function cleanUpFields() {
searchBar.value = '';
}
temperatureScaleToggler.onclick = toggleTemperatureScale;
searchButton.onclick = getWeather;
searchBar.onkeyup = handleSearchEnter;