-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df143a2
commit 5d61174
Showing
3 changed files
with
89 additions
and
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
export type QueryParams = { [key: string]: string | string[] | undefined } | ||
export function buildPaginatedUrl({ | ||
baseUrl, | ||
page, | ||
limit, | ||
queryParams, | ||
}: { | ||
baseUrl: string | ||
page: number | ||
limit: number | ||
queryParams?: QueryParams | ||
}) { | ||
const queryString = new URLSearchParams({ | ||
page: String(page), | ||
limit: String(limit), | ||
...queryParams, | ||
}).toString() | ||
return `${baseUrl}?${queryString}` | ||
} |
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 |
---|---|---|
@@ -1,110 +1,85 @@ | ||
import { isNumber } from 'lodash-es' | ||
import { buildPaginatedUrl, QueryParams } from './buildPaginatedUrl' | ||
|
||
type ItemType = 'page' | 'ellipsis' | ||
type PageItem<T extends ItemType> = T extends 'page' | ||
? { type: T; value: number; url: string } | ||
: { type: T; value: '...' } | ||
|
||
export type PaginationArgs = { | ||
page?: number | ||
pageSize?: number | ||
} | ||
|
||
export type IPagination = { | ||
count: number | ||
totalPages: number | ||
currentPage: number | ||
prevPage?: PageItem<'page'> | ||
pageItems: (PageItem<'page'> | PageItem<'ellipsis'>)[] | ||
nextPage?: PageItem<'page'> | ||
prevPage?: PageItem<'page'> | ||
} | ||
|
||
function buildUrl({ | ||
baseUrl, | ||
page, | ||
limit, | ||
function parsePage(page: string | string[] | number | undefined): number { | ||
return typeof page === 'string' ? Number(page) : 1 | ||
} | ||
|
||
export function getPaginationParamsWithDefaults({ | ||
defaultPaginate, | ||
searchParams, | ||
}: { | ||
baseUrl: string | ||
page: number | ||
limit: number | ||
defaultPaginate?: Exclude<PaginationArgs, 'page'> | ||
searchParams?: QueryParams | ||
}) { | ||
return `${baseUrl}?page=${page}&limit=${limit}` | ||
return { | ||
page: parsePage(searchParams?.page), | ||
pageSize: searchParams?.pageSize | ||
? Number(searchParams.pageSize) | ||
: (defaultPaginate?.pageSize ?? 25), | ||
} | ||
} | ||
|
||
export function buildPagination({ | ||
baseUrl, | ||
count, | ||
queryParams, | ||
page, | ||
pageSize, | ||
pageItemsCount = 10, | ||
}: { | ||
baseUrl: string | ||
count: number | ||
queryParams?: QueryParams | ||
page: number | ||
pageSize: number | ||
pageItemsCount?: number | ||
}) { | ||
const totalPages = Math.ceil(count / pageSize) | ||
const pagination: IPagination = { | ||
return { | ||
count, | ||
totalPages, | ||
currentPage: page, | ||
prevPage: | ||
page > 1 | ||
? { | ||
type: 'page', | ||
value: page - 1, | ||
url: buildUrl({ baseUrl, page: page - 1, limit: pageSize }), | ||
} | ||
type: 'page', | ||
value: page - 1, | ||
url: buildPaginatedUrl({ | ||
baseUrl, | ||
page: page - 1, | ||
limit: pageSize, | ||
queryParams, | ||
}), | ||
} | ||
: undefined, | ||
pageItems: [], | ||
nextPage: | ||
page < totalPages | ||
? { | ||
type: 'page', | ||
value: page + 1, | ||
url: buildUrl({ baseUrl, page: page + 1, limit: pageSize }), | ||
} | ||
type: 'page', | ||
value: page + 1, | ||
url: buildPaginatedUrl({ | ||
baseUrl, | ||
page: page + 1, | ||
limit: pageSize, | ||
queryParams, | ||
}), | ||
} | ||
: undefined, | ||
} | ||
|
||
for (let i = page - 1; i >= 1 && pagination.pageItems.length < 2; i--) { | ||
pagination.pageItems.unshift({ | ||
type: 'page', | ||
url: buildUrl({ baseUrl, page: i, limit: pageSize }), | ||
value: i, | ||
}) | ||
} | ||
|
||
const prevItem = pagination.pageItems[0] | ||
const prevPage = isNumber(prevItem?.value) ? prevItem.value : undefined | ||
if (pagination.pageItems.length > 0 && prevPage && prevPage < page - 2) { | ||
pagination.pageItems.push({ type: 'ellipsis', value: '...' }) | ||
} | ||
|
||
pagination.pageItems.push({ | ||
type: 'page', | ||
value: page, | ||
url: buildUrl({ baseUrl, page, limit: pageSize }), | ||
}) | ||
|
||
for ( | ||
let i = page + 1; | ||
i <= totalPages && pagination.pageItems.length < pageItemsCount; | ||
i++ | ||
) { | ||
pagination.pageItems.push({ | ||
type: 'page', | ||
value: i, | ||
url: buildUrl({ baseUrl, page: i, limit: pageSize }), | ||
}) | ||
} | ||
|
||
const nextItem = pagination.pageItems[pagination.pageItems.length - 1] | ||
const nextPage = isNumber(nextItem?.value) ? nextItem.value : undefined | ||
if (pagination.pageItems.length > 0 && nextPage && nextPage > page + 1) { | ||
pagination.pageItems.push({ type: 'ellipsis', value: '...' }) | ||
} | ||
|
||
return pagination | ||
} | ||
|
||
export function parsePage( | ||
page: string | string[] | number | undefined, | ||
): number { | ||
return typeof page === 'string' ? Number(page) : 1 | ||
} |
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