Skip to content
Open
Show file tree
Hide file tree
Changes from 2 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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Here is the [working version](https://mate-academy.github.io/react_pagination/)

You a given a list of items and markup for the `Pagination`. Implement the
You a given a list of items and markup for the `Pagination`. Implement the
`Pagination` as a stateless component to show only the items for a current page.

1. The `Pagination` should be used with the next props:
Expand Down Expand Up @@ -32,4 +32,4 @@ You a given a list of items and markup for the `Pagination`. Implement the
- Implement a solution following the [React task guideline](https://github.com/mate-academy/react_task-guideline#react-tasks-guideline).
- Use the [React TypeScript cheat sheet](https://mate-academy.github.io/fe-program/js/extra/react-typescript).
- Open one more terminal and run tests with `npm test` to ensure your solution is correct.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://<your_account>.github.io/react_pagination/) and add it to the PR description.
- Replace `<your_account>` with your Github username in the [DEMO LINK](https://Nika-Andriy.github.io/react_pagination/) and add it to the PR description.
131 changes: 57 additions & 74 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,67 @@
import React from 'react';
import './App.css';
import { getNumbers } from './utils';
import { Pagination } from './components/Pagination';
import { useSearchParams } from 'react-router-dom';

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

export const App: React.FC = () => {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This component is missing the requirement to integrate with React Router. The task specifies that page and perPage should be stored in the URL as query parameters (e.g., ?page=2&perPage=7) and used to initialize the state when the page loads.

const [searchParams, setSearchParams] = useSearchParams();

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

Choose a reason for hiding this comment

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

better to have validation - for now, can enter ?perPage=9999


const startIndex = (currentPage - 1) * perPage;
const visibleItems = items.slice(startIndex, startIndex + perPage);
Comment on lines +16 to +17
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

potential bug here, if currentPage > max pages → the array will be empty, need clamp:

const totalPages = Math.ceil(total / perPage);
const safePage = Math.min(currentPage, totalPages);


const updateParams = (params: {
page?: string | number;
perPage?: string | number;
}) => {
const newParams = new URLSearchParams(searchParams);

Object.entries(params).forEach(([key, value]) => {
if (value === null) {
newParams.delete(key);
} else {
newParams.set(key, value.toString());
}
});

setSearchParams(newParams);
};

const handlePerPageChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
// При зміні perPage завжди скидаємо на 1 сторінку за умовою
updateParams({
perPage: event.target.value,
page: 1,
});
};

const handlePageChange = (page: number) => {
updateParams({ page });
};

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} - ${Math.min(currentPage * perPage, total)} 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">
className="form-control"
value={perPage}
onChange={handlePerPageChange}
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -32,78 +74,19 @@ 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={total}
perPage={perPage}
currentPage={currentPage}
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 => (
<li key={item} data-cy="item">
{item}
</li>
))}
</ul>
</div>
);
Expand Down
75 changes: 74 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,74 @@
export const Pagination = () => {};
type Props = {
total: number;
perPage: number;
currentPage?: number;
onPageChange: (page: number) => void;
};

export const Pagination: React.FC<Props> = ({
total,
perPage,
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

no validation — user can pass any value via URL.

currentPage = 1,
onPageChange,
}) => {
const totalPages = Math.ceil(total / perPage);

// Використовуй цю функцію всюди, де є зміна сторінки
const handlePageClick = (e: React.MouseEvent, newPage: number) => {
e.preventDefault();
if (newPage !== currentPage && newPage >= 1 && newPage <= totalPages) {
onPageChange(newPage);
}
};

const pages = Array.from({ length: totalPages }, (_, i) => i + 1);

return (
<ul className="pagination">
{/* Кнопка « */}
<li className={`page-item ${currentPage === 1 ? 'disabled' : ''}`}>
<a
data-cy="prevLink"
className="page-link"
href="#prev"
aria-disabled={currentPage === 1}
onClick={e => handlePageClick(e, currentPage - 1)}
>
«
</a>
</li>

{/* Список сторінок */}
{pages.map(page => (
<li
key={page}
className={`page-item ${currentPage === page ? 'active' : ''}`}
>
<a
data-cy="pageLink"
className="page-link"
href={`#${page}`}
onClick={e => handlePageClick(e, page)}
>
{page}
</a>
</li>
))}

{/* Кнопка » */}
<li
className={`page-item ${currentPage === totalPages ? 'disabled' : ''}`}
>
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={currentPage === totalPages}
onClick={e => handlePageClick(e, 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';

import { HashRouter as Router } from 'react-router-dom';
import { App } from './App';

createRoot(document.getElementById('root') as HTMLElement).render(<App />);
const container = document.getElementById('root') as HTMLElement;

createRoot(container).render(
<Router>
<App />
</Router>,
);
Loading