Skip to content

Commit

Permalink
export pagination types
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jul 31, 2024
1 parent f664f42 commit ba86be2
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/hooks/api.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
import { type Dispatch, type SetStateAction, useState } from "react"

export type Pagination = { page: number; limit: number; offset: number }
export type SetPagination = Dispatch<
SetStateAction<{ page: number; limit: number }>
>
export type UsePaginationOptions = Partial<{
page: number
limit: number
}>

export function usePagination(
options?: UsePaginationOptions,
): [
{ page: number; limit: number; offset: number },
Dispatch<SetStateAction<{ page: number; limit: number }>>,
] {
): [Pagination, SetPagination] {
const { page = 0, limit = 150 } = options || {}

const [pagination, _setPagination] = useState({
const [pagination, _setPagination] = useState<Pagination>({
page,
limit,
offset: page * limit,
})

function setPagination(
value: SetStateAction<{ page: number; limit: number }>,
) {
const setPagination: SetPagination = value => {
_setPagination(({ page: previousPage, limit: previousLimit }) => {
let { page, limit } =
typeof value === "function"
Expand Down
9 changes: 8 additions & 1 deletion src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { usePagination, type UsePaginationOptions } from "./api"
import {
usePagination,
type Pagination,
type SetPagination,
type UsePaginationOptions,
} from "./api"
import {
useSession,
useSessionMetadata,
Expand All @@ -21,7 +26,9 @@ export {
useSearchParams,
useSession,
useSessionMetadata,
type Pagination,
type SessionMetadata,
type SetPagination,
type UsePaginationOptions,
type UseSessionChildren,
type UseSessionChildrenFunction,
Expand Down

0 comments on commit ba86be2

Please sign in to comment.