wip
This commit is contained in:
parent
9b459f70af
commit
6f2bff218f
3 changed files with 40 additions and 3 deletions
|
@ -16,7 +16,7 @@ export interface LoadMore<TData, TVariables> {
|
||||||
) => Promise<ApolloQueryResult<TData>>;
|
) => Promise<ApolloQueryResult<TData>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
type UseQuery<TData, TVariables> = QueryResult<TData, TVariables> &
|
export type UseQueryResult<TData, TVariables> = QueryResult<TData, TVariables> &
|
||||||
LoadMore<TData, TVariables>;
|
LoadMore<TData, TVariables>;
|
||||||
type UseQueryOpts<TData, TVariables> = Partial<{
|
type UseQueryOpts<TData, TVariables> = Partial<{
|
||||||
displayLoader: boolean;
|
displayLoader: boolean;
|
||||||
|
@ -26,7 +26,7 @@ type UseQueryOpts<TData, TVariables> = Partial<{
|
||||||
}>;
|
}>;
|
||||||
type UseQueryHook<TData, TVariables> = (
|
type UseQueryHook<TData, TVariables> = (
|
||||||
opts: UseQueryOpts<TData, TVariables>
|
opts: UseQueryOpts<TData, TVariables>
|
||||||
) => UseQuery<TData, TVariables>;
|
) => UseQueryResult<TData, TVariables>;
|
||||||
|
|
||||||
function makeQuery<TData, TVariables>(
|
function makeQuery<TData, TVariables>(
|
||||||
query: DocumentNode
|
query: DocumentNode
|
||||||
|
@ -36,7 +36,7 @@ function makeQuery<TData, TVariables>(
|
||||||
require,
|
require,
|
||||||
skip,
|
skip,
|
||||||
variables
|
variables
|
||||||
}: UseQueryOpts<TData, TVariables>): UseQuery<TData, TVariables> {
|
}: UseQueryOpts<TData, TVariables>): UseQueryResult<TData, TVariables> {
|
||||||
const notify = useNotifier();
|
const notify = useNotifier();
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
const [, dispatchAppState] = useAppState();
|
const [, dispatchAppState] = useAppState();
|
||||||
|
|
18
src/hooks/makeSearch.ts
Normal file
18
src/hooks/makeSearch.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
import { DocumentNode } from "graphql";
|
||||||
|
import { useState } from "react";
|
||||||
|
|
||||||
|
import Debounce from "../components/Debounce";
|
||||||
|
import { UseQueryResult } from "./makeQuery";
|
||||||
|
|
||||||
|
export interface SearchQueryVariables {
|
||||||
|
after?: string;
|
||||||
|
first: number;
|
||||||
|
query: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function makeSearch<TData, TVariables extends SearchQueryVariables>(
|
||||||
|
query: DocumentNode,
|
||||||
|
loadMoreFn: (result: UseQueryResult<TData, TVariables>) => void
|
||||||
|
): UseSearchHook<TData, TVariables> {
|
||||||
|
const [searchQuery, setSearchQuery] = useState("");
|
||||||
|
}
|
19
src/hooks/useDebounce.ts
Normal file
19
src/hooks/useDebounce.ts
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import { useEffect, useRef } from "react";
|
||||||
|
|
||||||
|
export type UseDebounceFn<T> = (...args: T[]) => void;
|
||||||
|
function useDebounce<T>(
|
||||||
|
debounceFn: UseDebounceFn<T>,
|
||||||
|
time = 200
|
||||||
|
): UseDebounceFn<T> {
|
||||||
|
const timer = useRef(null);
|
||||||
|
useEffect(() => () => clearTimeout(timer.current));
|
||||||
|
|
||||||
|
return (...args: T[]) => {
|
||||||
|
if (timer.current) {
|
||||||
|
clearTimeout(timer.current);
|
||||||
|
}
|
||||||
|
timer.current = setTimeout(() => debounceFn(...args), time);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
export default useDebounce;
|
Loading…
Reference in a new issue