Pagination.tsx 4.15 KB
Newer Older
Matija Obreza's avatar
Matija Obreza committed
1
2
3
import React from 'react';
import FilteredPage, { IPageRequest } from '@genesys/client/model/FilteredPage';
import { history } from './App';
4
import {withTranslation, WithTranslation} from "react-i18next";
Matija Obreza's avatar
Matija Obreza committed
5

6
interface IPagination extends React.ClassAttributes<any>, WithTranslation {
Matija Obreza's avatar
Matija Obreza committed
7
8
9
10
11
  loadData: (filter: Record<string, any>, pageR: IPageRequest) => Promise<any>;
  paged: FilteredPage<any>;
  children: React.ReactNode;
}

12
class Pagination extends React.Component<IPagination> {
Matija Obreza's avatar
Matija Obreza committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  public componentDidMount() {
    const { paged } = this.props;
    if (paged) {
      this.updateRoute(paged);
    }
  }

  public componentDidUpdate(prevProps: IPagination) {
    const { paged } = this.props;
    if ((paged && !prevProps.paged && paged.filterCode) || paged.filterCode !== prevProps.paged.filterCode) {
      this.updateRoute(paged);
    }
  }

  private toPage = (e: React.MouseEvent) => {
28
    e.preventDefault();
Matija Obreza's avatar
Matija Obreza committed
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
    const { loadData, paged } = this.props;
    const page = +(e.currentTarget as HTMLElement).dataset.page;

    loadData(paged.filter, this.makePageRequest(page))
      .then((data: FilteredPage<any>) => {
        this.updateRoute(data);
      });
  };

  private updateRoute = (page: FilteredPage<any>) => {
    // console.log('updateRoute', page);
    history.push(`/?filter=${page.filterCode}&p=${page.number}`);
  };

  private makePageRequest = (page: number): IPageRequest => {
    const { paged } = this.props;

    return { ...FilteredPage.nextPage(paged), ...{ page } };
  };

  private getPageNumbers = (): number[] => {
    const { paged: { number: current, totalPages, last } } = this.props;
    let base = [-2, -1, 0, 1, 2];

    if (totalPages < 5) {
      switch (totalPages) {
        case 4: {
          base.unshift();
          break;
        }
        case 3: {
          base = base.slice(1, 4);
          break;
        }
        case 2: {
          base = base.slice(2, 4);
          break;
        }
        default: {
          base = [0];
        }
      }
    }

    if (current === 0 || current === 1) {
      return base.map((page, i) => i + 1);
    }
    if (last || current === totalPages - 2) {
      return base.map((page, i) => current + page + (last ? -1 : 0));
    }
    return base.map((page) => current + page + 1);
  };

  private renderPagination = () => {
83
    const { paged, t } = this.props;
Matija Obreza's avatar
Matija Obreza committed
84
85

    return (
Maksym Tishchenko's avatar
Maksym Tishchenko committed
86
87
88
89
90
91
92
93
94
95
96
97
98
      <nav aria-label="Page navigation">
        <ul className="pagination">
          <li className={`page-item ${paged.number === 0 ? "disabled" : ""}`}>
            <a className="page-link" onClick={ this.toPage } data-page={ 0 } href="#" aria-label={t('pag.first')}>
              <span aria-hidden="true">&laquo; {t('pag.first')}</span>
            </a>
          </li>
          <li className={`page-item ${paged.number === 0 ? "disabled" : ""}`}>
            <a onClick={ this.toPage } data-page={ paged.number - 1 } href="#" className="page-link">&lsaquo; {t('pag.prev')}</a>
          </li>
          { this.getPageNumbers().map((page, i) => (
            <li key={ `page-${page}-${i}` } data-page={ page - 1 } onClick={ this.toPage } aria-current="page" className={`page-item ${paged.number === page - 1 ? "active" : ""}`}>
              {paged.number === page - 1
99
                ? <span tabIndex={-1} className="page-link">{page} <span className="sr-only">(current)</span></span>
Maksym Tishchenko's avatar
Maksym Tishchenko committed
100
101
102
103
104
105
106
                : <a className="page-link" href="#">{page}</a>
              }
            </li>
          )) }

          <li className={`page-item ${paged.last ? "disabled" : ""}`}>
            <a onClick={ this.toPage } data-page={ paged.number + 1 } href="#" className="page-link">{t('pag.next')} &rsaquo;</a>
107
          </li>
Maksym Tishchenko's avatar
Maksym Tishchenko committed
108
109
110
111
112
113
114
115
          <li className={`page-item ${paged.last ? "disabled" : ""}`}>
            <a className="page-link" onClick={ this.toPage } data-page={ paged.totalPages - 1 } href="#" aria-label="{t('pag.last')}">
              <span aria-hidden="true">{t('pag.last')} &raquo;</span>
            </a>
          </li>
        </ul>
      </nav>
    );
Matija Obreza's avatar
Matija Obreza committed
116
117
118
119
120
121
122
123
124
125
126
127
  }

  public render() {
    return (
      <>
        { this.renderPagination() }
        { this.props.children }
        { this.renderPagination() }
      </>
    );
  };
}
128
129

export default withTranslation()(Pagination)