-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
29 lines (25 loc) · 1.06 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
const timeframe = document.querySelectorAll(".dashboard__timeframe");
let hours = document.querySelectorAll(".dynamic-hours");
let previous = document.querySelectorAll(".dynamic-previous");
timeframe[1].classList.add("active");
getData("weekly");
function handleClick(index, period) {
timeframe.forEach((e) => e.classList.remove("active"));
timeframe[index].classList.add("active");
getData(period);
}
timeframe[0].addEventListener("click", () => handleClick(0, "daily"));
timeframe[1].addEventListener("click", () => handleClick(1, "weekly"));
timeframe[2].addEventListener("click", () => handleClick(2, "monthly"));
function getData(timeframe) {
fetch("data.json")
.then((response) => response.json())
.then((data) => {
for (let i = 0; i < data.length; i++) {
let outHours = data[i].timeframes[timeframe].current;
let outPrevious = data[i].timeframes[timeframe].previous;
hours[i].innerHTML = outHours;
previous[i].innerHTML = outPrevious;
}
});
}