Skip to content

added solution#1419

Open
juliasemchishin wants to merge 5 commits intomate-academy:masterfrom
juliasemchishin:develop
Open

added solution#1419
juliasemchishin wants to merge 5 commits intomate-academy:masterfrom
juliasemchishin:develop

Conversation

@juliasemchishin
Copy link

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Great job implementing the core pagination logic! The component correctly displays items and handles page navigation. However, a few changes are needed to fully meet the task requirements, primarily related to integrating React Router for state management.

Here's what needs to be addressed:

  1. URL State Management: In src/App.tsx, the currentPage and perPage state should be managed via URL query parameters (e.g., ?page=2&perPage=10). You can use the useSearchParams hook from react-router-dom to read the initial state from the URL and to update it when the user changes the page or items per page.

  2. Navigation Links: In src/components/Pagination/Pagination.tsx, the navigation elements should be React Router <Link> components instead of <a> tags. The to prop should be used to construct a URL that updates the search parameters correctly.

  3. Event Handler Logic: The onPageChange callback in Pagination.tsx should not be called when the user clicks on the link for the currently active page. Please add a check to prevent this unnecessary call.

You've built a solid foundation, and with these changes, your solution will be complete. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

<a
data-cy="pageLink"
className="page-link"
href={`#${page}`}

Choose a reason for hiding this comment

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

The task requires using React Router to manage the page state in the URL. Instead of using a plain <a> tag with an onClick handler and a hash href, you should use a component from React Router (like <Link>) and construct a to prop that updates the URL search parameters (e.g., ?page=...). This comment also applies to the previous and next links.

data-cy="pageLink"
className="page-link"
href={`#${page}`}
onClick={() => onPageChange(page)}

Choose a reason for hiding this comment

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

According to the requirements, the onPageChange callback should only be triggered if the page was changed. Currently, it's called even when clicking the link for the active page. You should add a condition to prevent this.

src/App.tsx Outdated
Comment on lines 9 to 10
const [currentPage, setCurrentPage] = useState(1);
const [perPage, setPerPage] = useState(5);

Choose a reason for hiding this comment

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

According to the task requirements, you need to use React Router to manage state in the URL. The initial values for currentPage and perPage should be read from the URL's search parameters (e.g., ?page=2&perPage=10). When these values change, the URL should also be updated. Consider using the useSearchParams hook from react-router-dom to achieve this.

