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
54 changes: 49 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
"classnames": "^2.5.1",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router": "^7.18.0",
"react-transition-group": "^4.4.5"
},
"devDependencies": {
"@cypress/react18": "^2.0.1",
"@mate-academy/scripts": "^1.9.12",
"@mate-academy/scripts": "^2.1.3",
"@mate-academy/students-ts-config": "*",
"@mate-academy/stylelint-config": "*",
"@types/node": "^20.14.10",
Expand Down
131 changes: 52 additions & 79 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
import React from 'react';
// eslint-disable-next-line import/no-extraneous-dependencies
import { useSearchParams } from 'react-router';
import './App.css';
import { Pagination } from './components/Pagination';
import { getNumbers } from './utils';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const items = getNumbers(1, 42).map(n => `Item ${n}`);
enum PerPage {
Comment on lines +5 to +8

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The Props interface marks currentPage as required, but the task states currentPage is optional with a default of 1. Make this prop optional (currentPage?: number) to match the requirements and your default parameter value.

THREE = 3,
FIVE = 5,
TEN = 10,
TWENTY = 20,
}

const items: string[] = getNumbers(1, 42).map(n => `Item ${n}`);

export const App: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();
const page = Number(searchParams.get('page')) || 1;
const perPage = Number(searchParams.get('perPage')) || PerPage.FIVE;

const startIndex = (page - 1) * perPage;
const endIndex = Math.min(startIndex + perPage, items.length);
const visibleItems = items.slice(startIndex, endIndex);

const handlePageChange = (newPage: number) => {
searchParams.set('page', newPage.toString());
setSearchParams(searchParams);
};

const handlePerPageChange = (e: React.ChangeEvent<HTMLSelectElement>) => {
searchParams.set('perPage', e.target.value);
searchParams.set('page', '1');
setSearchParams(searchParams);
};

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

<p className="lead" data-cy="info">
Page 1 (items 1 - 5 of 42)
Page {page} (items {startIndex + 1} - {endIndex} of {items.length})
</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={perPage}
onChange={handlePerPageChange}
>
<option value={PerPage.THREE}>{PerPage.THREE}</option>
<option value={PerPage.FIVE}>{PerPage.FIVE}</option>
<option value={PerPage.TEN}>{PerPage.TEN}</option>
<option value={PerPage.TWENTY}>{PerPage.TWENTY}</option>
</select>
</div>

Expand All @@ -32,78 +63,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={page}
onPageChange={handlePageChange}
/>
<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>
{visibleItems.map(item => {
return (
<li key={item} data-cy="item">
{item}
</li>
);
})}
</ul>
</div>
);
Expand Down
90 changes: 89 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,89 @@
export const Pagination = () => {};
import cn from 'classnames';
import React from 'react';
interface Props {
total: number;
perPage: number;
currentPage?: number;
onPageChange: (selectedPage: number) => void;
}

export const Pagination: React.FC<Props> = ({
total,
perPage = 5,
currentPage = 1,
onPageChange,
}) => {
const totalPages: number = Math.ceil(total / perPage);
const pages: number[] = Array.from({ length: totalPages }, (_, i) => i + 1);

return (
<ul className="pagination">
<li
className={cn('page-item', {
disabled: currentPage <= 1,
})}
>
<a
data-cy="prevLink"
className={cn('page-link')}
href="#prev"
aria-disabled={currentPage === 1}
onClick={e => {
e.preventDefault();
if (currentPage > 1) {
onPageChange(currentPage - 1);
}
}}
>
«
</a>
</li>
{pages.map(num => {
return (
<li
key={num}
className={cn('page-item', {
active: currentPage === num,
})}
>
<a
data-cy="pageLink"
className={cn('page-link', {
active: currentPage === num,
})}
href={`#${num}`}
onClick={e => {
e.preventDefault();
if (num !== currentPage) {
onPageChange(num);
}
}}
>
{num}
</a>
</li>
);
})}
Comment on lines +16 to +66

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The App manages pagination state correctly, but there is no use of React Router to sync page and perPage with the URL query parameters as required (e.g. ?page=2&perPage=7). You need to integrate React Router hooks here to read initial values from the URL on load and update the URL when page or perPage change.

<li
className={cn('page-item', {
disabled: currentPage === totalPages,
})}
>
<a
data-cy="nextLink"
className={cn('page-link')}
href="#next"
aria-disabled={currentPage === totalPages}
onClick={e => {
e.preventDefault();
if (currentPage < totalPages) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
);
};
8 changes: 7 additions & 1 deletion 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';
import { App } from './App';

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