aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Pagination/Pagination.tsx
blob: 2c24a8c56f0a94f56c072e815301246533171571 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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',
              },
              { 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',
              },
              { 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',
                },
                {
                  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',
                  },
                  {
                    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',
        })}
      </h2>
      <ul className={styles.list}>
        {hasPreviousPage && getPreviousPageItem()}
        {getItems()}
        {hasNextPage && getNextPageItem()}
      </ul>
    </nav>
  );
};

export default Pagination;