Skip to content
Open

done #1519

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
67 changes: 63 additions & 4 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-dom": "^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
116 changes: 40 additions & 76 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,48 @@
import React from 'react';
import './App.css';
import { getNumbers } from './utils';
import { useSearchParams } from 'react-router-dom';
import Pagination from './components/Pagination/Pagination';

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

currentPage is required in Props, but the requirements specify it must be optional and default to 1 if not provided (checklist items #2 and #20). Consider making currentPage optional in the type (e.g. currentPage?: number) and providing a default value of 1 in the component destructuring.

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

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

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

const total = items.length;

const startIndex = (currentPage - 1) * perPage;
const endIndex = startIndex + perPage;

const visibleItems = items.slice(startIndex, endIndex);

const updateParams = (page: number, perPageValue: number) => {
setSearchParams({
page: String(page),
perPage: String(perPageValue),
});
};

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 {total === 0 ? 0 : startIndex + 1} -{' '}
{Math.min(endIndex, 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={e => updateParams(1, Number(e.target.value))}
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -32,78 +55,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={page => updateParams(page, perPage)}
/>

<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, i) => (
<li key={i} data-cy="item">
{item}
</li>
))}
</ul>
</div>
);
Expand Down
85 changes: 84 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,84 @@
export const Pagination = () => {};
import React from 'react';

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

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

return (
<ul className="pagination">
{/* PREV */}
<li className={`page-item ${currentPage === 1 ? 'disabled' : ''}`}>

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's better to use classNames here

<a
data-cy="prevLink"
className="page-link"
href="#prev"
aria-disabled={currentPage === 1}
onClick={e => {
e.preventDefault();
if (currentPage > 1) {
onPageChange(currentPage - 1);
}
}}
>
«
</a>
</li>

{/* PAGES */}
{pages.map(page => (
<li
key={page}
className={`page-item ${currentPage === page ? 'active' : ''}`}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's better to use classNames here

>
<a
data-cy="pageLink"
className="page-link"
href={`#${page}`}
onClick={e => {
e.preventDefault();
if (page !== currentPage) {
onPageChange(page);
}
}}
>
{page}
</a>
</li>
))}

{/* NEXT */}
<li
className={`page-item ${currentPage === totalPages ? 'disabled' : ''}`}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It's better to use classNames here

>
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={currentPage === totalPages}
onClick={e => {
e.preventDefault();
if (currentPage < totalPages) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
);
};

export default Pagination;
12 changes: 8 additions & 4 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { createRoot } from 'react-dom/client';
import ReactDOM from 'react-dom/client';
import { BrowserRouter } from 'react-router-dom';
import App from './App';

import { App } from './App';

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