Skip to content

Commit

Permalink
Perf(JS) Update code
Browse files Browse the repository at this point in the history
  • Loading branch information
ViktorSvertoka committed Nov 5, 2023
1 parent fcf7806 commit 6f2a81f
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import SlimSelect from 'slim-select';

import { fetchBreeds, fetchCatByBreed } from './js/cat-api.js';
import iziToast from 'izitoast';
import 'izitoast/dist/css/iziToast.min.css';
import { fetchBreeds, fetchCatByBreed } from './js/cat-api.js';

const select = document.getElementById('breed-select');
const catInfo = document.querySelector('.cat-info');
Expand All @@ -22,15 +21,15 @@ const errorMessage = {
color: 'red',
};

function showElement(element, hidden) {
element.classList.toggle('ss-hide', !hidden);
function toggleClass(element, isVisible) {
element.classList.toggle('ss-hide', !isVisible);
}

async function handleBreedSelection() {
try {
const selectedBreedId = select.value;
showElement(catInfo, false);
showElement(spinner, true);
toggleClass(catInfo, false);
toggleClass(spinner, true);

const catData = await fetchCatByBreed(selectedBreedId);

Expand All @@ -39,7 +38,7 @@ async function handleBreedSelection() {
iziToast.show(errorMessage);
}

showElement(spinner, false);
toggleClass(spinner, false);
}

function displayCatInfo(catData) {
Expand All @@ -56,7 +55,7 @@ function displayCatInfo(catData) {
</div>
`;

showElement(catInfo, true);
toggleClass(catInfo, true);
}

async function initializeApp() {
Expand All @@ -66,14 +65,14 @@ async function initializeApp() {
slimSelect.setData([{ placeholder: true, text: '' }, ...data]);
});

showElement(select, true);
toggleClass(select, true);
select.addEventListener('change', handleBreedSelection);
} catch (error) {
iziToast.show(errorMessage);
showElement(select, false);
toggleClass(select, false);
}

showElement(spinner, false);
toggleClass(spinner, false);
}

initializeApp();

0 comments on commit 6f2a81f

Please sign in to comment.