feat(dashboard,types,js-sdk): Cleanup collection domain (#7502)
**What** - Adds missing collection HttpTypes - Adds missing sdk functions - Adds usage of sdk to collection domain.
This commit is contained in:
committed by
GitHub
parent
75791f2cbd
commit
5a9922916a
@@ -1,3 +1,5 @@
|
||||
import { FetchError } from "@medusajs/js-sdk"
|
||||
import { FindParams, HttpTypes, PaginatedResponse } from "@medusajs/types"
|
||||
import {
|
||||
QueryKey,
|
||||
UseMutationOptions,
|
||||
@@ -5,19 +7,10 @@ import {
|
||||
useMutation,
|
||||
useQuery,
|
||||
} from "@tanstack/react-query"
|
||||
import { client } from "../../lib/client"
|
||||
import { sdk } from "../../lib/client"
|
||||
import { queryClient } from "../../lib/query-client"
|
||||
import { queryKeysFactory } from "../../lib/query-key-factory"
|
||||
import {
|
||||
CreateProductCollectionReq,
|
||||
UpdateProductCollectionProductsReq,
|
||||
UpdateProductCollectionReq,
|
||||
} from "../../types/api-payloads"
|
||||
import {
|
||||
ProductCollectionDeleteRes,
|
||||
ProductCollectionListRes,
|
||||
ProductCollectionRes,
|
||||
} from "../../types/api-responses"
|
||||
import { productsQueryKeys } from "./products"
|
||||
|
||||
const COLLECTION_QUERY_KEY = "collections" as const
|
||||
export const collectionsQueryKeys = queryKeysFactory(COLLECTION_QUERY_KEY)
|
||||
@@ -26,9 +19,9 @@ export const useCollection = (
|
||||
id: string,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
ProductCollectionRes,
|
||||
Error,
|
||||
ProductCollectionRes,
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
@@ -36,7 +29,7 @@ export const useCollection = (
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: collectionsQueryKeys.detail(id),
|
||||
queryFn: async () => client.collections.retrieve(id),
|
||||
queryFn: async () => sdk.admin.collection.retrieve(id),
|
||||
...options,
|
||||
})
|
||||
|
||||
@@ -44,12 +37,12 @@ export const useCollection = (
|
||||
}
|
||||
|
||||
export const useCollections = (
|
||||
query?: Record<string, any>,
|
||||
query?: FindParams & HttpTypes.AdminCollectionFilters,
|
||||
options?: Omit<
|
||||
UseQueryOptions<
|
||||
ProductCollectionListRes,
|
||||
Error,
|
||||
ProductCollectionListRes,
|
||||
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>,
|
||||
FetchError,
|
||||
PaginatedResponse<{ collections: HttpTypes.AdminCollection[] }>,
|
||||
QueryKey
|
||||
>,
|
||||
"queryFn" | "queryKey"
|
||||
@@ -57,7 +50,7 @@ export const useCollections = (
|
||||
) => {
|
||||
const { data, ...rest } = useQuery({
|
||||
queryKey: collectionsQueryKeys.list(query),
|
||||
queryFn: async () => client.collections.list(query),
|
||||
queryFn: async () => sdk.admin.collection.list(query),
|
||||
...options,
|
||||
})
|
||||
|
||||
@@ -67,14 +60,13 @@ export const useCollections = (
|
||||
export const useUpdateCollection = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
ProductCollectionRes,
|
||||
Error,
|
||||
UpdateProductCollectionReq
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateCollection
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: UpdateProductCollectionReq) =>
|
||||
client.collections.update(id, payload),
|
||||
mutationFn: (payload) => sdk.admin.collection.update(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
@@ -90,19 +82,24 @@ export const useUpdateCollection = (
|
||||
export const useUpdateCollectionProducts = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
ProductCollectionRes,
|
||||
Error,
|
||||
UpdateProductCollectionProductsReq
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
HttpTypes.AdminUpdateCollectionProducts
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload: UpdateProductCollectionProductsReq) =>
|
||||
client.collections.updateProducts(id, payload),
|
||||
mutationFn: (payload) => sdk.admin.collection.updateProducts(id, payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: collectionsQueryKeys.detail(id),
|
||||
})
|
||||
/**
|
||||
* Invalidate products list query to ensure that the products collections are updated.
|
||||
*/
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: productsQueryKeys.lists(),
|
||||
})
|
||||
|
||||
options?.onSuccess?.(data, variables, context)
|
||||
},
|
||||
@@ -112,13 +109,13 @@ export const useUpdateCollectionProducts = (
|
||||
|
||||
export const useCreateCollection = (
|
||||
options?: UseMutationOptions<
|
||||
ProductCollectionRes,
|
||||
Error,
|
||||
CreateProductCollectionReq
|
||||
{ collection: HttpTypes.AdminCollection },
|
||||
FetchError,
|
||||
HttpTypes.AdminCreateCollection
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: (payload) => client.collections.create(payload),
|
||||
mutationFn: (payload) => sdk.admin.collection.create(payload),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
|
||||
|
||||
@@ -130,10 +127,14 @@ export const useCreateCollection = (
|
||||
|
||||
export const useDeleteCollection = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<ProductCollectionDeleteRes, Error, void>
|
||||
options?: UseMutationOptions<
|
||||
HttpTypes.DeleteResponse<"collection">,
|
||||
FetchError,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
return useMutation({
|
||||
mutationFn: () => client.collections.delete(id),
|
||||
mutationFn: () => sdk.admin.collection.delete(id),
|
||||
onSuccess: (data, variables, context) => {
|
||||
queryClient.invalidateQueries({ queryKey: collectionsQueryKeys.lists() })
|
||||
queryClient.invalidateQueries({
|
||||
|
||||
Reference in New Issue
Block a user