-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
66 lines (53 loc) · 1.98 KB
/
script.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
import getThisYearsData from "./data.js";
import { calcMax, calcMaxWithTime } from "./calc-max.js";
import { printData, printDataWithTime } from "./print-data.js";
import { container } from "./sort-btn.js";
const latestUpdate = document.querySelector("#latest-update");
const datetimeBtn = document.querySelector("#datetime-btn");
const dateBtn = document.querySelector("#date-btn");
const clockIcon = document.querySelector("#clock-icon");
/*let apiData = [];
let localStorageApiData = localStorage.getItem("apiData");
if (localStorageApiData) {
apiData = JSON.parse(localStorageApiData);
} else {
apiData = await fetchOpenData();
}*/
const apiData = await fetchOpenData();
//console.log(apiData);
const values = apiData.value;
let thisYearsData = [];
async function fetchOpenData() {
const openDataLink = "https://opendata-download-metobs.smhi.se/api/version/1.0/parameter/1/station/89230/period/latest-months/data.json";
const response = await fetch(openDataLink);
const latestMonthsData = await response.json();
//localStorage.setItem("apiData", JSON.stringify(latestMonthsData));
return latestMonthsData;
}
thisYearsData = getThisYearsData(values, thisYearsData);
//console.log(thisYearsData);
const latest = thisYearsData[thisYearsData.length - 1].datetime;
latestUpdate.innerText = `Senast uppdaterad: ${latest}`;
let maxTemps = calcMax(thisYearsData);
maxTemps.forEach(entry => {
printData(entry);
});
dateBtn.addEventListener("click", function () {
container.innerText = "";
let maxTempsWithTime = calcMaxWithTime(thisYearsData);
maxTempsWithTime.forEach(entry => {
printDataWithTime(entry);
});
datetimeBtn.style.display = "inline-block";
clockIcon.style.display = "inline-block";
dateBtn.style.display = "none";
});
datetimeBtn.addEventListener("click", function () {
container.innerText = "";
maxTemps.forEach(entry => {
printData(entry);
});
dateBtn.style.display = "inline-block";
datetimeBtn.style.display = "none";
clockIcon.style.display = "none";
});