src/App.tsx Outdated
<li data-cy="item">Item 3</li>
<li data-cy="item">Item 4</li>
<li data-cy="item">Item 5</li>
{items.slice(startIndex, calculatedEndIndex).map(item => (

Choose a reason for hiding this comment

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

While using calculatedEndIndex works here because slice handles out-of-bounds indices, you have already calculated the correct endIndex on line 20. It's best practice to use endIndex here for consistency and to make the code's intent clearer.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

Great job building the pagination UI and logic! The component works well with local state. However, this submission requires changes because a core requirement—managing state via URL parameters—has not been met.

Here are the two main things to fix:

  1. URL State Management: In src/App.tsx, please replace the useState hooks for currentPage and perPage with the useSearchParams hook from react-router-dom. This will allow the page state to be read from and saved to the URL's query parameters (e.g., ?page=2&perPage=10).
  2. Router Navigation: In src/components/Pagination/Pagination.tsx, all navigation elements should be <Link> components from react-router-dom instead of <a> tags. You'll need to use the to prop to construct the correct URL with the appropriate search parameters.

Once these changes are made, the application state will be correctly preserved in the URL. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Important

Fix the issues, commit the changes, and then re-request my review using the Re-request review button2 in the Reviewers tab.

Tip

If you have any comments about this review, please leave a message in the Add a comment section below and mention me with the @mateacademy-ai-mentor tag3.
I will read your message during my next review of your pull request. Please do not reply directly to my comments, as that feature is not supported yet.

Caution

Do not close the pull request until the review is complete. Closing it early can lead to unexpected behavior and disrupt the review process.

Footnotes

  1. Rate AI review example

  2. Re-request AI review example

  3. Add AI review comments example

src/App.tsx Outdated
const items = getNumbers(1, 42).map(n => `Item ${n}`);

export const App: React.FC = () => {
const [currentPage, setCurrentPage] = useState(1);

Choose a reason for hiding this comment

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

The currentPage state should not be managed by useState. According to the task requirements, it needs to be read from the URL query parameters (e.g., ?page=2) and updated via React Router. You can use the useSearchParams hook for this.

src/App.tsx Outdated

export const App: React.FC = () => {
const [currentPage, setCurrentPage] = useState(1);
const [perPage, setPerPage] = useState(5);

Choose a reason for hiding this comment

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

Similarly, perPage should also be managed via URL search parameters (e.g., ?perPage=10), not local state. This will allow the application state to be preserved in the URL.

src/App.tsx Outdated
Comment on lines 12 to 16
function onPageChange(page: number): void {
if (page !== currentPage) {
setCurrentPage(page);
}
}

Choose a reason for hiding this comment

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

When using URL-based state, this function's responsibility will change. Instead of calling setCurrentPage to update a local state variable, it should update the page query parameter in the URL using the setter function from useSearchParams.

Comment on lines 37 to 40
onChange={e => {
setPerPage(Number(e.target.value));
setCurrentPage(1);
}}

Choose a reason for hiding this comment

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

This handler should also be updated to modify the URL search parameters instead of calling setPerPage and setCurrentPage. When the user selects a new value, you should update the perPage parameter in the URL and reset the page parameter to 1.

Comment on lines 26 to 38
<a
data-cy="prevLink"
className="page-link"
href="#prev"
aria-disabled={currentPage === 1 ? 'true' : 'false'}
onClick={() => {
if (currentPage > 1) {
onPageChange(currentPage - 1);
}
}}
>
«
</a>

Choose a reason for hiding this comment

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

According to the task requirements, navigation should be handled by React Router. This <a> tag should be replaced with a <Link> component from react-router-dom. The href prop should be replaced with a to prop that constructs the correct URL with search parameters. For example: to={?page=${currentPage - 1}&perPage=${perPage}}. Consequently, the onClick handler would no longer be necessary.

Comment on lines 46 to 57
<a
data-cy="pageLink"
className="page-link"
href={`#${page}`}
onClick={() => {
if (page !== currentPage) {
onPageChange(page);
}
}}
>
{page}
</a>

Choose a reason for hiding this comment

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

Similar to the previous/next links, this should be a <Link> component. The to prop should point to a URL that includes the specific page number, like to={?page=${page}&perPage=${perPage}}. The onClick handler is not needed when using <Link> for navigation.

Comment on lines 67 to 81
<a
data-cy="nextLink"
className="page-link"
href="#next"
aria-disabled={
currentPage === pagesCountArray.length ? 'true' : 'false'
}
onClick={() => {
if (currentPage < pagesAmount) {
onPageChange(currentPage + 1);
}
}}
>
»
</a>

Choose a reason for hiding this comment

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

This link should also be a <Link> component from react-router-dom to ensure that clicking it updates the URL's query parameters correctly.

Copy link

@mateacademy-ai-mentor mateacademy-ai-mentor left a comment

Choose a reason for hiding this comment

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

Review Summary

I'm approving this submission as an educational override to help you progress, as there are a couple of important refinements still needed. You've done a fantastic job switching to <Link> components for navigation!

The main area for improvement is in src/App.tsx. It still uses useState to manage the page state, which creates a separate source of truth from the URL. To fix this and resolve issues with browser navigation, please replace the useState hooks for page and perPage with the useSearchParams hook.

Additionally, since the <Link> components now handle navigation, the onPageChange prop and the onClick handlers in src/components/Pagination/Pagination.tsx are redundant and can be removed.

You're very close to the ideal solution. Keep up the great work!


✨ Please rate this review in the Platform's notification center1. I will be grateful for your feedback! ✨

Footnotes

  1. Rate AI review example

src/App.tsx Outdated
Comment on lines 9 to 16
const location = useLocation();
const navigate = useNavigate();
const params = new URLSearchParams(location.search);
const pageFromUrl = Number(params.get('page')) || 1;
const perPageFromUrl = Number(params.get('perPage')) || 5;

const [currentPage, setCurrentPage] = useState(pageFromUrl);
const [perPage, setPerPage] = useState(perPageFromUrl);

Choose a reason for hiding this comment

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

This block of code for managing URL parameters and state can be greatly simplified by using the useSearchParams hook, as mentioned in the previous review. Using useState here creates a separate source of truth from the URL, which causes bugs when using browser navigation buttons.

Please replace this manual implementation (useLocation, useNavigate, useState) with useSearchParams to manage the page and perPage values directly from the URL.

Comment on lines 32 to 36
onClick={() => {
if (currentPage > 1) {
onPageChange(currentPage - 1);
}
}}

Choose a reason for hiding this comment

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

This onClick handler is redundant. The <Link> component already handles navigation to the URL specified in the to prop. State should be updated in the parent component in response to this URL change, not through a separate callback mechanism like onPageChange.

Comment on lines 51 to 55
onClick={() => {
if (page !== currentPage) {
onPageChange(page);
}
}}

Choose a reason for hiding this comment

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

Similar to the other navigation links, this onClick handler duplicates the navigation logic that is already handled by the <Link> component's to prop. Relying solely on the URL change for state updates simplifies the data flow.

Comment on lines 75 to 79
onClick={() => {
if (currentPage < pagesAmount) {
onPageChange(currentPage + 1);
}
}}

Choose a reason for hiding this comment

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

This onClick handler is also redundant. The <Link> component's to prop is sufficient for navigation, and the parent component should react to the resulting URL change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants