forked from USFGDSC/async-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
124 lines (101 loc) · 3.79 KB
/
Copy pathindex.js
File metadata and controls
124 lines (101 loc) · 3.79 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
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
API_key = "f4b548007394a5b93f012815fcabba74";
async function getCurrentWeather(lat, lon){
try{
const response = await fetch(`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=${API_key}`, {mode: "cors"});
const weatherData = await response.json();
console.log(weatherData);
return weatherData
} catch{}
}
async function getCoordinates(name){
try{
const response = await fetch(`http://api.openweathermap.org/geo/1.0/direct?q=${name}&limit=5&appid=${API_key}`, {mode: "cors"});
const data = await response.json();
const latLon ={
lat: data[0].lat,
lon: data[0].lon,
}
return latLon
}catch{}
}
async function weather(name){
try{
const coordinates = getCoordinates(name);
const data = await getCurrentWeather((await coordinates).lat, (await coordinates).lon);
const namelocation = data.name;
const countryCode = data.sys.country;
const description = data.weather[0].description;
const temperature = data.main.temp;
const feelsLike = data.main.feels_like;
const windspeed = data.wind.speed;
const humidity = data.main.humidity;
return{
namelocation,
countryCode,
description,
temperature,
feelsLike,
windspeed,
humidity,
}
}catch{
return " error"
}
}
weather("london");
const renderWeatherComponents = (weatherObject) => {
const main = document.createElement("main");
document.querySelector("body").appendChild(main)
const locationName = document.createElement("H1");
locationName.id='location';
locationName.textContent = `${weatherObject.namelocation}, ${weatherObject.countryCode}`;
main.appendChild(locationName)
const description = document.createElement("h2");
description.id='description';
description.textContent = `${weatherObject.description}`;
main.appendChild(description);
const bottomContainer = document.createElement("div");
bottomContainer.id = "bottomContainer";
main.appendChild(bottomContainer);
const leftSide = document.createElement("div");
leftSide.id = "leftSide";
bottomContainer.appendChild(leftSide);
const temperature = document.createElement("h2");
temperature.id = "temperature";
temperature.textContent = `${weatherObject.temperature}`;
leftSide.appendChild(temperature);
const units = document.createElement("h4")
units.id = "units";
units.textContent = 'C';
leftSide.appendChild(units);
const rightSide = document.createElement("div");
rightSide.id = "rightSide";
bottomContainer.appendChild(rightSide);
const feelsLike = document.createElement("p");
feelsLike.id = "feelsLike";
feelsLike.textContent = `Feels Like: ${weatherObject.feelsLike} C`;
rightSide.appendChild(feelsLike);
const windspeed = document.createElement("p");
windspeed.id = "wind";
windspeed.textContent = `Wind: ${weatherObject.windspeed}`;
rightSide.appendChild(windspeed);
const humidity = document.createElement("p");
humidity.id = "humidity";
humidity.textContent = `Humidity: ${weatherObject.humidity}%`;
rightSide.appendChild(humidity);
}
async function renderer(weatherObj, first = false){
const weatherData = await weatherObj;
if (first == true){
renderWeatherComponents(weatherData);
}else{
document.querySelector("main").remove();
document.querySelector("input").value=="";
renderWeatherComponents(weatherData);
}
}
document.querySelector("form").addEventListener("submit", (event)=> {
event.preventDefault();
renderer(weather(document.querySelector("input").value));
})
renderer(weather("lyon"),true);