Skip to content

Commit

Permalink
resultsCalculate 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 5791c7c commit a770209
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function StudentList() {
})

const resultsCalculate = useCallback(
() => calculateResults(paginationParams, invites, invites, students),
() => calculateResults(paginationParams, { invites, teachers: invites, students }),
[paginationParams, invites, students],
)

Expand Down
27 changes: 16 additions & 11 deletions apps/schools/domains/student/handlers/resultsCalculate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,22 @@ import { RowType } from '@domains/student/components/studentList/interfaces'

export const calculateResults = (
paginationParams: { page: number; pageSize: number },
invites: { count: number | undefined; results: any[] } | undefined,
teachers: { count: number | undefined; results: any[] } | undefined,
students: { count: number | undefined; results: any[] } | undefined,
data: {
[key: string]: { count: number | undefined; results: any[] } | undefined
},
): RowType[] => {
if (paginationParams.page <= Math.ceil((invites?.count ?? 0) / paginationParams.pageSize)) {
return invites?.results ?? []
} else if (
paginationParams.page <= Math.ceil(((teachers?.count ?? 0) + (invites?.count ?? 0)) / paginationParams.pageSize)
) {
return teachers?.results ?? []
} else {
return students?.results ?? []
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 []
}

0 comments on commit a770209

Please sign in to comment.