/
main.js
69 lines (60 loc) · 2.05 KB
/
main.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
67
68
69
"use strict"
// VARIABLES --------------------------------------------------------
const timeContainer = document.querySelector(".timeContainer")
const frequencyItems = document.querySelectorAll(".frequency__item")
let timeframe = "weekly"
let data = ""
// RENDER TIME --------------------------------------------------------
const render = function (data) {
// generate markup
const markup = data
.map((data) => {
const title = data.title.toLowerCase()
return `
<section class="${title}">
<div class="background-border background-border-${data.title
.toLowerCase()
.replace(" ", "-")}"></div>
<div class="${title}-content content">
<div class="${title}__header header">
<h2 class="heading-2">${data.title}</h2>
<img src="./images/icon-ellipsis.svg" alt="..." class="ellipsis" />
</div>
<div class="${title}__duration duration">
<h4 class="${title}__hour heading-4">${
data.timeframes[timeframe].current
} hrs</h4>
<h6 class="${title}__hour-lastweek heading-6">Last Week - ${
data.timeframes[timeframe].previous
} hrs</h6>
</div>
</div>
</section>
`
})
.join("")
// insert to container element
timeContainer.insertAdjacentHTML("beforeend", markup)
timeContainer.style.opacity = "1"
}
// CHANGE TIME FRAME --------------------------------------------------------
const setTimeframe = function () {
frequencyItems.forEach((item) => item.classList.remove("active"))
this.classList.add("active")
timeframe = this.textContent.toLowerCase()
//rerender container HTML
timeContainer.innerHTML = ""
render(data)
}
frequencyItems.forEach((item) => item.addEventListener("click", setTimeframe))
// LOAD DATA --------------------------------------------------------
const loadData = async function () {
try {
data = await fetch("./data.json").then((res) => res.json())
console.log(data)
render(data)
} catch (err) {
console.error(err.message)
}
}
loadData()