mirror of
https://github.com/umami-software/umami.git
synced 2026-02-04 20:57:17 +01:00
Refactor filter handling for queries.
This commit is contained in:
parent
5b300f1ff5
commit
ee6c68d27c
107 changed files with 731 additions and 835 deletions
|
|
@ -1,30 +1,27 @@
|
|||
import { UseQueryOptions } from '@tanstack/react-query';
|
||||
import { useState } from 'react';
|
||||
import { PageResult, PageParams, PagedQueryResult } from '@/lib/types';
|
||||
import { useApi } from './useApi';
|
||||
import { useNavigation } from './useNavigation';
|
||||
|
||||
export function usePagedQuery<T = any>({
|
||||
export function usePagedQuery({
|
||||
queryKey,
|
||||
queryFn,
|
||||
...options
|
||||
}: Omit<UseQueryOptions, 'queryFn'> & { queryFn: (params?: object) => any }): PagedQueryResult<T> {
|
||||
}: Omit<UseQueryOptions, 'queryFn'> & { queryFn: (params?: object) => any }) {
|
||||
const { query: queryParams } = useNavigation();
|
||||
const [params, setParams] = useState<PageParams>({
|
||||
search: '',
|
||||
page: queryParams?.page || '1',
|
||||
const [params, setParams] = useState({
|
||||
search: queryParams?.search ?? '',
|
||||
page: queryParams?.page ?? '1',
|
||||
});
|
||||
|
||||
const { useQuery } = useApi();
|
||||
const { data, ...query } = useQuery({
|
||||
queryKey: [{ ...queryKey, ...params }],
|
||||
queryFn: () => queryFn(params as any),
|
||||
...options,
|
||||
});
|
||||
|
||||
return {
|
||||
result: data as PageResult<T>,
|
||||
query,
|
||||
...useQuery({
|
||||
queryKey: [{ ...queryKey, ...params }],
|
||||
queryFn: () => queryFn(params),
|
||||
...options,
|
||||
}),
|
||||
params,
|
||||
setParams,
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue