-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
60 lines (50 loc) · 1.84 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
const buttons = document.querySelectorAll('button[data-type]');
const cards = {
'Work': document.querySelector('.work'),
'Play': document.querySelector('.play'),
'Study': document.querySelector('.study'),
'Exercise': document.querySelector('.exercise'),
'Social': document.querySelector('.social'),
'Self Care': document.querySelector('.self-care'),
};
const fetchActivities = timeframe => {
fetch('./data.json')
.then(response => response.json())
.then(data => handleActivities(timeframe, data))
.catch(error => handleError(error));
};
const handleError = error => console.error(error);
const handleActivities = (timeframe, activities) => {
activities.forEach(activity => {
const { title, timeframes } = activity;
const { current, previous } = timeframes[timeframe];
const card = cards[title];
card.querySelector('.card-title').textContent = title;
card.querySelector('.current').textContent = `${current}hrs`;
card.querySelector('.previous').textContent = `${lastTime(timeframe)} - ${previous}hrs`;
card.classList.add('zoom');
});
};
const lastTime = timeframe => {
switch (timeframe) {
case 'daily' : return 'Yesterday';
case 'weekly' : return 'Last Week';
default : return 'Last Month';
}
};
const handleClick = event => {
const className = 'active';
const activedButton = document.querySelector(`button.${className}`);
activedButton.classList.remove(className);
const clickedButton = event.target;
clickedButton.classList.add(className);
fetchActivities(clickedButton.dataset.type);
};
const handleAnimation = event => event.target.classList.remove('zoom');
for (title in cards) {
cards[title].addEventListener('animationend', handleAnimation);
}
for (btn of buttons) {
btn.addEventListener('click', handleClick);
if (btn.dataset.type === 'weekly') btn.click();
}