-
Notifications
You must be signed in to change notification settings - Fork 10
chore: Add pagination #103
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
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import { ChevronLeftIcon, ChevronRightIcon } from '@heroicons/react/20/solid'; | ||
| import clsx from 'clsx'; | ||
| import { VisuallyHidden } from 'ui'; | ||
| import { useRouter } from 'next/router'; | ||
|
|
||
| type PaginationProps = { | ||
| quantity: number; | ||
|
ssynowiec marked this conversation as resolved.
Outdated
|
||
| }; | ||
|
|
||
| export const Pagination = ({ quantity }: 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)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. shouldn't that be check for |
||
| ? Number(router.query.limit) | ||
| : 11; | ||
| const totalPages = Math.ceil(quantity / perPage); | ||
|
|
||
| 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=${currentPage - 1}`} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kinda magic numbers |
||
| 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" | ||
| > | ||
| Previous | ||
|
ssynowiec marked this conversation as resolved.
Outdated
|
||
| </a> | ||
| <a | ||
| href={`?page=${currentPage + 1}`} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yerr a wizard Harry 😸 |
||
| 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="#" | ||
| 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" | ||
|
ssynowiec marked this conversation as resolved.
Outdated
|
||
| > | ||
| Next | ||
| </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"> | ||
| {(currentPage - 1) * perPage + 1} | ||
|
ssynowiec marked this conversation as resolved.
Outdated
|
||
| </span> | ||
| - | ||
| <span className="font-medium"> | ||
| {currentPage !== totalPages ? currentPage * perPage : quantity} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's that? 😮 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is this comment? Explain your thoughts please, give others a chance to learn |
||
| </span>{' '} | ||
| z <span className="font-medium">{quantity}</span> wyników | ||
| </p> | ||
| </div> | ||
| <div> | ||
| <nav | ||
| className="isolate inline-flex -space-x-px rounded-md shadow-sm" | ||
| aria-label="Pagination" | ||
| > | ||
| {totalPages > 1 && ( | ||
| <a | ||
|
ssynowiec marked this conversation as resolved.
Outdated
|
||
| href={currentPage > 1 ? `?page=${currentPage - 1}` : ''} | ||
| className="relative inline-flex items-center rounded-l-md 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" | ||
| > | ||
| <VisuallyHidden>Poprzednia strona</VisuallyHidden> | ||
| <ChevronLeftIcon className="h-5 w-5" aria-hidden="true" /> | ||
| </a> | ||
| )} | ||
| {Array.from({ length: totalPages }, (_, i) => ( | ||
| <a | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also a component candidate |
||
| key={i} | ||
| href={`?page=${i + 1}`} | ||
| className={clsx( | ||
| i + 1 === currentPage | ||
| ? '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', | ||
| )} | ||
| > | ||
| {i + 1} | ||
| </a> | ||
| ))} | ||
| {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"> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. component |
||
| ... | ||
| </span> | ||
| )} | ||
| {totalPages > 1 && ( | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems as repeated. Maybe it would be better to make a couple different components for different pagination scenarios instead of making so many cases here. It is kinda very hard to read. |
||
| <a | ||
| href={ | ||
| currentPage <= totalPages - 1 | ||
| ? `?page=${currentPage + 1}` | ||
| : '' | ||
| } | ||
| className="relative inline-flex items-center rounded-r-md 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" | ||
| > | ||
| <VisuallyHidden>Następna strona</VisuallyHidden> | ||
| <ChevronRightIcon className="h-5 w-5" aria-hidden="true" /> | ||
| </a> | ||
| )} | ||
| </nav> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
Uh oh!
There was an error while loading. Please reload this page.