Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #37644 - Pagination doesnt update between react pages #10241

Merged
merged 1 commit into from
Jul 25, 2024

Conversation

MariaAga
Copy link
Member

To recreate the bug: navigate to models?search=&page=2&per_page=5
from the menu, go to audits, then from the menu go to models
result is that the page will show page 1, per page 20 results from the api, but the pagination ui will show page 2, per page 5.
if the search is not empty, params.get('page') won't always be there, so the code sets the default value

Copy link
Member

@ekohl ekohl left a comment

Choose a reason for hiding this comment

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

It's confusing to me that there's a certain overlap between this and

const urlParams = new URLSearchParams(historySearch);
const urlParamsSearch = urlParams.get('search') || '';
const search = urlParamsSearch || getURIsearch();
const defaultParams = { search: search || '' };
if (updateParamsByUrl) {
const urlPage = urlParams.get('page');
const urlPerPage = urlParams.get('per_page');
if (urlPage) {
defaultParams.page = parseInt(urlPage, 10);
}
if (urlPerPage) {
defaultParams.per_page = parseInt(urlPerPage, 10);
}
}

It makes me question: why does the front end hardcode a per_page default? Shouldn't it just make an API request and read the per_page value from the result? That way it respects the default settings.

nextPage = Number(params.get('page'));
nextPerPage = Number(params.get('per_page'));
nextPage = Number(params.get('page') || getURIpage());
nextPerPage = Number(params.get('per_page') || getURIperPage());
Copy link
Member

Choose a reason for hiding this comment

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

On line 34 there's perPage. Why isn't that used?

Copy link
Member Author

Choose a reason for hiding this comment

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

line 34 is: const [perPage, setPerPage] = useState(propsPerPage || settingsPerPage);
and then later in this useeffect theres setPerPage(current => nextPerPage || current || settingsPerPage);

@MariaAga
Copy link
Member Author

It makes me question: why does the front end hardcode a per_page default? Shouldn't it just make an API request and read the per_page value from the result? That way it respects the default settings

I think the code snippet you gave, is the one to prepage the page param before the api request, to know which page to request based on the url, so if the url is models?search=&page=2&per_page=5, request only 5 per_page

Copy link
Contributor

@kmalyjur kmalyjur left a comment

Choose a reason for hiding this comment

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

Awesome, now it works as it should

Copy link
Contributor

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

from the menu, go to audits, then from the menu go to models
result is that the page will show page 1, per page 20 results from the api, but the pagination ui will show page 2, per page 5.

When I do this on develop, I get the behavior above.
After checking out the pr, I get behavior that is better but still not correct: It seems like the pagination shows a correct page of 1 but an incorrect perPage:

image

"1-5 of 10" but it still shows all 10. (My default per page setting is unchanged from the default of 20.)

@MariaAga
Copy link
Member Author

Thanks @jeremylenz added another condition to make sure the value is correct

Copy link
Contributor

@jeremylenz jeremylenz left a comment

Choose a reason for hiding this comment

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

Works well now 👍

thanks @MariaAga!

@jeremylenz
Copy link
Contributor

Ruby Katello failures unrelated; merging.

@jeremylenz jeremylenz merged commit eb684bb into theforeman:develop Jul 25, 2024
64 of 67 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
4 participants