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

[DataGrid] Fix overflow of maximum page #1583

Merged
merged 12 commits into from
May 18, 2021

Conversation

oliviertassinari
Copy link
Member

A continuation of #1571

@oliviertassinari oliviertassinari added bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module! labels May 7, 2021
@@ -88,7 +88,7 @@ export const setGridRowCountStateUpdate = (state, payload): GridPaginationState
...state,
pageCount: newPageCount,
rowCount: totalRowCount,
page: state.page > newPageCount ? newPageCount - 1 : state.page,
page: Math.min(state.page, newPageCount - 1),
Copy link
Member

Choose a reason for hiding this comment

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

How is that different 🤔

Copy link
Member Author

Choose a reason for hiding this comment

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

min is equivalent to:

Suggested change
page: Math.min(state.page, newPageCount - 1),
page: state.page > newPageCount -1 ? newPageCount - 1 : state.page,,

Copy link
Member

Choose a reason for hiding this comment

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

ah yes of course!
IMO , it's easier and more common to read page: state.page > newPageCount -1 ? newPageCount - 1 : state.page,

Copy link
Member Author

@oliviertassinari oliviertassinari May 7, 2021

Choose a reason for hiding this comment

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

I have used min because it makes it impossible for this error to happen, and since it went through two reviews unnoticed, I thought that we might as well enforce it. No real preference.

@oliviertassinari
Copy link
Member Author

Arf, it doesn't seem to work, there is a regression in Argos-CI, with one of the test case

@oliviertassinari
Copy link
Member Author

It seems to be a regression from #1571 that the snapshot test catches by chance.

@dtassone
Copy link
Member

dtassone commented May 7, 2021

It seems to be a regression from #1571 that the snapshot test catches by chance.

the check was green in the other PR 🤔

@oliviertassinari
Copy link
Member Author

oliviertassinari commented May 7, 2021

the check was green in the other PR 🤔

I have added a proper test case for it in 4db548b (snapshots test are unreliable for this). It passes on HEAD and fails on these two PRs.

@dtassone
Copy link
Member

dtassone commented May 7, 2021

The issue is that the page value is out of range when applying this filter model

@dtassone
Copy link
Member

dtassone commented May 7, 2021

The issue is that the page value is out of range when applying this filter model

Check https://codesandbox.io/s/material-demo-forked-87xex
It's an issue with the page not the filtering

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented May 7, 2021

Thanks Olivier for fix. It's all happening I think because page starts from zero index and the new pageCount is calculated as Math.ceil(rowCount, pageSize) which rounds up to largest integer. So in case of zero based decimals eg: 0.04 it's 1. So newPageCount always needs to be decremented by 1. (newPageCount - 1) like you did. I missed this one case.
Should we reconsider if Math.ceil is correct and thus instead avoid decrementing newPageCount by 1?
https://github.com/mui-org/material-ui-x/blob/00526d79d3cba481021afefe2a2a57591aa9283a/packages/grid/_modules_/grid/hooks/features/pagination/gridPaginationReducer.ts#L56

The issue is that the page value is out of range when applying this filter model

Check https://codesandbox.io/s/material-demo-forked-87xex
It's an issue with the page not the filtering

Won't filtered data will always be less than the total initial count of rows. So page range won't exceed, right? Unless user adds a page out of range which will trigger the warning.

@ZeeshanTamboli
Copy link
Member

Test case failures looks related to auto controlling of page size.

@oliviertassinari
Copy link
Member Author

oliviertassinari commented May 7, 2021

Test case failures looks related to auto controlling of page size.

@ZeeshanTamboli True, I have a fix for it.

Regarding the issue found by @dtassone in #1583 (comment), I think that we can open a new GitHub issue for it, it looks wrong as well but can be solved independently.

@oliviertassinari oliviertassinari force-pushed the improve-#1571 branch 2 times, most recently from dbe597e to 51d5526 Compare May 7, 2021 20:01
@oliviertassinari
Copy link
Member Author

Alright, It seems to work correctly now. I went on refactoring the hook. I didn't need it to do it here, but while I was as it, why not.

@ZeeshanTamboli
Copy link
Member

ZeeshanTamboli commented May 10, 2021

Doesn't this PR fully fix #1571 which we are including in alpha-28? So why are we are not considering this for alpha-28?

@oliviertassinari
Copy link
Member Author

@ZeeshanTamboli We ship at a fixed frequency, as long as there are no blockers on HEAD. #1571 is already an improvement by solving the case where the active page is above the last page + 1. Solving the +1 offset (#1583) is another improvement that can come independently.


const PAGINATION_STATE_ID = 'pagination';
function computeState(state) {
Copy link
Member

Choose a reason for hiding this comment

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

function computeState(state: GridPaginationState): GridPaginationState {

Copy link
Member

Choose a reason for hiding this comment

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

I would also rename computeState to getUpdatedState

Copy link
Member Author

@oliviertassinari oliviertassinari May 16, 2021

Choose a reason for hiding this comment

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

I have updated the name of the function to better describe the use case. It's meant to compute derivative states. In theory, we could remove them from the state, it could be done inside a selector but I didn't want to refactor too much 🤷‍♂️

);
logger.debug(`Setting page size to ${pageSize}`);

setGridState((oldState) => ({
Copy link
Member

Choose a reason for hiding this comment

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

I would rename to oldState to state

Copy link
Member Author

@oliviertassinari oliviertassinari May 16, 2021

Choose a reason for hiding this comment

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

In the codebase, I can find:

  • setGridState((state x29
  • setGridState((oldState x16
  • setGridState((previousState x1
  • setGridState((p x1

OK, it would be great to do a follow-up to clean this up

Copy link
Member

Choose a reason for hiding this comment

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

setGridState((state ... x29 👍

@oliviertassinari oliviertassinari dismissed dtassone’s stale review May 16, 2021 16:25

updated with the review, see the last 2 commits

@oliviertassinari oliviertassinari requested a review from dtassone May 16, 2021 16:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something doesn't work component: data grid This is the name of the generic UI component, not the React module!
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants