Skip to content

Commit

Permalink
handlePaginationChange work for n element data
Browse files Browse the repository at this point in the history
  • Loading branch information
levil664 committed Jul 12, 2024
1 parent a770209 commit 3570697
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 59 deletions.
77 changes: 21 additions & 56 deletions apps/schools/domains/common/handlers/paginationChange.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,7 @@
export const handlePaginationChange = (
setPaginationParams: (params: { page: number; pageSize: number }) => void,
setQueryPaginationParams: (params: {
invites: { page: number; pageSize: number }
teachers: { page: number; pageSize: number }
students: { page: number; pageSize: number }
}) => void,
invitesCount: number | undefined,
teachersCount: number | undefined,
setQueryPaginationParams: (params: { [key: string]: { page: number; pageSize: number } }) => void,
counts: { [key: string]: number | undefined },
newPage: number,
newPageSize: number,
defaultPaginationTablePage: number,
Expand All @@ -18,60 +13,30 @@ export const handlePaginationChange = (
pageSize: newPageSize,
})

let newQueryParams: {
invites: { page: number; pageSize: number }
teachers: { page: number; pageSize: number }
students: { page: number; pageSize: number }
}
const newQueryParams: { [key: string]: { page: number; pageSize: number } } = {}

if (newPage <= Math.ceil((invitesCount ?? 0) / newPageSize)) {
newQueryParams = {
invites: {
page: newPage,
pageSize: newPageSize,
},
teachers: {
page: defaultPaginationTablePage,
pageSize: defaultPaginationTablePageSize,
},
students: {
page: defaultPaginationTablePage,
pageSize: defaultPaginationTablePageSize,
},
}
} else if (newPage <= Math.ceil(((teachersCount ?? 0) + (invitesCount ?? 0)) / newPageSize)) {
const nextPage = Math.abs(newPage - Math.ceil((invitesCount ?? 0) / newPageSize))
newQueryParams = {
invites: {
page: defaultPaginationTablePage,
pageSize: defaultPaginationTablePageSize,
},
teachers: {
page: nextPage,
let currentPage = newPage
let currentOffset = 0

Object.keys(counts).forEach((key, index) => {
const count = counts[key] ?? 0
const maxPages = Math.ceil(count / newPageSize)

if (currentPage <= maxPages) {
newQueryParams[key] = {
page: currentPage,
pageSize: newPageSize,
},
students: {
}
} else {
currentPage -= maxPages
newQueryParams[key] = {
page: defaultPaginationTablePage,
pageSize: defaultPaginationTablePageSize,
},
}
}
} else {
const nextPage = Math.abs(newPage - Math.ceil(((teachersCount ?? 0) + (invitesCount ?? 0)) / newPageSize))
newQueryParams = {
invites: {
page: defaultPaginationTablePage,
pageSize: defaultPaginationTablePageSize,
},
teachers: {
page: defaultPaginationTablePage,
pageSize: defaultPaginationTablePageSize,
},
students: {
page: nextPage,
pageSize: newPageSize,
},
}
}

currentOffset += count
})

setQueryPaginationParams(newQueryParams)
scrollToTop()
Expand Down
9 changes: 6 additions & 3 deletions apps/schools/domains/student/components/studentList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ export function StudentList() {
const handlePageChange = (newPage: number, newPageSize: number) => {
handlePaginationChange(
setPaginationParams,
setQueryPaginationParams,
invites?.count,
invites?.count,
setQueryPaginationParams as any,
{
invites: invites?.count,
teachers: invites?.count,
students: students?.count,
},
newPage,
newPageSize,
defaultPaginationTablePage,
Expand Down

0 comments on commit 3570697

Please sign in to comment.