feat(medusa-react): add product category queries and mutations (#3218)
This commit is contained in:
@@ -18,6 +18,7 @@ export * from "./price-lists"
|
||||
export * from "./product-tags"
|
||||
export * from "./product-types"
|
||||
export * from "./products"
|
||||
export * from "./product-categories"
|
||||
export * from "./publishable-api-keys"
|
||||
export * from "./regions"
|
||||
export * from "./return-reasons"
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
export * from "./queries"
|
||||
export * from "./mutations"
|
||||
@@ -0,0 +1,167 @@
|
||||
import {
|
||||
useMutation,
|
||||
UseMutationOptions,
|
||||
useQueryClient,
|
||||
} from "@tanstack/react-query"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import {
|
||||
AdminDeleteProductCategoriesCategoryProductsBatchReq,
|
||||
AdminPostProductCategoriesCategoryProductsBatchReq,
|
||||
AdminPostProductCategoriesCategoryReq,
|
||||
AdminPostProductCategoriesReq,
|
||||
AdminProductCategoriesCategoryDeleteRes,
|
||||
AdminProductCategoriesCategoryRes,
|
||||
} from "@medusajs/medusa"
|
||||
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
import { adminProductCategoryKeys } from "./queries"
|
||||
import { adminProductKeys } from "../products"
|
||||
|
||||
/**
|
||||
* Hook provides a mutation function for creating product categories.
|
||||
*
|
||||
* @experimental This feature is under development and may change in the future.
|
||||
* To use this feature please enable the corresponding feature flag in your medusa backend project.
|
||||
*/
|
||||
export const useAdminCreateProductCategory = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminProductCategoriesCategoryRes>,
|
||||
Error,
|
||||
AdminPostProductCategoriesReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostProductCategoriesReq) =>
|
||||
client.admin.productCategories.create(payload),
|
||||
buildOptions(queryClient, [adminProductCategoryKeys.list()], options)
|
||||
)
|
||||
}
|
||||
|
||||
/** Update a product category
|
||||
*
|
||||
* @experimental This feature is under development and may change in the future.
|
||||
* To use this feature please enable feature flag `product_categories` in your medusa backend project.
|
||||
* @description updates a product category
|
||||
* @returns the updated medusa product category
|
||||
*/
|
||||
export const useAdminUpdateProductCategory = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminProductCategoriesCategoryRes>,
|
||||
Error,
|
||||
AdminPostProductCategoriesCategoryReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation(
|
||||
(payload: AdminPostProductCategoriesCategoryReq) =>
|
||||
client.admin.productCategories.update(id, payload),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminProductCategoryKeys.lists(), adminProductCategoryKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a product category
|
||||
*
|
||||
* @experimental This feature is under development and may change in the future.
|
||||
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
|
||||
* @param id
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminDeleteProductCategory = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminProductCategoriesCategoryDeleteRes>,
|
||||
Error,
|
||||
void
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation(
|
||||
() => client.admin.productCategories.delete(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminProductCategoryKeys.lists(), adminProductCategoryKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Add products to a product category
|
||||
*
|
||||
* @experimental This feature is under development and may change in the future.
|
||||
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
|
||||
* @description Add products to a product category
|
||||
* @param id
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminAddProductsToCategory = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminProductCategoriesCategoryRes>,
|
||||
Error,
|
||||
AdminPostProductCategoriesCategoryProductsBatchReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation(
|
||||
(payload: AdminPostProductCategoriesCategoryProductsBatchReq) => {
|
||||
return client.admin.productCategories.addProducts(id, payload)
|
||||
},
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminProductCategoryKeys.lists(),
|
||||
adminProductCategoryKeys.detail(id),
|
||||
adminProductKeys.list({ product_category_id: [id] }),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove products from a product category
|
||||
* @experimental This feature is under development and may change in the future.
|
||||
* To use this feature please enable featureflag `product_categories` in your medusa backend project.
|
||||
* @description remove products from a product category
|
||||
* @param id
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminDeleteProductsFromCategory = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminProductCategoriesCategoryRes>,
|
||||
Error,
|
||||
AdminDeleteProductCategoriesCategoryProductsBatchReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation(
|
||||
(payload: AdminDeleteProductCategoriesCategoryProductsBatchReq) => {
|
||||
return client.admin.productCategories.removeProducts(id, payload)
|
||||
},
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[
|
||||
adminProductCategoryKeys.lists(),
|
||||
adminProductCategoryKeys.detail(id),
|
||||
adminProductKeys.list({ product_category_id: [id] }),
|
||||
],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
import {
|
||||
AdminGetProductCategoriesParams,
|
||||
AdminProductCategoriesListRes,
|
||||
AdminGetProductCategoryParams,
|
||||
AdminProductCategoriesCategoryRes,
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { UseQueryOptionsWrapper } from "../../../types"
|
||||
import { queryKeysFactory } from "../../utils"
|
||||
|
||||
const ADMIN_PRODUCT_CATEGORIES_QUERY_KEY = `product_categories` as const
|
||||
export const adminProductCategoryKeys = queryKeysFactory(
|
||||
ADMIN_PRODUCT_CATEGORIES_QUERY_KEY
|
||||
)
|
||||
type ProductCategoryQueryKeys = typeof adminProductCategoryKeys
|
||||
|
||||
export const useAdminProductCategories = (
|
||||
query?: AdminGetProductCategoriesParams,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminProductCategoriesListRes>,
|
||||
Error,
|
||||
ReturnType<ProductCategoryQueryKeys["list"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminProductCategoryKeys.list(query),
|
||||
() => client.admin.productCategories.list(query),
|
||||
options
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
|
||||
export const useAdminProductCategory = (
|
||||
id: string,
|
||||
query?: AdminGetProductCategoryParams,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminProductCategoriesCategoryRes>,
|
||||
Error,
|
||||
ReturnType<ProductCategoryQueryKeys["detail"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminProductCategoryKeys.detail(id),
|
||||
() => client.admin.productCategories.retrieve(id, query),
|
||||
options
|
||||
)
|
||||
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
Reference in New Issue
Block a user