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

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
"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
124 changes: 50 additions & 74 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,58 @@
import React from 'react';
import React, { useState } from 'react';
import './App.css';
import { getNumbers } from './utils';
import { Pagination } from './components/Pagination';

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

interface RenderingPages {
partOfPage: string[];
before: number;
to: number;
}

export const App: React.FC = () => {
const [count, setCount] = useState(1);
const [pages, setPages] = useState(5);

const minPage = count * pages - (pages - 1);
const maxPage = count * pages > items.length ? items.length : count * pages;

const handlePerPageChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
setPages(+event.target.value);
setCount(1);
};

const pageChange = (num: number) => setCount(num);

const renderPerPage = ({ partOfPage, before, to }: RenderingPages) => {
const slicedItems = partOfPage.slice(before, to);

Comment on lines +23 to +31

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

onPageChange(currentPage - 1) is executed even when currentPage === 1. According to checklist items #5, #7 and #9, the prev link must be disabled on the first page and the callback should only fire when the page actually changes. Add a guard so that when currentPage === 1 the handler does nothing and does not call onPageChange.

return slicedItems.map(item => (
<li data-cy="item" key={item}>
{item}
</li>
));
};

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

<p className="lead" data-cy="info">
Page 1 (items 1 - 5 of 42)
Page {count} (items {minPage} - {maxPage} of 42)
</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"
onChange={handlePerPageChange}
value={pages}
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -32,78 +65,21 @@ 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={pages}
currentPage={count}
onPageChange={page => {
pageChange(page);
}}
/>

<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>
{renderPerPage({
partOfPage: items,
before: minPage - 1,
to: maxPage,
})}
</ul>
</div>
);
Expand Down
87 changes: 86 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,86 @@
export const Pagination = () => {};
import React from 'react';
import cn from 'classnames';
import { getNumbers } from '../../utils';

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

export const Pagination = ({
Comment on lines +9 to +12

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 RenderingPages interface defines a property item, but the renderPerPage function uses partOfPage in its parameter destructuring, which will cause a TypeScript error. Align the interface and destructuring (use the same property name).

total,
perPage,
currentPage = 1,
onPageChange,
}: PaginationType) => {
const pages = getNumbers(1, Math.ceil(total / perPage));

return (
<>
<ul className="pagination">
<li className={cn(`page-item ${currentPage === 1 ? `disabled` : ``}`)}>
<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.map(page => (
<li
key={page}
className={cn(`page-item ${currentPage === page ? 'active' : ''}`)}
>
<a
data-cy="pageLink"
className="page-link"
href={`#${page}`}
onClick={e => {
e.preventDefault();

if (page !== currentPage) {
onPageChange(page);
}
}}
>
{page}
</a>
</li>
))}

<li
className={cn(
`page-item ${currentPage === pages.length ? `disabled` : ``}`,
)}
>
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={currentPage === pages.length}
onClick={e => {
e.preventDefault();

if (currentPage < pages.length) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
</>
);
};
Loading