Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 52 additions & 79 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,59 @@
/* eslint-disable import/no-extraneous-dependencies */
import React from 'react';
import { useEffect, useState } from 'react';
import { useSearchParams } from 'react-router-dom';
import './App.css';
import { getNumbers } from './utils';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const items = getNumbers(1, 42).map(n => `Item ${n}`);
import { Pagination } from './components/Pagination';

export const App: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();
// читаємо з URL
const pageFromUrl = Number(searchParams.get('page'));
const perPageFromUrl = Number(searchParams.get('perPage'));

const [pegeAmount, setAmount] = useState(
perPageFromUrl > 0 ? perPageFromUrl : 5,
);
const [currentPage, setCurrent] = useState(pageFromUrl > 0 ? pageFromUrl : 1);

// синхронізуємо стан URL
useEffect(() => {
setSearchParams({
page: String(currentPage),
perPage: String(pegeAmount),
});
}, [currentPage, pegeAmount, setSearchParams]);

const total = 42;
const pages = Math.ceil(total / pegeAmount);
const end = currentPage !== pages ? pegeAmount * currentPage : total;
const start = pegeAmount * (currentPage - 1) + 1;

return (
<div className="container">
<h1>Items with Pagination</h1>

<p className="lead" data-cy="info">
Page 1 (items 1 - 5 of 42)
Page {currentPage} (items {start} - {end} of {total})
</p>

<div className="form-group row">
<div className="col-3 col-sm-2 col-xl-1">
<select
data-cy="perPageSelector"
id="perPageSelector"
className="form-control">
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
<option value="20">20</option>
className="form-control"
value={pegeAmount}
onChange={e => {
setAmount(Number(e.target.value));
setCurrent(1);
}}
Comment on lines +48 to +51

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When perPage changes you reset the page to 1 (good), but you also need to update the URL query params here so the new perPage and page=1 are reflected in the address bar (use the Router navigation hook).

>
<option value={3}>3</option>
<option value={5}>5</option>
<option value={10}>10</option>
<option value={20}>20</option>
</select>
</div>

Expand All @@ -33,77 +63,20 @@ export const App: React.FC = () => {
</div>

{/* Move this markup to Pagination */}
<ul className="pagination">
<li className="page-item disabled">
<a
data-cy="prevLink"
className="page-link"
href="#prev"
aria-disabled="true">
«
</a>
</li>
<li className="page-item active">
<a data-cy="pageLink" className="page-link" href="#1">
1
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#2">
2
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#3">
3
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#4">
4
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#5">
5
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#6">
6
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#7">
7
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#8">
8
</a>
</li>
<li className="page-item">
<a data-cy="pageLink" className="page-link" href="#9">
9
</a>
</li>
<li className="page-item">
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled="false">
»
</a>
</li>
</ul>
<Pagination
total={total} // total number of items to paginate
perPage={pegeAmount} // number of items per page
currentPage={currentPage} /* optional with 1 by default */
onPageChange={newPege => {
setCurrent(newPege);
}}
Comment on lines +70 to +72

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When onPageChange sets the new page you update state correctly — also update the URL query params here so ?page stays in sync with the selected page (use the Router navigation hook).

/>
<ul>
<li data-cy="item">Item 1</li>
<li data-cy="item">Item 2</li>
<li data-cy="item">Item 3</li>
<li data-cy="item">Item 4</li>
<li data-cy="item">Item 5</li>
{getNumbers(start, end).map(n => (
<li data-cy="item" key={n}>
Item {n}
</li>
))}
</ul>
</div>
);
Expand Down
78 changes: 77 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
export const Pagination = () => {};
import React from 'react';
import { getNumbers } from '../../utils';

interface Props {
total: number;
perPage: number;
currentPage?: number;
onPageChange: (pege: number) => void;
}

export const Pagination: React.FC<Props> = ({
total,
perPage,
currentPage = 1,
onPageChange,
}) => {
const pages = Math.ceil(total / perPage);

return (
<ul className="pagination">
<li className={currentPage === 1 ? 'page-item disabled' : 'page-item'}>
<a
data-cy="prevLink"
className="page-link"
href="#prev"
aria-disabled={currentPage === 1 ? 'true' : 'false'}
onClick={e => {
e.preventDefault();
if (currentPage > 1) {
onPageChange(currentPage - 1);
}
}}
>
«
</a>
</li>
{getNumbers(1, pages).map(num => (
<li
className={`page-item ${currentPage === num ? 'active' : ''}`}
key={num}
>
<a
data-cy="pageLink"
className="page-link"
href={`#${num}`}
onClick={e => {
e.preventDefault();
if (currentPage !== num) {
onPageChange(num);
}
}}
>
{num}
</a>
</li>
))}
<li
className={currentPage === pages ? 'page-item disabled' : 'page-item'}
>
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={currentPage === pages ? 'true' : 'false'}
onClick={e => {
e.preventDefault();
if (currentPage < pages) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
);
};
10 changes: 8 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { createRoot } from 'react-dom/client';

// eslint-disable-next-line import/no-extraneous-dependencies
import { BrowserRouter } from 'react-router-dom';
import { App } from './App';
import React from 'react';

createRoot(document.getElementById('root') as HTMLElement).render(<App />);
createRoot(document.getElementById('root') as HTMLElement).render(
<BrowserRouter>
<App />
</BrowserRouter>,
);
Loading