Skip to content

Commit

Permalink
Pagination info
Browse files Browse the repository at this point in the history
  • Loading branch information
EuJinnLucaShow committed Nov 12, 2023
1 parent 72722e7 commit fe58972
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 20 deletions.
56 changes: 41 additions & 15 deletions src/js/pagination.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const prevButton = document.getElementById('prev-button');
const nextButton = document.getElementById('next-button');

let isShown = 0;
let isFirstSearch = true;
let isFirstSearch = null;
let currentPage = 1;
let query = ''
const api = new ApiService();
Expand Down Expand Up @@ -71,7 +71,7 @@ async function fetchGallery(currentPage) {
}

if (isFirstSearch && isShown < totalHits) {
showToast('success', `Hooray! We found ${totalHits} images !!!`);
showToast('success', `Hooray! We found ${totalHits} images!`);
isFirstSearch = false;
setupPagination({ hits, totalHits });
}
Expand All @@ -81,7 +81,7 @@ async function fetchGallery(currentPage) {
isShown += hits.length;

if (isShown >= totalHits) {
showToast('info', "You've reached the end of search results.");
showToast('info', "You've reached the end of search results.");
}
} catch (error) {
console.error('Error fetching gallery:', error);
Expand Down Expand Up @@ -134,29 +134,55 @@ function setupPagination({ hits, totalHits }) {
pageNumber.textContent = i;

paginationByttons.appendChild(pageNumber);
paginationContainer.classList.remove('is-hidden')

if (pageCount > 2) {
paginationContainer.classList.remove('is-hidden')
}

pageNumber.addEventListener('click', () => {
setCurrentPage(i);
});
}
handlePageButtonsStatus();
handlePageButtonsStatus();
handleActivePageNumber();
}

function setCurrentPage(i) {
currentPage = i;
fetchGallery(currentPage);
handlePageButtonsStatus();
handleActivePageNumber();
}

function handlePageButtonsStatus() {
prevButton.disabled = currentPage === 1;
nextButton.disabled = currentPage === paginationByttons.children.length;
}
const disableButton = (button) => {
button.classList.add("disabled");
button.setAttribute("disabled", true);
};

const enableButton = (button) => {
button.classList.remove("disabled");
button.removeAttribute("disabled");
};

const handlePageButtonsStatus = () => {
if (currentPage === 1) {
disableButton(prevButton);
} else {
enableButton(prevButton);
}

prevButton.addEventListener('click', () => {
setCurrentPage(currentPage - 1);
});
if (currentPage === paginationByttons.children.length) {
disableButton(nextButton);
} else {
enableButton(nextButton);
}
};

nextButton.addEventListener('click', () => {
setCurrentPage(currentPage + 1);
});
const handleActivePageNumber = () => {
document.querySelectorAll(".pagination-number").forEach((button, page) => {
button.classList.remove("active");
if (page + 1 === currentPage) {
button.classList.add("active");
}
});
};
15 changes: 10 additions & 5 deletions src/sass/_pagination.scss
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
.pagination-container {
display: flex;
justify-content: center;
justify-content: center;
margin: 20px 0;
}

.pagination-number,
.pagination-button{
font-size: 1.1rem;
background-color: transparent;
border: none;
margin: 0.25rem 0.25rem;
cursor: pointer;
margin: 0.25rem 0.25rem;
height: 2.5rem;
width: 2.5rem;
border-radius: .2rem;
}

.pagination-number:hover,
.pagination-button:not(.disabled):hover {
background: #fff;
background: #dedede;
border-radius: 50px;
cursor: pointer;
}

.pagination-number.active {
color: #fff;
background: #0085b6;
background: #949494;
border-radius: 50px;
}

0 comments on commit fe58972

Please sign in to comment.