feat(dashboard,js-sdk,types,admin-shared): Add Product Types domain (#7732)
This commit is contained in:
committed by
GitHub
parent
70a72ce2df
commit
2d8d2c4255
@@ -1,26 +1,34 @@
|
||||
import { QueryKey, UseQueryOptions, useQuery } from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
UseQueryOptions,
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
|
||||
const PRODUCT_TYPES_QUERY_KEY = "product_types" as const
|
||||
const productTypesQueryKeys = queryKeysFactory(PRODUCT_TYPES_QUERY_KEY)
|
||||
export const productTypesQueryKeys = queryKeysFactory(PRODUCT_TYPES_QUERY_KEY)
|
||||
|
||||
export const useProductType = (
|
||||
id: string,
|
||||
query?: Record<string, any>,
|
||||
query?: HttpTypes.AdminProductTypeParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
{ product_type: HttpTypes.AdminProductType },
|
||||
Error,
|
||||
{ product_type: HttpTypes.AdminProductType },
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.productTypes.retrieve(id, query),
|
||||
queryFn: () => sdk.admin.productType.retrieve(id, query),
|
||||
queryKey: productTypesQueryKeys.detail(id),
|
||||
...options,
|
||||
})
|
||||
@@ -29,22 +37,84 @@ export const useProductType = (
|
||||
}
|
||||
|
||||
export const useProductTypes = (
|
||||
query?: Record<string, any>,
|
||||
query?: HttpTypes.AdminProductTypeListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
{ product_types: HttpTypes.AdminProductType[] },
|
||||
Error,
|
||||
{ product_types: HttpTypes.AdminProductType[] },
|
||||
HttpTypes.AdminProductTypeListResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductTypeListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
"queryKey" | "queryFn"
|
||||
>
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryFn: () => client.productTypes.list(query),
|
||||
queryFn: () => sdk.admin.productType.list(query),
|
||||
queryKey: productTypesQueryKeys.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
return { ...data, ...rest }
|
||||
}
|
||||
|
||||
export const useCreateProductType = (
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateProductType
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productType.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: productTypesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useUpdateProductType = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTypeResponse,
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateProductType
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => sdk.admin.productType.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTypesQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: productTypesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export const useDeleteProductType = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.AdminProductTypeDeleteResponse,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => sdk.admin.productType.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productTypesQueryKeys.detail(id),
|
||||
})
|
||||
queryClient.invalidateQueries({ queryKey: productTypesQueryKeys.lists() })
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { HttpTypes } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
@@ -223,11 +224,11 @@ export const useProduct = (
|
||||
}
|
||||
|
||||
export const useProducts = (
|
||||
query?: Record<string, any>,
|
||||
query?: HttpTypes.AdminProductListParams,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
HttpTypes.AdminProductListResponse,
|
||||
Error,
|
||||
FetchError,
|
||||
HttpTypes.AdminProductListResponse,
|
||||
QueryKey
|
||||
>,
|
||||
|
||||
Reference in New Issue
Block a user