Skip to content

Commit

Permalink
reuse query state handler
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Aug 15, 2024
1 parent bd0b4ae commit 2041787
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions src/components/TablePagination.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
CircularProgress,
TablePagination as MuiTablePagination,
type TablePaginationProps as MuiTablePaginationProps,
Stack,
Expand All @@ -15,8 +14,7 @@ import {
} from "react"

import { type Pagination, usePagination } from "../hooks/api"
import type { ListArg, ListResult } from "../utils/api"
import SyncError from "./SyncError"
import { type ListArg, type ListResult, handleQueryState } from "../utils/api"

export type TablePaginationProps<
QueryArg extends ListArg,
Expand Down Expand Up @@ -67,7 +65,7 @@ const TablePagination = <
RootComponent,
AdditionalProps
>): JSX.Element => {
const [trigger, { data: result, isLoading, error }] = useLazyListQuery()
const [trigger, result] = useLazyListQuery()
const [{ limit, page, offset }, setPagination] = usePagination({
page: initialPage,
limit: initialLimit,
Expand All @@ -77,11 +75,7 @@ const TablePagination = <
trigger({ limit, offset, ...filters } as QueryArg)
}, [trigger, limit, offset, filters])

useEffect(() => {
console.error(error)
}, [error])

const { data, count, max_limit } = result || {}
const { count, max_limit } = result.data || {}

if (max_limit) {
rowsPerPageOptions = rowsPerPageOptions.filter(
Expand All @@ -91,12 +85,14 @@ const TablePagination = <

return (
<Stack {...stackProps}>
{isLoading ? (
<CircularProgress />
) : error || !data ? (
<SyncError />
) : (
children(data, { limit, page, offset, count, maxLimit: max_limit })
{handleQueryState(result, ({ data }) =>
children(data, {
limit,
page,
offset,
count,
maxLimit: max_limit,
}),
)}
<MuiTablePagination
component="div"
Expand Down

0 comments on commit 2041787

Please sign in to comment.