feat(medusa,medusa-js,medusa-react): Add BatchJob API support in medusa-js + medusa-react (#1704)
* Add BatchJob API to medusa-js * Adds BatchJob API hooks * Fix tests in medusa-react
This commit is contained in:
committed by
GitHub
parent
5f2744eb9f
commit
7302d76e12
@@ -0,0 +1,2 @@
|
||||
export * from "./queries"
|
||||
export * from "./mutations"
|
||||
@@ -0,0 +1,74 @@
|
||||
import { AdminBatchJobRes, AdminPostBatchesReq } from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { buildOptions } from "../../utils/buildOptions"
|
||||
import { adminBatchJobsKeys } from "./queries"
|
||||
|
||||
/**
|
||||
* Hook returns functions for creating batch jobs.
|
||||
*
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminCreateBatchJob = (
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminBatchJobRes>,
|
||||
Error,
|
||||
AdminPostBatchesReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
(payload: AdminPostBatchesReq) => client.admin.batchJobs.create(payload),
|
||||
buildOptions(queryClient, adminBatchJobsKeys.lists(), options)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook return functions for canceling a batch job
|
||||
*
|
||||
* @param id - id of the batch job
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminCancelBatchJob = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<Response<AdminBatchJobRes>, Error>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.batchJobs.cancel(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminBatchJobsKeys.lists(), adminBatchJobsKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook return functions for confirming a batch job
|
||||
*
|
||||
* @param id - id of the batch job
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminConfirmBatchJob = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<Response<AdminBatchJobRes>, Error>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation(
|
||||
() => client.admin.batchJobs.confirm(id),
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminBatchJobsKeys.lists(), adminBatchJobsKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
50
packages/medusa-react/src/hooks/admin/batch-jobs/queries.ts
Normal file
50
packages/medusa-react/src/hooks/admin/batch-jobs/queries.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import {
|
||||
AdminBatchJobListRes,
|
||||
AdminBatchJobRes,
|
||||
AdminGetBatchParams,
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useQuery } from "react-query"
|
||||
import { useMedusa } from "../../../contexts"
|
||||
import { UseQueryOptionsWrapper } from "../../../types"
|
||||
import { queryKeysFactory } from "../../utils/index"
|
||||
|
||||
const ADMIN_COLLECTIONS_QUERY_KEY = `admin_batches` as const
|
||||
|
||||
export const adminBatchJobsKeys = queryKeysFactory(ADMIN_COLLECTIONS_QUERY_KEY)
|
||||
|
||||
type BatchJobsQueryKey = typeof adminBatchJobsKeys
|
||||
|
||||
export const useAdminBatchJobs = (
|
||||
query?: AdminGetBatchParams,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminBatchJobListRes>,
|
||||
Error,
|
||||
ReturnType<BatchJobsQueryKey["list"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminBatchJobsKeys.list(query),
|
||||
() => client.admin.batchJobs.list(query),
|
||||
options
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
|
||||
export const useAdminBatchJob = (
|
||||
id: string,
|
||||
options?: UseQueryOptionsWrapper<
|
||||
Response<AdminBatchJobRes>,
|
||||
Error,
|
||||
ReturnType<BatchJobsQueryKey["detail"]>
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const { data, ...rest } = useQuery(
|
||||
adminBatchJobsKeys.detail(id),
|
||||
() => client.admin.batchJobs.retrieve(id),
|
||||
options
|
||||
)
|
||||
return { ...data, ...rest } as const
|
||||
}
|
||||
@@ -1,26 +1,27 @@
|
||||
export * from "./auth"
|
||||
export * from "./collections"
|
||||
export * from "./batch-jobs"
|
||||
export * from "./claims"
|
||||
export * from "./customers"
|
||||
export * from "./collections"
|
||||
export * from "./customer-groups"
|
||||
export * from "./customers"
|
||||
export * from "./discounts"
|
||||
export * from "./draft-orders"
|
||||
export * from "./gift-cards"
|
||||
export * from "./invites"
|
||||
export * from "./notes"
|
||||
export * from "./notifications"
|
||||
export * from "./orders"
|
||||
export * from "./products"
|
||||
export * from "./price-lists"
|
||||
export * from "./product-tags"
|
||||
export * from "./product-types"
|
||||
export * from "./price-lists"
|
||||
export * from "./return-reasons"
|
||||
export * from "./products"
|
||||
export * from "./regions"
|
||||
export * from "./return-reasons"
|
||||
export * from "./returns"
|
||||
export * from "./shipping-options"
|
||||
export * from "./shipping-profiles"
|
||||
export * from "./notes"
|
||||
export * from "./invites"
|
||||
export * from "./notifications"
|
||||
export * from "./returns"
|
||||
export * from "./store"
|
||||
export * from "./swaps"
|
||||
export * from "./tax-rates"
|
||||
export * from "./users"
|
||||
export * from "./variants"
|
||||
export * from "./tax-rates"
|
||||
|
||||
Reference in New Issue
Block a user