-
Notifications
You must be signed in to change notification settings - Fork 432
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
Change from offset & limit to number and size #9207
Conversation
validator=Range(min=1), | ||
missing=1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Starting page number from 1
instead of 0
A related issue: If you are on page 3 with a page size of 20, so the first item has offset 40 and you change the page size to 50, what page do you end up on? There is no 50-item sized page that exactly aligns with offset 40. A possible solution would be to adjust the page number to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. There are a few variable names and comments in the frontend that will need an update.
@@ -191,7 +191,7 @@ export default function EditGroupMembersForm({ | |||
const config = useContext(Config)!; | |||
const currentUserid = config.context.user.userid; | |||
|
|||
const [pageIndex, setPageIndex] = useState(0); | |||
const [pageIndex, setPageIndex] = useState(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These should be renamed to pageNumber
/setPageNumber
to reflect the change to 1-based numbers.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Done
currentPage={pageIndex + 1} | ||
onChangePage={page => setPageIndex(page - 1)} | ||
currentPage={pageIndex} | ||
onChangePage={page => setPageIndex(page)} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to onChangePage={setPageNumber}
after renaming mentioned above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Done
offset: pageIndex * pageSize, | ||
limit: pageSize, | ||
pageNumber: pageIndex, | ||
pageSize: pageSize, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Options can be simplified to { pageNumber, pageSize, signal: abort.signal }
after the renaming mentioned above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Done
@@ -113,10 +113,10 @@ export type APIOptions = { | |||
signal?: AbortSignal; | |||
|
|||
/** Index of first item to return in paginated APIs. */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment needs to be updated to eg. "1-based number of first page to return ..."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Copilot reviewed 5 out of 9 changed files in this pull request and generated no suggestions.
Files not reviewed (4)
- h/static/scripts/group-forms/utils/api.ts: Evaluated as low risk
- h/static/scripts/group-forms/utils/test/api-test.js: Evaluated as low risk
- h/static/scripts/group-forms/components/test/EditGroupMembersForm-test.js: Evaluated as low risk
- h/views/api/group_members.py: Evaluated as low risk
Comments skipped due to low confidence (2)
tests/functional/api/groups/members_test.py:100
- The use of 'offset' in the 'group_members_service.get_memberships' call should be reviewed to ensure it aligns with the new pagination logic. Consider updating it to use 'pageNumber' and 'pageSize'.
context.group, offset=4, limit=2
tests/functional/api/groups/members_test.py:282
- The error message should be updated to reflect the new parameters 'page[number]' and 'page[size]'.
"reason": "page[offset]: -1 is less than minimum value 0\npage[limit]: 0 is less than minimum value 1"
Change the list-memberships API's pagination query params from `page[offset]` and `page[limit]` to `page[number]` and `page[size]`. This is a better API design because it doesn't enable the client to send nonsensical values. For example `page[offset]=2` and `page[size]=10` is nonsensical because `offset` isn't a multiple of `limit`: what would the `offset` and `limit` for the previous page link be? With `page` and `size` this class of nonsensical requests is no longer possible. This will make it easier to add first, last, prev and next page links to the backend's API responses, as was attempted in this PR (not merged): #9181 If the client supplies a page number that is beyond the end of the group's list of memberships, rather than an error the server simply responds with empty date: { "meta": { "page": { "total": 101 } }, "data": [] } The client can use the `meta.page.total` value to figure out how many pages there should be and re-render its pagination controls. (This can happen for example if members leave the group after pagination controls are rendered: the pagination controls may contain links to pages that no longer exist.)
Change the list-memberships API's pagination query params from
page[offset]
andpage[limit]
topage[number]
andpage[size]
.This is a better API design because it doesn't enable the client to send
nonsensical values. For example
page[offset]=2
andpage[size]=10
is nonsensical because
offset
isn't a multiple oflimit
: what wouldthe
offset
andlimit
for the previous page link be?With
page
andsize
this class of nonsensical requests is no longerpossible.
This will make it easier to add first, last, prev and next page links to
the backend's API responses, as was attempted in this PR (not merged):
#9181
If the client supplies a page number that is beyond the end of the
group's list of memberships, rather than an error the server simply
responds with empty data:
The client can use the
meta.page.total
value to figure out how manypages there should be and re-render its pagination controls.
(This can happen for example if members leave the group after pagination
controls are rendered: the pagination controls may contain links to
pages that no longer exist.)
Testing
Creating a group with lots of members
Log in as devdata_admin
Create a new group
Create an auth client with:
Authority: localhost
Grant type: client_credentials
Trusted: yes
Run this script to add 100 members to the group:
The script requires
httpx
so:Testing the API
Create an API key then:
Testing the UI
Go to http://localhost:5000/admin/features and enable the
group_members
feature flag, then go to the group's edit page and test the pagination controls at the bottom.