Skip to content
Closed
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
5 changes: 2 additions & 3 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 All @@ -25,11 +25,10 @@ You a given a list of items and markup for the `Pagination`. Implement the
1. Implement the `<select data-cy="perPageSelector">` with `3`, `5`, `10`, `20` options to change the `perPage`;
- show the 1st page after changing a `perPage`;
1. (*) Use React Router to save `?page=2&perPage=7` in the URL and apply them on page load

## Instructions

- Install Prettier Extention and use this [VSCode settings](https://mate-academy.github.io/fe-program/tools/vscode/settings.json) to enable format on save.
- 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://NemH.github.io/react_pagination/) and add it to the PR description.
114 changes: 40 additions & 74 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,50 @@
import React from 'react';
import './App.css';
import React, { useEffect } from 'react';
import { getNumbers } from './utils';
import { useSearchParams } from 'react-router-dom';
import { Pagination } from './components/Pagination';
import { useState } from 'react';

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

export const App: React.FC = () => {
const [searchParams, setSearchParams] = useSearchParams();
const pageFromUrl = Number(searchParams.get('page')) || 1;
const perPageFromUrl = Number(searchParams.get('perPage')) || 5;
const [perPage, setPerPage] = useState(perPageFromUrl);
const [currentPage, setCurrentPage] = useState(pageFromUrl);

useEffect(() => {
setSearchParams({ page: String(currentPage), perPage: String(perPage) });
}, [currentPage, perPage, setSearchParams]);

const onPageChange = (page: number) => {
setCurrentPage(page);
};

const start = (currentPage - 1) * perPage;
const end = Math.min(start + (perPage - 1), items.length - 1);

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 {start + 1} - {end + 1} 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">
className="form-control"
value={perPage}
onChange={event => {
setPerPage(Number(event.target.value));
setCurrentPage(1);
}}
>
<option value="3">3</option>
<option value="5">5</option>
<option value="10">10</option>
Expand All @@ -33,77 +58,18 @@ export const App: React.FC = () => {
</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}
/>
<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(start, end + 1).map(item => (
<li data-cy="item" key={item}>
{item}
</li>
))}
</ul>
</div>
);
Expand Down
78 changes: 77 additions & 1 deletion src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1 +1,77 @@
export const Pagination = () => {};
import React from 'react';

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

export const Pagination: React.FC<Props> = ({
total,
perPage,
currentPage = 1,
onPageChange,
}) => {
const totalPages = Math.ceil(total / perPage);
const arrayPages = 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 ? true : false}
onClick={event => {
event.preventDefault();
if (currentPage !== 1) {
onPageChange(currentPage - 1);
}
}}
Comment on lines +30 to +32
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 pagination information is hardcoded. According to the requirements, it should dynamically display the current page, the range of items shown, and the total number of items. You can use the currentPage, perPage, and items.length variables to construct this string.

>
«
</a>
</li>
{arrayPages.map(page => (
<li
className={`page-item ${page === currentPage ? '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.

The logic to determine the active page is incorrect because page is a 0-based index from arrayPages while currentPage is 1-based. For example, for the first page, this will compare 0 with 1.

key={page}
>
<a
data-cy="pageLink"
className="page-link"
href={`#${page}`}
onClick={event => {
event.preventDefault();
if (page !== currentPage) {
onPageChange(page);
}
}}
>
{page}
</a>
</li>
))}
<li
className={`page-item ${currentPage === totalPages ? 'disabled' : ''}`}
>
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={currentPage === totalPages ? true : false}
onClick={event => {
event.preventDefault();
if (currentPage !== totalPages) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>
</li>
</ul>
);
};
8 changes: 6 additions & 2 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 { App } from './App';
import { BrowserRouter } from 'react-router-dom';

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