-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
77 lines (69 loc) · 2.46 KB
/
app.js
File metadata and controls
77 lines (69 loc) · 2.46 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
window.addEventListener("load", () => {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
position => {
const lat = position.coords.latitude;
const long = position.coords.longitude;
getWeather(lat, long);
},
function(e) {
window.alert("This app needs your location to function properly!");
}
);
}
});
const setIcons = (icon, iconID) => {
const skycons = new Skycons({ color: "white" });
skycons.set(icon, iconID);
skycons.play();
};
async function getWeather(lat, long) {
const fixedLat = Number(lat.toFixed(4));
const fixedLong = Number(long.toFixed(4));
const corsAnywhereUrl = "https://cors-anywhere.herokuapp.com/";
const apiUrl = `https://api.darksky.net/forecast/e0391512f84780b1d9fb3bb1f8f708cd/${fixedLat},${fixedLong}`;
let weatherReq;
try {
weatherReq = await fetch(corsAnywhereUrl + apiUrl);
} catch (e) {
paintDom(null, false);
console.error(e);
}
const json = await weatherReq.json();
paintDom(json, true);
}
function paintDom(json, sucessful) {
if (!sucessful) {
title.innerText = "Error loading... Please try again later";
tempValue.innerText = tempUnit.innerText = tempDesc.innerText = "...";
return;
}
const title = document.getElementsByClassName("title")[0];
const iconCanvas = document.getElementsByClassName("animated-icon")[0];
const tempValue = document.getElementsByClassName("temp-value")[0];
const tempUnit = document.getElementsByClassName("temp-unit")[0];
const tempDesc = document.getElementsByClassName("temp-desc")[0];
title.innerText = `Timezone: ${json.timezone
.split("/")
.reverse()
.join(", ")
.replace(/_/gm, " ")}`;
tempValue.innerText = `Temperature: ${json.currently.temperature}`;
tempUnit.innerText = `Fahreinheit, ${json.flags.units.toUpperCase()}`;
tempDesc.innerHTML = `Currently <strong>${json.currently.summary}</strong>`;
setIcons(iconCanvas, json.currently.icon);
document
.getElementsByClassName("degree-section")[0]
.addEventListener("click", function() {
if (tempUnit.innerText.includes("US")) {
tempValue.innerText = `Temperature: ${(
json.currently.temperature -
32 * (5 / 9)
).toFixed(2)}`;
tempUnit.innerText = "Celsius, EARTH";
} else {
tempValue.innerText = `Temperature: ${json.currently.temperature}`;
tempUnit.innerText = `Fahreinheit, ${json.flags.units.toUpperCase()}`;
}
});
}