diff options
| author | Armand Philippot <git@armandphilippot.com> | 2022-05-24 19:35:12 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-05-24 19:35:12 +0200 |
| commit | c85ab5ad43ccf52881ee224672c41ec30021cf48 (patch) | |
| tree | 8058808d9bfca19383f120c46b34d99ff2f89f63 /src/components/Pagination/Pagination.tsx | |
| parent | 52404177c07a2aab7fc894362fb3060dff2431a0 (diff) | |
| parent | 11b9de44a4b2f305a6a484187805e429b2767118 (diff) | |
refactor: use storybook and atomic design (#16)
BREAKING CHANGE: rewrite most of the Typescript types, so the content format (the meta in particular) needs to be updated.
Diffstat (limited to 'src/components/Pagination/Pagination.tsx')
| -rw-r--r-- | src/components/Pagination/Pagination.tsx | 136 |
1 files changed, 0 insertions, 136 deletions
diff --git a/src/components/Pagination/Pagination.tsx b/src/components/Pagination/Pagination.tsx deleted file mode 100644 index 55c366a..0000000 --- a/src/components/Pagination/Pagination.tsx +++ /dev/null @@ -1,136 +0,0 @@ -import { settings } from '@utils/config'; -import Link from 'next/link'; -import { useRouter } from 'next/router'; -import { useIntl } from 'react-intl'; -import styles from './Pagination.module.scss'; - -const Pagination = ({ baseUrl, total }: { baseUrl: string; total: number }) => { - const intl = useIntl(); - const { asPath } = useRouter(); - const totalPages = Math.floor(total / settings.postsPerPage); - const currentPage = asPath.includes('/page/') - ? Number(asPath.split(`${baseUrl}/page/`)[1]) - : 1; - const hasPreviousPage = currentPage !== 1; - const hasNextPage = currentPage !== totalPages; - - const getPreviousPageItem = () => { - return ( - <li className={styles.item}> - <Link href={`${baseUrl}/page/${currentPage - 1}`}> - <a className={styles.link}> - {intl.formatMessage( - { - defaultMessage: '{icon} Previous page', - description: 'Pagination: previous page link', - id: 'aMFqPH', - }, - { icon: '←' } - )} - </a> - </Link> - </li> - ); - }; - - const getNextPageItem = () => { - return ( - <li className={styles.item}> - <Link href={`${baseUrl}/page/${currentPage + 1}`}> - <a className={styles.link}> - {intl.formatMessage( - { - defaultMessage: 'Next page {icon}', - description: 'Pagination: Next page link', - id: 'R4yaW6', - }, - { icon: '→' } - )} - </a> - </Link> - </li> - ); - }; - - const getPages = () => { - const pages = []; - for (let i = 1; i <= totalPages; i++) { - if (i === currentPage) { - pages.push({ - id: `page-${i}`, - link: ( - <span className={`${styles.link} ${styles['link--current']}`}> - {intl.formatMessage( - { - defaultMessage: '<a11y>Page </a11y>{number}', - description: 'Pagination: page number', - id: 'TSXPzr', - }, - { - number: i, - a11y: (chunks: string) => ( - <span className="screen-reader-text">{chunks}</span> - ), - } - )} - </span> - ), - }); - } else { - pages.push({ - id: `page-${i}`, - link: ( - <Link href={`${baseUrl}/page/${i}`}> - <a className={styles.link}> - {intl.formatMessage( - { - defaultMessage: '<a11y>Page </a11y>{number}', - description: 'Pagination: page number', - id: 'TSXPzr', - }, - { - number: i, - a11y: (chunks: string) => ( - <span className="screen-reader-text">{chunks}</span> - ), - } - )} - </a> - </Link> - ), - }); - } - } - - return pages; - }; - - const getItems = () => { - const pages = getPages(); - - return pages.map((page) => ( - <li key={page.id} className={styles.item}> - {page.link} - </li> - )); - }; - - return ( - <nav className={styles.wrapper} aria-labelledby="pagination-title"> - <h2 id="pagination-title" className="screen-reader-text"> - {intl.formatMessage({ - defaultMessage: 'Pagination', - description: 'Pagination: pagination title', - id: 'BAkq7J', - })} - </h2> - <ul className={styles.list}> - {hasPreviousPage && getPreviousPageItem()} - {getItems()} - {hasNextPage && getNextPageItem()} - </ul> - </nav> - ); -}; - -export default Pagination; |
