-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·47 lines (43 loc) · 1.45 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
import "./src/styles/main.scss";
import "@babel/polyfill";
import Forecast from "./src/modules/forecast";
const forecast = new Forecast();
const form = document.querySelector("form");
const cityDets = document.querySelector(".card_details");
const card = document.querySelector(".card");
const cardImg = document.querySelector(".card img");
const cardIcon = document.querySelector(".icon img");
const updateUI = ({ location, conditions }) => {
const imgSrc = conditions.IsDayTime
? "./src/assets/img/day.svg"
: "./src/assets/img/night.svg";
cardImg.setAttribute("src", imgSrc);
const iconSrc = `./src/assets/img/icons/${conditions.WeatherIcon}.svg`;
cardIcon.setAttribute("src", iconSrc);
cityDets.innerHTML = `
<h5 class="my-3">${location.EnglishName}</h5>
<div class="my-3">${conditions.WeatherText}</div>
<div class="display-4 my-4"><span>${
conditions.Temperature.Metric.Value
}</span><span>°C</span></div>
`;
card.classList.remove("d-none");
};
form.addEventListener("submit", e => {
e.preventDefault();
const city = form.city.value.trim();
form.reset();
forecast
.updateCity(city)
.then(data => updateUI(data))
.catch(error => console.log(error));
localStorage.setItem("city", city);
window.scrollTo(0, document.body.scrollHeight);
});
const city = localStorage.getItem("city");
if (city) {
forecast
.updateCity(city)
.then(data => updateUI(data))
.catch(error => console.log(error));
}