-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
84 lines (75 loc) · 2.56 KB
/
index.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
const searchForm = document.querySelector('.search-location');
const cityValue = document.querySelector('.search-location input');
const cityName = document.querySelector('.city-name p');
const cardBody = document.querySelector('.card-body');
const timeImage = document.querySelector('.card-top img');
const cardInfo = document.querySelector('.back-card');
const splitOutCelcius = (kelvin) => {
const celcius = Math.round(kelvin - 273.15);
return celcius;
};
const isDayTime = (icon) => {
if (icon.includes('d')) {
return true;
} else {
return false;
}
};
const updateWeatherApp = (city) => {
const imageName = city.weather[0].icon;
const iconSrc = `//openweathermap.org/img/wn/${imageName}@2x.png`;
cityName.textContent = city.name;
cardBody.innerHTML = `
<div class="card-mid row">
<div class="col-8 text-center temp">
<span>${splitOutCelcius(city.main.temp)}°C</span>
</div>
<div class="col-4 condition-temp">
<p class="condition">${city.weather[0].description}</p>
<p class="high">${splitOutCelcius(city.main.temp_max)}°C</p>
<p class="low">${splitOutCelcius(city.main.temp_min)}°C</p>
</div>
<div class="icon-container card shadow mx-auto">
<img src="${iconSrc}" alt="icon" />
</div>
<div class="card-bottom px-5 py-4 row">
<div class="col text-center">
<p>${splitOutCelcius(city.main.feels_like)}°C</p>
<span> Feels Like </span>
</div>
<div class="col text-center">
<p>${city.main.humidity}</p>
<span> Humidity </span>
</div>
</div>
</div>
`;
if (isDayTime(imageName)) {
timeImage.setAttribute('src', 'img/day_image.svg');
if (cityName.classList.contains('text-white')) {
cityName.classList.remove('text-white');
} else {
cityName.classList.add('text-black');
}
} else {
timeImage.setAttribute('src', 'img/night_image.svg');
if (cityName.classList.contains('text-black')) {
cityName.classList.remove('text-black');
} else {
cityName.classList.add('text-white');
}
cityName.classList.add('text-white');
}
cardInfo.classList.remove('d-none');
};
// An event listener for the form
searchForm.addEventListener('submit', e => {
e.preventDefault();
const citySearched = cityValue.value.trim();
searchForm.reset();
requestCity(citySearched)
.then((data) => {
updateWeatherApp(data);
})
.catch((error) => { console.log(error); });
});