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

Pagination #1530

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open

Pagination #1530

wants to merge 10 commits into from

Conversation

ARADDCC002
Copy link
Member

@ARADDCC002 ARADDCC002 commented Sep 30, 2024

No description provided.

@ARADDCC002 ARADDCC002 changed the title wip for pagination Pagination Sep 30, 2024
@ARADDCC002 ARADDCC002 marked this pull request as ready for review October 18, 2024 16:28
ARADDCC012
ARADDCC012 previously approved these changes Oct 21, 2024
@ARADDCC012 ARADDCC012 dismissed their stale review October 21, 2024 15:46

Unapproving

ARADDCC012
ARADDCC012 previously approved these changes Oct 22, 2024
@ARADDCC012 ARADDCC012 dismissed their stale review October 22, 2024 11:05

Unapproving after testing merge protections.

ARADDCC012
ARADDCC012 previously approved these changes Oct 22, 2024
@ARADDCC012 ARADDCC012 dismissed their stale review October 22, 2024 13:29

Unapproving after fixing branch protection on main

@@ -20,6 +20,8 @@ export const getModelsSearchSchema = z.object({
search: z.string().optional().default(''),
allowTemplating: strictCoerceBoolean(z.boolean().optional()),
schemaId: z.string().optional(),
currentPage: z.string().optional(),
Copy link
Member

Choose a reason for hiding this comment

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

We can use z.coerce.number().optional() to validate currentPage and pageSize as numbers and transform them to numbers at the same time. That way we don't ever have to treat them as strings.

}
const promise = Promise.all([
ModelModel.find(query)
.limit(pageSize)
Copy link
Member

Choose a reason for hiding this comment

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

Looks like Github actions is having a wobbly here as this is definitely valid based on the mongoose docs: https://mongoosejs.com/docs/6.x/docs/api/query.html#query_Query-limit

// Sort by text search
cursor = cursor.sort({ score: { $meta: 'textScore' } })
}
const promise = Promise.all([
Copy link
Member

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 const [results, count] = await Promise.all([...

const auths = await authorisation.models(user, results, ModelAction.View)
return results.filter((_, i) => auths[i].success)
return { results: results.filter((_, i) => auths[i].success), totalEntries: count }
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if this is an issue or not, but totalEntries will include entries that the user doesn't have access to.

Should we instead return a count of all the entires the user does have access to?

Copy link
Member Author

Choose a reason for hiding this comment

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

As we check the user auth after making the call, we'll need to fetch all the documents that match the query without limiting the result based on current page and page size etc.

I don't think we can do this as part of the Mongo query which means that we'll be losing a lot of the benefits that pagination provides, but I'm not sure we can get around this?


interface PaginationSelectorProps {
currentPage: number | string
currentPageOnChange: (newValue: number | string) => void
Copy link
Member

Choose a reason for hiding this comment

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

To follow convention consider renaming to onCurrentPageChange

)

const menuItems = useMemo(() => {
return [...Array(lastPage | 0)].map((_, i) => {
Copy link
Member

Choose a reason for hiding this comment

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

Minor, but I'm never a fan of single character variable names. In most cases it's fair to assume that i is an index, but that involves either making an assumption or reading the entire block of code to check. It's easier to simply name this index in my opinion.

Copy link
Member Author

Choose a reason for hiding this comment

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

index is still one of the few places I default to using i, left over my Java days! Happy to change though :)

[currentPageOnChange],
)

const menuItems = useMemo(() => {
Copy link
Member

Choose a reason for hiding this comment

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

Consider renaming to something more descriptive like pageNumberOptions

label='Age'
onChange={handleManualPageChange}
MenuProps={{
style: {
Copy link
Member

Choose a reason for hiding this comment

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

Can we use sx here?

<Select
size='small'
value={currentPage.toString()}
label='Age'
Copy link
Member

Choose a reason for hiding this comment

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

label='Page'?

<Typography>Page:</Typography>
<Select
size='small'
value={currentPage.toString()}
Copy link
Member

Choose a reason for hiding this comment

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

Two things here. Firstly we usually use string templates for this sort of thing: value={${currentPage}}.

Secondly, we shouldn't need to transform it to a string as the menu items' values are numbers.

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