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
124 changes: 48 additions & 76 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
import React from 'react';
import './App.css';
import { getNumbers } from './utils';

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

export const App: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();

const currentPage = Number(searchParams.get('page')) || 1;
const perPage = Number(searchParams.get('perPage')) || 5;

function onPageChange(newPage: number): void {
const params = new URLSearchParams(searchParams);

params.set('page', newPage.toString());
setSearchParams(params);
}

function onPerPageChange(newPerPage: number): void {
const params = new URLSearchParams(searchParams);

params.set('perPage', newPerPage.toString());
params.set('page', '1');
setSearchParams(params);
}

const startIndex = (currentPage - 1) * perPage;
const calculatedEndIndex = startIndex + perPage;
const endIndex = Math.min(calculatedEndIndex, items.length);

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 ${startIndex + 1} - ${endIndex} of ${items.length})`}
</p>

{/* зміна items на сторінці */}
<div className="form-group row">
<div className="col-3 col-sm-2 col-xl-1">
<select
data-cy="perPageSelector"
id="perPageSelector"
className="form-control">
className="form-control"
onChange={e => {
onPerPageChange(Number(e.target.value));
}}
Comment on lines 48 to 50

Choose a reason for hiding this comment

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

This handler should also be updated to modify the URL search parameters instead of calling setPerPage and setCurrentPage. When the user selects a new value, you should update the perPage parameter in the URL and reset the page parameter to 1.

value={perPage}
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -32,78 +62,20 @@ export const App: React.FC = () => {
</label>
</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={items.length}
perPage={perPage}
currentPage={currentPage}
onPageChange={onPageChange}
/>

{/*Items - список який відображаєтьс на сторінці, змінюється в залежності від вибраної кількості*/}
<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>
{items.slice(startIndex, endIndex).map(item => (
<li key={item} data-cy="item">
{item}
</li>
))}
</ul>
</div>
);
Expand Down
71 changes: 70 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,70 @@
export const Pagination = () => {};
import { pagesArray } from '../../utils';
import { Link } from 'react-router-dom';

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

// total - загальна кількісьть елементів
// perPage - кількість елементів на сторінці
// currentPage = 1 - //кількість сторінок яка може бути

export const Pagination = ({
total,
perPage,
currentPage = 1,
}: PaginationType) => {
const pagesAmount = Math.ceil(total / perPage);
const pagesCountArray: number[] = pagesArray(pagesAmount);

return (
<ul className="pagination">
<li className={currentPage === 1 ? 'page-item disabled' : 'page-item'}>
<Link
to={`/?page=${currentPage - 1}&perPage=${perPage}`}
data-cy="prevLink"
className="page-link"
aria-disabled={currentPage === 1 ? 'true' : 'false'}
>
«
</Link>
</li>

{pagesCountArray.map(page => (
<li
className={page === currentPage ? 'page-item active' : 'page-item'}
key={page}
>
<Link
to={`/?page=${page}&perPage=${perPage}`}
data-cy="pageLink"
className="page-link"
>
{page}
</Link>
</li>
))}
<li
className={
currentPage === pagesCountArray.length
? 'page-item disabled'
: 'page-item'
}
>
<Link
to={`/?page=${currentPage < pagesAmount ? currentPage + 1 : pagesAmount}&perPage=${perPage}`}
data-cy="nextLink"
className="page-link"
aria-disabled={
currentPage === pagesCountArray.length ? 'true' : 'false'
}
>
»
</Link>
</li>
</ul>
);
};
7 changes: 6 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { createRoot } from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';

import { App } from './App';

createRoot(document.getElementById('root') as HTMLElement).render(<App />);
createRoot(document.getElementById('root') as HTMLElement).render(
<BrowserRouter>
<App />
</BrowserRouter>,
);
10 changes: 10 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,13 @@ export function getNumbers(from: number, to: number): number[] {

return numbers;
}

export function pagesArray(number: number): number[] {
const pages = [];

for (let i = 1; i <= number; i++) {
pages.push(i);
}

return pages;
}
Loading