From 3570697daf08fc5f4eacc8a301c50897be226bd9 Mon Sep 17 00:00:00 2001 From: lev tarasov <56757711+levil664@users.noreply.github.com> Date: Fri, 12 Jul 2024 16:43:54 +0500 Subject: [PATCH] handlePaginationChange work for n element data --- .../common/handlers/paginationChange.ts | 77 +++++-------------- .../student/components/studentList/index.tsx | 9 ++- 2 files changed, 27 insertions(+), 59 deletions(-) diff --git a/apps/schools/domains/common/handlers/paginationChange.ts b/apps/schools/domains/common/handlers/paginationChange.ts index d0c80b0d..bbd0217c 100644 --- a/apps/schools/domains/common/handlers/paginationChange.ts +++ b/apps/schools/domains/common/handlers/paginationChange.ts @@ -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, @@ -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() diff --git a/apps/schools/domains/student/components/studentList/index.tsx b/apps/schools/domains/student/components/studentList/index.tsx index 923106d0..81d28345 100644 --- a/apps/schools/domains/student/components/studentList/index.tsx +++ b/apps/schools/domains/student/components/studentList/index.tsx @@ -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,