Skip to content

Commit

Permalink
Merge pull request #65 from Valik3201/feature/pagination
Browse files Browse the repository at this point in the history
 feat: kombinacja z paginacją
  • Loading branch information
Valik3201 committed Jan 3, 2024
2 parents 6f4da46 + 479009c commit f6e8a9e
Show file tree
Hide file tree
Showing 6 changed files with 270 additions and 38 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
"url": "https://github.com/Valik3201/bookshelf/issues"
},
"devDependencies": {
"@parcel/transformer-image": "^2.10.3",
"@parcel/packager-raw-url": "^2.10.3",
"@parcel/transformer-image": "^2.10.3",
"@parcel/transformer-sass": "^2.8.3",
"@parcel/transformer-webmanifest": "^2.10.3",
"buffer": "^6.0.3",
Expand All @@ -33,8 +33,8 @@
},
"dependencies": {
"axios": "^1.6.2",
"lazysizes": "^5.3.2",
"firebase": "^10.7.1",
"lazysizes": "^5.3.2",
"notiflix": "^3.2.6",
"swiper": "^11.0.5"
}
Expand Down
36 changes: 29 additions & 7 deletions src/images/icons.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/js/localStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,15 @@ export const displayBooksFromLocalStorage = () => {

const imageBooks = new URL('/src/images/books.png', import.meta.url).href;
// Sprawdź, czy localStorage jest pusty
if (storedBooks.length === 0) {
if (storedBooks.length === 0 && container) {
// Jeśli localStorage jest pusty, wyświetl komunikat
const emptyMessageMarkup = `
<div class="book-message">This page is empty, add some books and proceed to order.</div>
<img class="books-img" src="${imageBooks}" alt="books" />
`;

container.innerHTML = emptyMessageMarkup;
} else if (container) {
} else {
// Jeśli localStorage nie jest pusty, dodaj książki do kontenera
storedBooks.forEach(book => {
const { _id, title, author, book_image, description, buy_links } = book;
Expand Down
118 changes: 118 additions & 0 deletions src/js/pagination.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import localStorage from './localStorage.js';

const paginationSection = document.querySelector('.pagination');
const paginationContainerPages = document.querySelector('.pagination_container-pages');
const paginationContainerBackBtn = document.querySelector('.pagination_container-back');
const paginationContainerEndBtn = document.querySelector('.pagination_container-end');
const startButton = document.querySelector("button[name='startButton']");
const previousButton = document.querySelector("button[name='previousButton']");
const nextButton = document.querySelector("button[name='nextButton']");
const endButton = document.querySelector("button[name='endButton']");

const shoppingList = JSON.parse(localStorage.getItem()) || [];

const pageSize = 3;
let totalPages = Math.ceil(shoppingList.length / pageSize);
let currentPage = 1;

for (let i = 1; i <= totalPages; i++) {
if (shoppingList.length <= 3) {
return;
}
const pageNumber = i;
const button = document.createElement('button');
button.classList.add('pagination_btn');
button.classList.add('pagination_btn--pages');
button.textContent = i;

activDisplayFlexOnElement(paginationContainerBackBtn);
activDisplayFlexOnElement(paginationContainerEndBtn);

button.addEventListener('click', () => {
currentPage = pageNumber;
removeDisableforElement(startButton);
removeDisableforElement(endButton);
});
paginationContainerPages.appendChild(button);
}

paginationContainerPages.firstChild.classList.add('active');

paginationSection.addEventListener('click', handlerPaginationButtonsStartPreviousNextStart);

function handlerPaginationButtonsStartPreviousNextStart(event) {
const activButton = document.querySelector('.active');
console.log(event.target);
if (event.target.tagName !== 'BUTTON') {
return;
}

switch (event.target) {
case previousButton:
if (currentPage > 1) {
currentPage--;
removeDisableforElement(endButton);

activButton.classList.remove('active');
activButton.previousElementSibling.classList.add('active');
}
break;

case nextButton:
if (currentPage < totalPages) {
currentPage++;
removeDisableforElement(startButton);

activButton.classList.remove('active');
activButton.nextElementSibling.classList.add('active');
}
break;

case startButton:
currentPage = 1;
addDisableforElement(startButton);
removeDisableforElement(endButton);
highlighteTheCurrentPage(paginationContainerPages.firstChild);
break;

case endButton:
currentPage = totalPages;
addDisableforElement(endButton);
removeDisableforElement(startButton);
highlighteTheCurrentPage(paginationContainerPages.lastElementChild);
break;
default:
break;
}
}

paginationContainerPages.addEventListener('click', handleButtonPaginationContainerPages);

function handleButtonPaginationContainerPages(event) {
if (event.target.tagName !== 'BUTTON') {
return;
}
highlighteTheCurrentPage(event.target);
}

function removeDisableforElement(element) {
element.disabled = false;
}

function addDisableforElement(element) {
element.disabled = true;
}

function activDisplayFlexOnElement(element) {
element.style.display = 'flex';
}

function highlighteTheCurrentPage(element) {
const activButton = document.querySelector('.active');

if (activButton) {
activButton.classList.remove('active');
}

element.classList.add('active');
}
73 changes: 46 additions & 27 deletions src/partials/shopping-list/shopping-list-main.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,50 @@ <h1 class="heading">Shopping <span class="heading-span"> List</span></h1>
<div class="books-container"></div>

<div class="pagination">
<button class="go-to-first-page">
<svg width="24" height="24">
<use href="/src/images/icons.svg#icon-slider-pages-left"></use>
</svg>
<svg width="24" height="24">
<use href="/src/images/icons.svg#icon-slider-pages-left"></use>
</svg>
</button>
<button class="prev-page">
<svg width="24" height="24">
<use href="/src/images/icons.svg#icon-slider-pages-left"></use>
</svg>
</button>
<button class="first-page">1</button>
<button class="second-page">2</button>
<button class="third-page">3</button>
<button class="morePages">...</button>
<button class="next-page">
<svg width="24" height="24">
<use href="/src/images/icons.svg#icon-slider-pages-right"></use>
</svg>
</button>
<button class="go-to-last-page">
<svg width="24" height="24">
<use href="/src/images/icons.svg#icon-slider-pages-right"></use>
</svg>
</button>
<div class="pagination-container pagination_container-back">
<button
type="button"
name="startButton"
class="pagination_btn pagination_btn--back"
aria-label="Jump to first page"
>
<svg class="pagination_icon" width="24" height="24">
<use href="./images/icons.svg#icon-slider-double-left"></use>
</svg>
</button>
<button
type="button"
name="previousButton"
class="pagination_btn pagination_btn--back"
aria-label="Previous page"
>
<svg class="pagination_icon" width="24" height="24">
<use href="./images/icons.svg#icon-slider-left"></use>
</svg>
</button>
</div>
<div class="pagination-container pagination_container-pages"></div>

<div class="pagination-container pagination_container-end">
<button
type="button"
name="nextButton"
class="pagination_btn pagination_btn--end"
aria-label="Next page"
>
<svg class="pagination_icon" width="24" height="24">
<use href="./images/icons.svg#icon-slider-right"></use>
</svg>
</button>
<button
type="button"
name="endButton"
class="pagination_btn pagination_btn--end"
aria-label="Jump to last pages"
>
<svg class="pagination_icon" width="24" height="24">
<use href="./images/icons.svg#icon-slider-double-right"></use>
</svg>
</button>
</div>
</div>
73 changes: 73 additions & 0 deletions src/style/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -1048,3 +1048,76 @@ svg.logo-icon {
#sign-up-button:not(.hidden) {
display: block;
}

/* Pagination */
.pagination {
display: flex;
margin-top: 40px;
justify-content: center;
align-items: center;
}
.pagination_container {
display: inline-flex;
justify-content: center;
align-items: center;
}

.pagination_container-back,
.pagination_container-end {
display: none;
}

.pagination_container-pages {
display: flex;
margin-left: 15px;
margin-right: 15px;
}

@media screen and (min-width: 767px) {
.pagination_container-pages {
margin-left: 24px;
margin-right: 24px;
}
}

.pagination_btn {
width: 30px;
height: 30px;
border-radius: 50px;
border: 1px solid black;
}
@media screen and (min-width: 767px) {
.pagination_btn {
width: 44px;
height: 44px;
}
}

.pagination_btn--back {
justify-content: center;
align-items: center;
margin-left: 8px;
background-color: black;
border-color: black;
fill: #b4afaf;
}

.pagination_btn--pages {
margin-left: 10px;
background-color: transparent;
border-color: black;
font-size: 18px;
line-height: 22px;
}

.pagination_btn--end {
justify-content: center;
align-items: center;
margin-left: 8px;
background-color: #eac645;
border-color: black;
}

.pagination_icon {
pointer-events: none;
}

0 comments on commit f6e8a9e

Please sign in to comment.