-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
182 lines (168 loc) · 4.86 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
// Selecting element
const factsTextBox = document.querySelector(".main__text");
const newJockBtn = document.querySelector(".button__newJock");
const timerBtn = document.querySelector(".button__timer");
const addFavsBtn = document.querySelector(".button__addfavs");
const openFavBtn = document.querySelector(".button__openfavs");
const timer = document.querySelector(".timer");
const favlist = document.querySelector(".favlist");
const clearFavBtn = document.querySelector(".button__clear");
const backBtn = document.querySelector(".button__back");
const homePage = document.querySelector(".homepage");
const favText = document.querySelector(".facts__textbox");
// global varible
const API_URL = "https://api.chucknorris.io/jokes/random";
let intervalId;
let timerStart;
let fact;
let factList = getSavedFacts();
let deleteBtn;
let wrapper;
// function
function getSavedFacts() {
const factJSON = localStorage.getItem("fact");
try {
return factJSON ? JSON.parse(factJSON) : [];
} catch (e) {
return [];
}
}
function preLoader() {
const loading = `<div class="loader">
<img
src="./images/loading-bar.gif"
alt="Loading..."
class="loader__img"
/>
</div>`;
factsTextBox.innerHTML = loading;
}
function newJock() {
preLoader();
axios
.get(API_URL)
.then((res) => {
fact = res.data.value;
factsTextBox.textContent = fact;
})
.catch((err) => {
console.log(err);
});
addFavsBtn.textContent = "ADD TO FAVS";
addFavsBtn.style.color = "#acaaf5";
}
function countdownTimer() {
timerStart -= 0.1;
if (timerStart <= 0.0) {
timerStart = 3.1;
}
timer.textContent = `NEW JOKE IN ${timerStart.toFixed(1)}`;
}
function startTimer() {
if (timerBtn.textContent.includes("START")) {
intervalId = setInterval(newJock, 3000);
intervalId2 = setInterval(countdownTimer, 100);
timerStart = 3.1;
timerBtn.textContent = "STOP 3 SEC TIMER";
} else if (timerBtn.textContent.includes("STOP")) {
clearInterval(intervalId);
clearInterval(intervalId2);
timerBtn.textContent = "START 3 SEC TIMER";
timer.textContent = "";
}
}
function addFactToList() {
if (addFavsBtn.textContent.includes("ADD") & (fact !== undefined)) {
factList.push(fact);
addFavsBtn.textContent = "DELETE FROM FAVS";
addFavsBtn.style.color = "#fc6969";
} else if (addFavsBtn.textContent.includes("DELETE")) {
factList.pop();
addFavsBtn.textContent = "ADD TO FAVS";
addFavsBtn.style.color = "#acaaf5";
}
if (factList.length === 10) {
factList.shift();
}
writeFact();
}
writeFact();
function writeFact() {
favText.innerHTML = "";
localStorage.setItem("fact", JSON.stringify(factList));
if (factList.length > 0) {
factList.forEach((factt, index) => {
let output = `
<div class='wrapper'>
<div class="facts__text fav__text">
${factt}
</div>
<div class="bottom-wrapper">
<span class="favfactnumber">${index + 1}</span>
<div class="btn-2 button__delete">
DELETE
</div>
</div>
</div>`;
favText.innerHTML += output;
});
} else {
const emptymsg = document.createElement("p");
emptymsg.classList.add("emptymsg");
emptymsg.textContent = "No Favs to show 🧙♂️";
favText.appendChild(emptymsg);
}
wrapper = document.querySelectorAll(".wrapper");
deleteBtn = document.querySelectorAll(".button__delete");
deletefav();
}
function openFavList() {
favlist.classList.remove("disable");
homePage.classList.add("disable");
}
function clearFavButton() {
localStorage.clear();
factList = [];
writeFact();
addFavsBtn.textContent = "ADD TO FAVS";
addFavsBtn.style.color = "#acaaf5";
}
function deleteButton(btn, index) {
factList.splice(index, 1);
localStorage.setItem("fact", JSON.stringify(factList));
wrapper.forEach((el) => {
el.addEventListener("click", (e) => {
if (e.target === btn) {
// el.remove();
writeFact();
}
wrapper = document.querySelectorAll(".wrapper");
deleteBtn = document.querySelectorAll(".button__delete");
if (deleteBtn.length === 0) {
addFavsBtn.textContent = "ADD TO FAVS";
addFavsBtn.style.color = "#acaaf5";
}
});
});
}
function backButton() {
favlist.classList.add("disable");
homePage.classList.remove("disable");
}
function deletefav() {
if (deleteBtn.length > 0) {
deleteBtn.forEach((btn, index) => {
btn.addEventListener("click", () => {
deleteButton(btn, index);
});
});
}
}
deletefav();
// Adding event listener
backBtn.addEventListener("click", backButton);
newJockBtn.addEventListener("click", newJock);
addFavsBtn.addEventListener("click", addFactToList);
timerBtn.addEventListener("click", startTimer);
openFavBtn.addEventListener("click", openFavList);
clearFavBtn.addEventListener("click", clearFavButton);