Files
medusa-store/packages/admin/dashboard/src/lib/query-key-factory.ts
Kasper Fabricius Kristensen 904f0926f1 fix(dashboard): Load product variant edit page and fix product detail query key (#10029)
**What**
- Fixes Edit Variant form so it properly loads the product variant
- Fixes the query key for product details to prevent the cache from being shared between queries for the same ID but with different params.

Resolves CMRC-685

Co-authored-by: Frane Polić <16856471+fPolic@users.noreply.github.com>
2024-11-12 08:40:08 +00:00

54 lines
1.5 KiB
TypeScript

import { QueryKey, UseQueryOptions } from "@tanstack/react-query"
export type TQueryKey<TKey, TListQuery = any, TDetailQuery = string> = {
all: readonly [TKey]
lists: () => readonly [...TQueryKey<TKey>["all"], "list"]
list: (
query?: TListQuery
) => readonly [...ReturnType<TQueryKey<TKey>["lists"]>, { query: TListQuery }]
details: () => readonly [...TQueryKey<TKey>["all"], "detail"]
detail: (
id: TDetailQuery,
query?: TListQuery
) => readonly [
...ReturnType<TQueryKey<TKey>["details"]>,
TDetailQuery,
{ query: TListQuery }
]
}
export type UseQueryOptionsWrapper<
// Return type of queryFn
TQueryFn = unknown,
// Type thrown in case the queryFn rejects
E = Error,
// Query key type
TQueryKey extends QueryKey = QueryKey
> = Omit<
UseQueryOptions<TQueryFn, E, TQueryFn, TQueryKey>,
"queryKey" | "queryFn"
>
export const queryKeysFactory = <
T,
TListQueryType = any,
TDetailQueryType = string
>(
globalKey: T
) => {
const queryKeyFactory: TQueryKey<T, TListQueryType, TDetailQueryType> = {
all: [globalKey],
lists: () => [...queryKeyFactory.all, "list"],
list: (query?: TListQueryType) =>
[...queryKeyFactory.lists(), query ? { query } : undefined].filter(
(k) => !!k
),
details: () => [...queryKeyFactory.all, "detail"],
detail: (id: TDetailQueryType, query?: TListQueryType) =>
[...queryKeyFactory.details(), id, query ? { query } : undefined].filter(
(k) => !!k
),
}
return queryKeyFactory
}