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://SerhiyShimko.github.io/react_pagination/) and add it to the PR description.
12 changes: 7 additions & 5 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
115 changes: 39 additions & 76 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
import React from 'react';
import React, { useState } from 'react';
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 currently doesn't persist or apply ?page=...&perPage=... in the URL. The task explicitly requires using React Router to save query params on change and apply them on load. You need to read initial values from the location (query params) and push/update the URL when page or perPage changes (e.g. using useSearchParams / useLocation + useNavigate or useHistory).

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}`);
const items: string[] = getNumbers(0, 42).map(n => `Item ${n}`);
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 items array is built with getNumbers(0, 42) which likely produces Item 0 as the first element. Your pagination calculations (startsWith) are 1-based, so this can cause an off-by-one mismatch between the displayed info and the actual items. Consider creating items starting from 1 (e.g. getNumbers(1, 42)) or adjust indices consistently.


Comment on lines 4 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 per-page <select> is present and controlled in App (good), but the checklist requires the per-page selector to be a controlled component in both Pagination.tsx and App.tsx. Pagination.tsx does not render the <select> and its Props type doesn't accept a handler to change perPage. Add a prop like onPerPageChange: (perPage: number) => void and render a controlled <select data-cy="perPageSelector" value={perPage} ...> inside Pagination.

export const App: React.FC = () => {
const [perPage, changePerPage] = useState(5);
const [currentPage, changeCurrentPage] = useState(1);
Comment on lines +10 to +11
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pagination state must be persisted in the URL using React Router query params. Initialize page and perPage from the URL on load (useSearchParams or useLocation) and update the query string whenever they change. Currently state is local only, so add router-based syncing here when initializing the states.

const startIndex = (currentPage - 1) * perPage + 1;
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

startIndex is computed incorrectly. The task requires a zero-based start index: use const startIndex = (currentPage - 1) * perPage; (no + 1). This index should be used for slicing items (see next comment) so visible items line up with the info text. (Checklist items #1 and #10).

const nowItems = items.slice(startIndex, startIndex + 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.

nowItems slice uses the wrong startIndex (because startIndex above is 1-based). After fixing startIndex to be zero-based, slice like items.slice(startIndex, startIndex + perPage). Do not use splice or a 1-based index — slice is correct but the index must be zero-based.

const total = 42;
let ends = perPage * currentPage;

Comment on lines +11 to +16
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pagination currently doesn't render the per-page selector at all. Per the Implementation Details, render the inside Pagination and control it via props (perPage, onPerPageChange). Add those props to the component type and render the options 3, 5, 10, 20.

if (ends > total) {
Comment on lines +11 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.

The component currently requires currentPage in the Props destructuring. Per requirement #16, currentPage should be optional with a default of 1. Provide a default value in the destructuring, for example: ({ total, perPage, currentPage = 1, onPageChange }) so the component works when currentPage is omitted.

ends = total;
}

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} - {ends} of {total})
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 info text displays items {startIndex} - {ends} but startIndex should be the human-readable 1-based item number. After you change startIndex to be zero-based for slicing, compute a displayStart (e.g. const displayStart = startIndex + 1) and use that in the info text so it reads: Page 1 (items 1 - 5 of 42).

</p>
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Similarly, the prev link's onClick does not call preventDefault(), so clicking will change the hash. Update the handler to accept the click event and call event.preventDefault() before running the onPageChange guard. Same for the next link.


<div className="form-group row">
<div className="col-3 col-sm-2 col-xl-1">
<select
value={perPage}
data-cy="perPageSelector"
id="perPageSelector"
className="form-control">
className="form-control"
onChange={e => {
changeCurrentPage(1);
changePerPage(+e.target.value);
}}
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 numbered page onClick handler is attached to the li while the a anchors still have hrefs that change the hash. Prefer moving the click handler to the <a> and calling event.preventDefault() so clicks don't change the URL hash. Also keep the guard that only calls onPageChange when the page actually changes (you already have if (button !== currentPage) — keep that).

>
<option value="3">3</option>
<option value="5">5</option>
<option value="5"> 5 </option>
<option value="10">10</option>
<option value="20">20</option>
Comment on lines +39 to 44
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 click handler for selecting a page is attached to the

  • , while the still has an href that will change the URL hash. The requirement is that anchors should not perform default navigation. Move the handler to the or call event.preventDefault() inside the anchor click handler so clicking page links doesn't change the URL hash.

  • </select>
    Comment on lines 31 to 45
    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 per-page is rendered here in App, but the task requires the per-page selector to be rendered inside the stateless Pagination component and controlled via props (e.g. pass perPage and onPerPageChange). Move this selector into Pagination and remove it from App.

    Expand All @@ -32,78 +50,23 @@ export const App: React.FC = () => {
    </label>
    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 anchor uses href={#${button}} but has no onClick that prevents default, so clicking it will update the hash. Ensure the anchor itself calls e.preventDefault() (or remove/replace href) to prevent default navigation, per the task instructions.

    </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 => {
    changeCurrentPage(page);
    }}
    />
    Comment on lines +53 to +60
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    When using the Pagination component you should also pass an onPerPageChange prop (after you move the selector into Pagination). Right now Pagination receives perPage/currentPage/onPageChange only; add and pass onPerPageChange from App so App can update perPage (and reset to page 1).


    <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>
    {nowItems.map((item, index) => {
    return (
    <li data-cy="item" key={index}>
    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 next link also should prevent default navigation to avoid hash changes. Update the onClick handler on the next anchor to receive the event and call event.preventDefault() before calling onPageChange (when appropriate).

    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    Minor: when rendering the items list in App, prefer using a stable key such as the item string instead of the array index to avoid potential key collisions and to satisfy the low-priority checklist suggestion.

    {item}
    </li>
    );
    })}
    </ul>
    </div>
    );
    Expand Down
    77 changes: 76 additions & 1 deletion src/components/Pagination/Pagination.tsx
    Original file line number Diff line number Diff line change
    @@ -1 +1,76 @@
    export const Pagination = () => {};
    import React from 'react';
    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 currently has no React Router usage. The task requires saving ?page=2&perPage=7 into the URL and applying them on page load. Add router hooks (for example useLocation/useHistory in v5 or useSearchParams in v6) to read initial page and perPage from query params and to push/replace the query params when currentPage or perPage change.

    import { getNumbers } from '../../utils';

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

    Choose a reason for hiding this comment

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

    Pagination props are missing an onPerPageChange (or similar) to allow App to control per-page value from the selector if you move it into Pagination. Add an onPerPageChange: (perPage: number) => void prop and render the inside Pagination as required.

    };
    Comment on lines +4 to +9
    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 currentPage prop is declared as required in the Props type. The task requires currentPage to be optional with a default of 1 — change the type to currentPage?: number.


    export const Pagination: React.FC<Props> = ({
    Comment on lines +9 to +11
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    React Router query param syncing is missing. The App should read page and perPage from the URL on initial load and update the URL ?page=...&perPage=... whenever currentPage or perPage changes so pagination state is persisted and restorable.

    total,
    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 startsWith value is a 1-based (human) position which is correct for the info text, but array indexing is zero-based. Compute a zero-based start index (e.g. const startIndex = (currentPage - 1) * perPage) to use when slicing the items array.

    perPage,
    Comment on lines +12 to +13
    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 startIndex is computed as (currentPage - 1) * perPage + 1, which is a 1-based index. The task requires a zero-based start index: startIndex = (currentPage - 1) * perPage, and then use items.slice(startIndex, startIndex + perPage) so visible items line up with the info text. After that, compute a display start for the UI: displayStart = startIndex + 1.

    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 slice call is off by one. You compute a zero-based startIndex (correct) but then use startIndex + 1 when slicing items. Use items.slice(startIndex, startIndex + perPage) so the displayed items match the info text and tests.

    currentPage = 1,
    onPageChange,
    }) => {
    Comment on lines +11 to +16
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    Provide a default value for currentPage when destructuring props so the component behaves correctly when currentPage isn't provided: e.g. currentPage = 1 in the parameter list.

    const buttons = getNumbers(1, Math.ceil(total / perPage));

    return (
    <ul className="pagination">
    <li className={currentPage === 1 ? 'page-item disabled' : 'page-item'}>
    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 ? 'true' : 'false'}
    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 info paragraph uses startIndex for display. If you change startIndex to be zero-based (as required), update the displayed start to startIndex + 1 (e.g. use displayStart) so the text shows Page 1 (items 1 - 5 of 42).

    onClick={() => {
    if (currentPage !== 1) {
    onPageChange(currentPage - 1);
    }
    }}
    >
    «
    </a>
    </li>
    {buttons.map(button => {
    return (
    Comment on lines +36 to +38
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    When perPage changes you reset the current page to 1 (good), but you also need to update the URL query string (page=1&perPage=...) so the new state is persisted. Integrate this into the onChange handler (or centralize URL updates in App's state effects).

    <li
    Comment on lines +31 to +39
    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 per-page selector must be rendered inside the stateless Pagination component and controlled via props (e.g. pass an onPerPageChange handler). Currently the lives in App; move it into Pagination and drive it via props so Pagination owns the UI for per-page selection as required.

    onClick={() => {
    if (button !== currentPage) {
    onPageChange(button);
    }
    }}
    className={
    currentPage === button ? 'page-item active' : 'page-item'
    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

    }
    key={button}
    >
    <a data-cy="pageLink" className="page-link" href={`#${button}`}>
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    Numbered page anchors (<a data-cy="pageLink" ...>) currently have their click handler on the parent

  • and the anchor has an href that updates the hash. This violates the requirement to prevent default navigation. Move the click handler to the anchor (or call event.preventDefault() inside it) so clicks do not change the URL hash.

  • {button}
    </a>
    Comment on lines +37 to +52
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    In the Pagination component the numbered anchors use href="#..." and the click handler for changing page is on the li. Clicking the <a> will still change the URL hash. Prevent default navigation (call e.preventDefault()), or move the onClick to the <a> and prevent default there to avoid unwanted hash changes.

    </li>
    );
    })}
    <li
    className={
    currentPage === buttons.length ? 'page-item disabled' : 'page-item'
    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 === buttons.length ? 'true' : 'false'}
    onClick={() => {
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    You use [...items].splice(startsWith, perPage) which (a) uses a 1-based startsWith leading to an off-by-one and (b) uses splice (mutating) instead of slice. Replace with something like items.slice(startIndex, startIndex + perPage) where startIndex = (currentPage - 1) * perPage so the rendered items match the info text.

    if (currentPage !== buttons.length) {
    Comment on lines +64 to +68
    Copy link
    Copy Markdown

    Choose a reason for hiding this comment

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

    Using the array index as the key for list items can cause rendering issues. Use the item text or number (e.g. key={item}) which is unique, instead of key={index}.

    onPageChange(currentPage + 1);
    }
    }}
    >
    »
    </a>
    </li>
    </ul>
    );
    };
    Loading