-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
83 lines (73 loc) · 2.87 KB
/
Copy pathindex.js
File metadata and controls
83 lines (73 loc) · 2.87 KB
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
const searchform = document.querySelector('.search-location');
const cityValue = document.querySelector('.search-location input');
const cityName = document.querySelector('.city-name p');
const timeImg = document.querySelector('.card-top img');
const cardInfo = document.querySelector('.back-card');
const cardBody = document.querySelector('.card-body');
const tellCelsius = (kelvin) => {
celsius = Math.round(kelvin - 273);
return celsius;
}
const isDay = (icon) => {
if (icon.includes('d')) {
return true;
} else {
return false;
}
}
updateApp = (city) => {
console.log(city);
cityName.textContent = city.name;
const imgName = city.weather[0].icon;
const iconLink = " https://openweathermap.org/img/wn/" + imgName + "@2x.png";
cardBody.innerHTML = `
<div class="card-body">
<div class="card-mid row ">
<div class="col-8 text-center temp ">
<span>${tellCelsius(city.main.temp)}°C</span>
</div>
<div class="col-4 condition-temp ">
<p class="condition ">${city.weather[0].description}</p>
<p class="high ">${tellCelsius(city.main.temp_max)}°C</p>
<p class="low ">${tellCelsius(city.main.temp_min)}°C</p>
</div>
<div class="icon-container card shadow mx-auto ">
<img src="${iconLink}" alt=" " />
</div>
<div class="card-bottom px-5 py-4 row ">
<div class="col text-center ">
<p>${tellCelsius(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>
`;
if (isDay(imgName)) {
timeImg.setAttribute('src', 'img/day_image.svg');
if (cityName.classList.contains('text-white')) {
cityName.classList.remove('text-white');
} else {
cityName.classList.add('text-black');
}
} else {
timeImg.setAttribute('src', 'img/night_image.svg');
if (cityName.classList.contains('text-black')) {
cityName.classList.remove('text-black');
} else {
cityName.classList.add('text-white');
}
}
cardInfo.classList.remove('d-none');
}
searchform.addEventListener('submit', event => {
event.preventDefault();
const citySearched = cityValue.value.trim();
console.log(citySearched);
searchform.reset();
requestCity(citySearched)
.then((data) => { updateApp(data); })
.catch((error) => { console.log(error) })
})