-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Add pagination #103
Open
ssynowiec
wants to merge
2
commits into
Frontlive:main
Choose a base branch
from
ssynowiec:pagination
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
apps/web/components/molecules/pagginationArrow/paginationArrow.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import { VisuallyHidden } from 'ui'; | ||
import { | ||
ChevronLeftIcon as PrevPageIcon, | ||
ChevronRightIcon as NextPageIcon, | ||
} from '@heroicons/react/20/solid'; | ||
import clsx from 'clsx'; | ||
|
||
type PaginationArrowProps = { | ||
pageNumber: number; | ||
isDisabled: boolean; | ||
type: 'next' | 'prev'; | ||
}; | ||
|
||
export const PaginationArrow = ({ | ||
pageNumber, | ||
isDisabled, | ||
type, | ||
}: PaginationArrowProps) => { | ||
return ( | ||
<a | ||
href={isDisabled ? '' : `?page=${pageNumber}`} | ||
className={clsx( | ||
type === 'prev' && 'rounded-l-lg', | ||
type === 'next' && 'rounded-r-lg', | ||
'relative inline-flex items-center px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0', | ||
)} | ||
> | ||
{type === 'prev' && ( | ||
<> | ||
<VisuallyHidden>Poprzednia strona</VisuallyHidden> | ||
<PrevPageIcon className="h-5 w-5" aria-hidden="true" /> | ||
</> | ||
)} | ||
{type === 'next' && ( | ||
<> | ||
<VisuallyHidden>Następna strona</VisuallyHidden> | ||
<NextPageIcon className="h-5 w-5" aria-hidden="true" /> | ||
</> | ||
)} | ||
</a> | ||
); | ||
}; |
24 changes: 24 additions & 0 deletions
24
apps/web/components/molecules/paginationItem/paginationItem.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import clsx from 'clsx'; | ||
|
||
type PaginationItemProps = { | ||
pageNumber: number; | ||
isCurrent: boolean; | ||
}; | ||
|
||
export const PaginationItem = ({ | ||
pageNumber, | ||
isCurrent, | ||
}: PaginationItemProps) => { | ||
return ( | ||
<a | ||
href={`?page=${pageNumber}`} | ||
className={clsx( | ||
isCurrent | ||
? 'relative z-10 inline-flex items-center bg-blue-600 px-4 py-2 text-sm font-semibold text-white focus:z-20 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600' | ||
: 'relative hidden items-center px-4 py-2 text-sm font-semibold text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 md:inline-flex', | ||
)} | ||
> | ||
{pageNumber} | ||
</a> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { useRouter } from 'next/router'; | ||
import { PaginationItem } from 'molecules/paginationItem/paginationItem'; | ||
import { PaginationArrow } from 'molecules/pagginationArrow/paginationArrow'; | ||
|
||
type PaginationProps = { | ||
numberOfItems: number; | ||
}; | ||
|
||
export const Pagination = ({ numberOfItems }: PaginationProps) => { | ||
const router = useRouter(); | ||
const currentPage: number = | ||
router.query.page && !isNaN(Number(router.query.page)) | ||
? Number(router.query.page) | ||
: 1; | ||
const perPage = | ||
router.query.page && !isNaN(Number(router.query.limit)) | ||
? Number(router.query.limit) | ||
: 11; | ||
const totalPages = Math.ceil(numberOfItems / perPage); | ||
|
||
const prevPageNumber = currentPage - 1; | ||
const nextPageNumber = currentPage + 1; | ||
const startNumberOfItemsOnCurrentPage = (currentPage - 1) * perPage + 1; | ||
const endNumberOfItemsOnCurrentPage = | ||
currentPage !== totalPages ? currentPage * perPage : numberOfItems; | ||
|
||
return ( | ||
<div className="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6"> | ||
<div className="flex flex-1 justify-between sm:hidden"> | ||
<a | ||
href={`?page=${prevPageNumber}`} | ||
className="relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50" | ||
> | ||
Poprzednia | ||
</a> | ||
<a | ||
href={`?page=${nextPageNumber}`} | ||
className="relative z-10 inline-flex items-center bg-blue-600 px-4 py-2 text-sm font-semibold text-white focus:z-20 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600" | ||
> | ||
{currentPage} | ||
</a> | ||
<a | ||
href="apps/web/components/organisms/pagination#" | ||
className="relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50" | ||
> | ||
Następna | ||
</a> | ||
</div> | ||
<div className="hidden sm:flex sm:flex-1 sm:items-center sm:justify-between"> | ||
<div> | ||
<p className="text-sm text-gray-700"> | ||
<span className="font-medium"> | ||
{startNumberOfItemsOnCurrentPage} | ||
</span> | ||
- | ||
<span className="font-medium">{endNumberOfItemsOnCurrentPage}</span>{' '} | ||
z <span className="font-medium">{numberOfItems}</span> wyników | ||
</p> | ||
</div> | ||
<div> | ||
<nav | ||
className="isolate inline-flex -space-x-px rounded-md shadow-sm" | ||
aria-label="Pagination" | ||
> | ||
<PaginationArrow | ||
pageNumber={prevPageNumber} | ||
isDisabled={currentPage === 1} | ||
type="prev" | ||
/> | ||
{Array.from({ length: totalPages }, (_, i) => ( | ||
<PaginationItem | ||
key={i} | ||
pageNumber={i + 1} | ||
isCurrent={i + 1 === currentPage} | ||
/> | ||
))} | ||
{totalPages > 5 && ( | ||
<span className="relative inline-flex items-center px-4 py-2 text-sm font-semibold text-gray-700 ring-1 ring-inset ring-gray-300 focus:outline-offset-0"> | ||
... | ||
</span> | ||
)} | ||
<PaginationArrow | ||
pageNumber={nextPageNumber} | ||
isDisabled={currentPage === totalPages} | ||
type="next" | ||
/> | ||
</nav> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is this number "11" ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could be moved to constant
SOMETHING_NUMBER
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's default quantity of items per page