-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: create generic
createGetQueryData
- Loading branch information
1 parent
2038e57
commit 4b69aae
Showing
2 changed files
with
15 additions
and
11 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 |
---|---|---|
@@ -1,6 +1,5 @@ | ||
import { tokenKeys } from '../model' | ||
import { queryClient } from '@/shared/api/query-client' | ||
import { TokenKey } from '@/entities/token' | ||
import { createGetQueryData } from '@/shared/lib/queries' | ||
|
||
export const getTokenQueryData = <Result, Key extends TokenKey = TokenKey>(key: TokenKey, keyData: Parameters<typeof tokenKeys[Key]>[0]) => | ||
queryClient.getQueryData<Result>(tokenKeys[key](keyData)) | ||
|
||
export const getTokenQueryData = createGetQueryData(tokenKeys) |
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,11 +1,16 @@ | ||
import { useQuery, QueryKey } from '@tanstack/react-query' | ||
import type { UseQueryOptions } from '@tanstack/react-query' | ||
import { QueryKey, useQuery } from '@tanstack/react-query' | ||
import { queryClient } from '@/shared/api/query-client' | ||
|
||
export function createQueryHook<TParams, TData, TQueryKey extends QueryKey>( | ||
export const createQueryHook = <TParams, TData, TQueryKey extends QueryKey>( | ||
getQueryOptions: (params: TParams, condition?: boolean) => UseQueryOptions<TData, Error, TData, TQueryKey>, | ||
) { | ||
return (params: TParams, condition?: boolean) => { | ||
const options = getQueryOptions(params, condition) | ||
return useQuery<TData, Error, TData, TQueryKey>(options) | ||
} | ||
) => (params: TParams, condition?: boolean) => { | ||
const options = getQueryOptions(params, condition) | ||
return useQuery<TData, Error, TData, TQueryKey>(options) | ||
} | ||
|
||
export const createGetQueryData = <KeysObject extends Record<keyof KeysObject & string, (arg: any) => any>>( | ||
keys: KeysObject | ||
) => | ||
<Result, Key extends keyof KeysObject = keyof KeysObject>(key: Key, keyData: Parameters<KeysObject[Key]>[0]) => | ||
queryClient.getQueryData<Result>(keys[key](keyData)) |