Skip to content

Commit e9db037

Browse files
committed
Navigation styles and loading state
1 parent 25958a0 commit e9db037

File tree

8 files changed

+223
-78
lines changed

8 files changed

+223
-78
lines changed

index.html

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
66
<link rel="stylesheet" href="./styles/styles.css">
7+
<link rel="stylesheet" href="./styles/elements.css">
8+
<link id="currentTheme" rel="stylesheet" href="./styles/lightTheme.css">
9+
<link rel='stylesheet' href='https://cdn-uicons.flaticon.com/uicons-regular-rounded/css/uicons-regular-rounded.css'>
710
<link rel="preconnect" href="https://fonts.googleapis.com">
811
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
912
<link href="https://fonts.googleapis.com/css2?family=Gabarito:wght@400;500;600&display=swap" rel="stylesheet">
@@ -16,6 +19,9 @@ <h1>Random cats app</h1>
1619
<p>
1720
Random cat viewer, choose the image you like the most and mark it as your favorite one
1821
</p>
22+
<button type="button" id="switchTheme">
23+
<i class="fi fi-rr-swatchbook"></i> theme
24+
</button>
1925
<section id="uploadingCat">
2026
<h2></h2>
2127
<form id="uploadingForm">
@@ -27,17 +33,16 @@ <h2></h2>
2733
</section>
2834
<span id="error"></span>
2935
<ul>
30-
<li>
36+
<li id="randomCatsNavigation">
37+
<i class="fi fi-rr-gallery"></i>
3138
Random cats
3239
</li>
33-
<li>
40+
<li id="favoriteCatsNavigation">
41+
<i class="fi fi-rr-heart"></i>
3442
Favorites
3543
</li>
3644
</ul>
37-
<section id="randomCats">
38-
</section>
39-
<h2>Favorite cats</h2>
40-
<section id="favoriteCats">
45+
<section id="cats">
4146
</section>
4247
</div>
4348
</div>

javascript/constants.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ const API_KEY = "live_l0ii654AjIwmoMG0NEH0vOPDbD5FllJPEqXF8HIB8nFxMXHhoq37OXCLnk
22
const BASE_API_URL = "https://api.thecatapi.com/v1/";
33
const API_URL = `${BASE_API_URL}images/search?limit=18`;
44
const API_FAVOURITES_API = `${BASE_API_URL}favourites`;
5+
const link = document.getElementById("currentTheme");
56
const form = document.getElementById('uploadingForm');
6-
const randomCatsReference = document.getElementById("randomCats");
7-
const favoriteCatsReference = document.getElementById("favoriteCats");
7+
const catsReference = document.getElementById("cats");
88
const spanError = document.getElementById("error");
9+
const randomCatsNavigation = document.getElementById("randomCatsNavigation");
10+
const favoriteCatsNavigation = document.getElementById(
11+
"favoriteCatsNavigation"
12+
);

