generated from 8iq/nodejs-hackathon-boilerplate-starter-kit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #150 from open-schools/fix/SCHOOL-841/pagination-s…
…tudent-list Fix/school 841/pagination student list
- Loading branch information
Showing
12 changed files
with
184 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
export const handlePaginationChange = ( | ||
setPaginationParams: (params: { page: number; pageSize: number }) => void, | ||
setQueryPaginationParams: (params: { [key: string]: { page: number; pageSize: number } }) => void, | ||
counts: { [key: string]: number | undefined }, | ||
newPage: number, | ||
newPageSize: number, | ||
defaultPaginationTablePage: number, | ||
defaultPaginationTablePageSize: number, | ||
scrollToTop: () => void, | ||
) => { | ||
setPaginationParams({ | ||
page: newPage, | ||
pageSize: newPageSize, | ||
}) | ||
|
||
const newQueryParams: { [key: string]: { page: number; pageSize: number } } = {} | ||
|
||
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, | ||
} | ||
} else { | ||
currentPage -= maxPages | ||
newQueryParams[key] = { | ||
page: defaultPaginationTablePage, | ||
pageSize: defaultPaginationTablePageSize, | ||
} | ||
} | ||
|
||
currentOffset += count | ||
}) | ||
|
||
setQueryPaginationParams(newQueryParams) | ||
scrollToTop() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const getTotalPages = (counts: { [key: string]: { count: number | undefined } }, pageSize: number) => { | ||
return Object.keys(counts).reduce((total, key) => { | ||
return total + Math.ceil((counts[key].count ?? 0) / pageSize) * pageSize | ||
}, 0) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { RowType } from '@domains/student/components/studentList/interfaces' | ||
|
||
export const calculateResults = ( | ||
paginationParams: { page: number; pageSize: number }, | ||
data: { | ||
[key: string]: { count: number | undefined; results: any[] } | undefined | ||
}, | ||
): RowType[] => { | ||
const dataArray = Object.values(data) | ||
const pageIndex = paginationParams.page - 1 | ||
let offset = 0 | ||
|
||
for (const item of dataArray) { | ||
const count = item?.count ?? 0 | ||
const pageCount = Math.ceil(count / paginationParams.pageSize) | ||
if (pageIndex < offset + pageCount) { | ||
return item?.results ?? [] | ||
} | ||
offset += pageCount | ||
} | ||
|
||
return [] | ||
} |
Oops, something went wrong.