-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
76 lines (56 loc) · 1.68 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
70
71
72
73
74
75
76
let isLightThemeColor = false;
let isRandom = true;
async function loadRandomCats() {
const data = await fetchData(API_URL);
if (data === null) return;
loadData(data, false);
}
async function loadFavoriteCats() {
const data = await fetchData(API_FAVOURITES_API);
if (data === null) return;
catsReference.innerHTML = '';
loadData(data, true);
}
function setTheme() {
isLightThemeColor = !isLightThemeColor;
if (isLightThemeColor) {
link.href = "./styles/lightTheme.css";
} else {
link.href = "./styles/darkTheme.css";
}
}
async function setContent(isRandomCats, firstRender) {
if (isRandom === isRandomCats || isLoading) {
if (!firstRender) return;
}
selectNavigation(isRandomCats);
catsReference.innerHTML = "";
addLoadingCards();
if (isRandomCats) {
await loadRandomCats();
} else {
await loadFavoriteCats();
}
removeLoadingCards();
isRandom = isRandomCats;
}
function selectNavigation(isRandomCats) {
randomCatsNavigation.className = "";
favoriteCatsNavigation.className = "";
if (isRandomCats) {
randomCatsNavigation.className = "selectedNavigation";
} else {
favoriteCatsNavigation.className = "selectedNavigation";
}
}
async function main() {
const switchTheme = document.getElementById('switchTheme');
switchTheme.onclick = () => setTheme();
setTheme();
const uploadButton = document.getElementById('upload');
uploadButton.onclick = () => uploadPhoto();
randomCatsNavigation.onclick = () => setContent(true, false);
favoriteCatsNavigation.onclick = () => setContent(false, false);
await setContent(true, true);
}
main();