javascript/dynamicContent.js

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
function loadData(reference, data, isFavorite) {
1+
let isLoading = true;
2+
3+
function loadData(data, isFavorite) {
24
for (let i = 0; i < data.length; i++) {
3-
const url = reference === favoriteCatsReference ? data[i].image.url : data[i].url;
5+
const url = isFavorite ? data[i].image.url : data[i].url;
46

5-
addCatImage(reference, url, data[i].id, isFavorite);
7+
addCatImage(url, data[i].id, isFavorite);
68
}
79
}
810

9-
function addCatImage(reference, sourceImage, imageId, isFavorite) {
11+
function addCatImage(sourceImage, imageId, isFavorite) {
1012
const catArticle = document.createElement("article");
1113
const catImage = document.createElement("img");
1214
catImage.alt = "Random cat picture";
@@ -35,5 +37,30 @@ function addCatImage(reference, sourceImage, imageId, isFavorite) {
3537

3638
catArticle.appendChild(options);
3739

38-
reference.appendChild(catArticle);
40+
catsReference.appendChild(catArticle);
41+
}
42+
43+
function removeLoadingCards() {
44+
const loadingCards = document.querySelectorAll(".catLoading");
45+
46+
loadingCards.forEach(element => {
47+
element.parentNode.removeChild(element);
48+
});
49+
50+
isLoading = false;
3951
}
52+
53+
function addLoadingCards() {
54+
isLoading = true;
55+
56+
for (let i = 0; i < 10; i++) {
57+
addLoadingCard();
58+
}
59+
}
60+
61+
function addLoadingCard() {
62+
const catLoading = document.createElement('article');
63+
catLoading.className = 'catLoading';
64+
65+
catsReference.appendChild(catLoading);
66+
}

main.js

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1+
let isLightThemeColor = false;
2+
let isRandom = true;
3+
14
async function loadRandomCats() {
25
const data = await fetchData(API_URL);
36

47
if (data === null) return;
58

6-
randomCatsReference.innerHTML = '';
7-
loadData(randomCatsReference, data, false);
9+
loadData(data, false);
810
}
911

1012
async function loadFavoriteCats() {
1113
const data = await fetchData(API_FAVOURITES_API);
1214

1315
if (data === null) return;
1416

15-
favoriteCatsReference.innerHTML = '';
16-
loadData(favoriteCatsReference, data, true);
17+
catsReference.innerHTML = '';
18+
loadData(data, true);
1719
}
1820

1921
async function uploadPhoto() {
@@ -22,12 +24,59 @@ async function uploadPhoto() {
2224
console.log(formData.get('file'));
2325
}
2426

27+
function setTheme() {
28+
isLightThemeColor = !isLightThemeColor;
29+
30+
if (isLightThemeColor) {
31+
link.href = "./styles/lightTheme.css";
32+
} else {
33+
link.href = "./styles/darkTheme.css";
34+
}
35+
}
36+
37+
async function setContent(isRandomCats, firstRender) {
38+
if (isRandom === isRandomCats || isLoading) {
39+
if (!firstRender) return;
40+
}
41+
42+
selectNavigation(isRandomCats);
43+
catsReference.innerHTML = "";
44+
addLoadingCards();
45+
46+
if (isRandomCats) {
47+
await loadRandomCats();
48+
} else {
49+
await loadFavoriteCats();
50+
}
51+
52+
removeLoadingCards();
53+
54+
isRandom = isRandomCats;
55+
}
56+
57+
function selectNavigation(isRandomCats) {
58+
randomCatsNavigation.className = "";
59+
favoriteCatsNavigation.className = "";
60+
61+
if (isRandomCats) {
62+
randomCatsNavigation.className = "selectedNavigation";
63+
} else {
64+
favoriteCatsNavigation.className = "selectedNavigation";
65+
}
66+
}
67+
2568
async function main() {
69+
const switchTheme = document.getElementById('switchTheme');
70+
switchTheme.onclick = () => setTheme();
71+
72+
setTheme();
73+
2674
const uploadButton = document.getElementById('upload');
2775
uploadButton.onclick = () => uploadPhoto();
76+
randomCatsNavigation.onclick = () => setContent(true, false);
77+
favoriteCatsNavigation.onclick = () => setContent(false, false);
2878

29-
await loadRandomCats();
30-
await loadFavoriteCats();
79+
await setContent(true, true);
3180
}
3281

3382
main();

styles/darkTheme.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:root {
2+
--background-color: rgb(17, 17, 17);
3+
--title-color: rgb(255, 255, 255);
4+
--text-color: rgb(112, 112, 112);
5+
--light-color: rgb(66, 66, 66);
6+
--first-loading-color: #3c3c3c;
7+
--second-loading-color: #242424;
8+
}

styles/elements.css

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
h1 {
2+
margin-bottom: 6px;
3+
}
4+
5+
h1,
6+
h2 {
7+
color: var(--title-color);
8+
}
9+
10+
p,
11+
ul li {
12+
color: var(--text-color);
13+
font-weight: 300;
14+
}
15+
16+
button {
17+
padding: 8px 10px;
18+
border-radius: 8px;
19+
border: none;
20+
background: var(--title-color);
21+
color: var(--background-color);
22+
font-family: var(--font-family);
23+
font-weight: 400;
24+
text-transform: capitalize;
25+
}
26+
27+
ul {
28+
display: flex;
29+
align-items: center;
30+
justify-content: center;
31+
list-style: none;
32+
border-top: 1px solid var(--light-color);
33+
}
34+
35+
ul li {
36+
display: flex;
37+
justify-content: center;
38+
align-items: center;
39+
gap: 10px;
40+
padding: 20px;
41+
cursor: pointer;
42+
}
43+
44+
.selectedNavigation {
45+
border-top: 3px solid var(--title-color);
46+
color: var(--title-color);
47+
}
48+
49+
article {
50+
position: relative;
51+
height: 350px;
52+
}
53+
54+
.options {
55+
position: absolute;
56+
top: 0;
57+
display: none;
58+
justify-content: center;
59+
align-items: center;
60+
width: 100%;
61+
height: 100%;
62+
background: rgba(0, 0, 0, 0.1);
63+
transition: display 2s ease-in-out;
64+
}
65+
66+
article:hover .options {
67+
display: flex;
68+
}
69+
70+
.options button {
71+
cursor: pointer;
72+
}
73+
74+
article img {
75+
width: 100%;
76+
height: 350px;
77+
object-fit: cover;
78+
border-radius: 1px;
79+
}
80+
81+
.catLoading {
82+
background: linear-gradient(90deg, var(--first-loading-color) 0%, var(--second-loading-color) 50%, var(--first-loading-color) 75%);
83+
background-size: 200% 100%;
84+
animation: loading 1.6s infinite;
85+
}
86+
87+
@keyframes loading {
88+
0% {
89+
background-position: 200% 0;
90+
}
91+
92+
100% {
93+
background-position: -200% 0;
94+
}
95+
}

styles/lightTheme.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
:root {
2+
--background-color: rgb(255, 255, 255);
3+
--title-color: rgb(27, 27, 27);
4+
--text-color: rgb(66, 66, 66);
5+
--light-color: rgb(212, 212, 212);
6+
--first-loading-color: #f0f0f0;
7+
--second-loading-color: #c4c4c4;
8+
}

0 commit comments

Comments
 (0)