-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
110 lines (91 loc) · 3.11 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
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
106
107
108
109
110
let cards = document.querySelectorAll('.single_card');
let cardCurrent = document.querySelectorAll('.current_time');
let cardPrevTime = document.querySelectorAll('.previous_time');
let cardPrevTitle = document.querySelectorAll('.previous_time_title');
let setClick = document.querySelectorAll(".profile_bottom li")
async function getLocalData() {
const res = await fetch('./data.json');
if (res.ok) {
const result = await res.json();
return result;
} else {
console.log(res.status)
}
}
setClick.forEach(set => {
set.addEventListener('click', () => {
setClick.forEach(set => set.classList.remove('display_active'))
if (set.childNodes[1].textContent === "Daily") {
set.classList.add("display_active");
getLocalData()
.then(result => insertData(result, set.childNodes[1].textContent))
.catch(error => console.log("error"))
}
else if (set.childNodes[1].textContent === "Weekly") {
set.classList.add("display_active");
getLocalData()
.then(result => insertData(result, set.childNodes[1].textContent))
.catch(error => console.log("error"))
}
else if (set.childNodes[1].textContent === "Monthly") {
set.classList.add("display_active");
getLocalData()
.then(result => insertData(result, set.childNodes[1].textContent))
.catch(error => console.log("error"))
}
})
})
function insertData(result, cardTitle) {
function switcher(checker, period) {
switch (checker) {
case 'Work':
cardCurrent[0].textContent = `${period.current}hrs`
cardPrevTime[0].textContent = `${period.previous}hrs`;
break;
case 'Play':
cardCurrent[1].textContent = `${period.current}hrs`
cardPrevTime[1].textContent = `${period.previous}hrs`;
break;
case 'Study':
cardCurrent[2].textContent = `${period.current}hrs`
cardPrevTime[2].textContent = `${period.previous}hrs`;
break;
case 'Exercise':
cardCurrent[3].textContent = `${period.current}hrs`
cardPrevTime[3].textContent = `${period.previous}hrs`;
break;
case 'Social':
cardCurrent[4].textContent = `${period.current}hrs`
cardPrevTime[4].textContent = `${period.previous}hrs`;
break;
case 'Self Care':
cardCurrent[5].textContent = `${period.current}hrs`
cardPrevTime[5].textContent = `${period.previous}hrs`;
break;
}
}
for (data of result) {
let checker = data.title;
if (cardTitle === "Daily") {
let period = data.timeframes.daily;
for (prevTitle of cardPrevTitle) {
prevTitle.textContent = `Yesterday - `;
}
switcher(checker, period)
}
else if (cardTitle === "Weekly") {
let period = data.timeframes.weekly;
for (prevTitle of cardPrevTitle) {
prevTitle.textContent = `Last Week - `;
}
switcher(checker, period)
}
else if (cardTitle === "Monthly") {
let period = data.timeframes.monthly;
for (prevTitle of cardPrevTitle) {
prevTitle.textContent = 'Last Month - '
}
switcher(checker, period)
}
}
}