-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
105 lines (96 loc) · 4.13 KB
/
app.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
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
//theme toggler
const toggle = document.querySelector('.toggle');
toggle.addEventListener('click',()=>{
document.body.classList.toggle('light-mode');
toggle.classList.toggle('active');
})
//switch active class
const btns = document.querySelectorAll('.btn');
btns.forEach(btn=>{
btn.addEventListener('click',(e)=>{
document.querySelector('.btn.active').classList.remove('active');
e.target.classList.add('active');
})
})
//fetch data
fetch('./data.json')
.then(res => res.json())
.then(data => {
const main = document.querySelector('main');
function getDaily(){
main.textContent = "";
for(let i=0; i<data.length; i++){
main.innerHTML += `
<div class="card">
<a href="#" class="details">
<section class="details__title">
<h2>${data[i].title}</h2>
<i class="fa-solid fa-ellipsis"></i>
</section>
<section class="details__time">
<h3 data-num="${data[i].timeframes.daily.current}">${data[i].timeframes.daily.current}hrs</h3>
<p>Yesterday - ${data[i].timeframes.daily.previous}hrs</p>
</section>
</a>
</div>`
}
}
function getWeekly(){
main.textContent = "";
for(let i=0; i<data.length; i++){
main.innerHTML += `
<div class="card">
<a href="#" class="details">
<section class="details__title">
<h2>${data[i].title}</h2>
<i class="fa-solid fa-ellipsis"></i>
</section>
<section class="details__time">
<h3 data-num="${data[i].timeframes.weekly.current}">${data[i].timeframes.weekly.current}hrs</h3>
<p>Last Week - ${data[i].timeframes.weekly.previous}hrs</p>
</section>
</a>
</div>`
}
}
function getMonthly(){
main.textContent = "";
for(let i=0; i<data.length; i++){
main.innerHTML += `
<div class="card">
<a href="#" class="details">
<section class="details__title">
<h2>${data[i].title}</h2>
<i class="fa-solid fa-ellipsis"></i>
</section>
<section class="details__time">
<h3 data-num="${data[i].timeframes.monthly.current}">${data[i].timeframes.monthly.current}hrs</h3>
<p>Last Month - ${data[i].timeframes.monthly.previous}hrs</p>
</section>
</a>
</div>`
}
}
window.addEventListener('load',getWeekly()); //load weekly data by default
//counter animation
let numbers = document.querySelectorAll('h3');
let interval = 1000;
numbers.forEach(num =>{
let startValue = 0;
let endValue = parseInt(num.getAttribute('data-num'));
let duration = Math.floor(interval / endValue);
let counter = setInterval(()=>{
startValue++;
num.textContent = `${startValue}hrs`;
if(startValue === endValue){
clearInterval(counter);
}
}, duration)
})
const dailyBtn = document.querySelector('.daily-btn');
const weeklyBtn = document.querySelector('.weekly-btn');
const monthlyBtn = document.querySelector('.monthly-btn');
dailyBtn.addEventListener('click', getDaily);
weeklyBtn.addEventListener('click', getWeekly);
monthlyBtn.addEventListener('click', getMonthly);
});