feat(medusa, medusa-js, medusa-react): Bulk add Products to a SalesChannel (#1833)
This commit is contained in:
@@ -1735,4 +1735,13 @@ export const adminHandlers = [
|
||||
})
|
||||
)
|
||||
}),
|
||||
|
||||
rest.post("/admin/sales-channels/:id/products/batch", (req, res, ctx) => {
|
||||
return res(
|
||||
ctx.status(200),
|
||||
ctx.json({
|
||||
sales_channel: fixtures.get("sales_channel"),
|
||||
})
|
||||
)
|
||||
}),
|
||||
]
|
||||
|
||||
@@ -3,7 +3,8 @@ import {
|
||||
AdminSalesChannelsRes,
|
||||
AdminPostSalesChannelsSalesChannelReq,
|
||||
AdminSalesChannelsDeleteRes,
|
||||
AdminDeleteSalesChannelsChannelProductsBatchReq
|
||||
AdminDeleteSalesChannelsChannelProductsBatchReq,
|
||||
AdminPostSalesChannelsChannelProductsBatchReq,
|
||||
} from "@medusajs/medusa"
|
||||
import { Response } from "@medusajs/medusa-js"
|
||||
import { useMutation, UseMutationOptions, useQueryClient } from "react-query"
|
||||
@@ -117,4 +118,34 @@ export const useAdminDeleteProductsFromSalesChannel = (
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add products to a sales channel
|
||||
* @experimental This feature is under development and may change in the future.
|
||||
* To use this feature please enable featureflag `sales_channels` in your medusa backend project.
|
||||
* @description Add products to a sales channel
|
||||
* @param id
|
||||
* @param options
|
||||
*/
|
||||
export const useAdminAddProductsToSalesChannel = (
|
||||
id: string,
|
||||
options?: UseMutationOptions<
|
||||
Response<AdminSalesChannelsRes>,
|
||||
Error,
|
||||
AdminPostSalesChannelsChannelProductsBatchReq
|
||||
>
|
||||
) => {
|
||||
const { client } = useMedusa()
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation(
|
||||
(payload: AdminPostSalesChannelsChannelProductsBatchReq) => {
|
||||
return client.admin.salesChannels.addProducts(id, payload)
|
||||
},
|
||||
buildOptions(
|
||||
queryClient,
|
||||
[adminSalesChannelsKeys.lists(), adminSalesChannelsKeys.detail(id)],
|
||||
options
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
useAdminCreateSalesChannel,
|
||||
useAdminUpdateSalesChannel,
|
||||
useAdminDeleteProductsFromSalesChannel,
|
||||
useAdminAddProductsToSalesChannel,
|
||||
} from "../../../../src"
|
||||
import { fixtures } from "../../../../mocks/data"
|
||||
import { createWrapper } from "../../../utils"
|
||||
@@ -107,3 +108,25 @@ describe("useAdminDeleteProductsFromSalesChannel hook", () => {
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
describe("useAdminAddProductsToSalesChannel hook", () => {
|
||||
test("add products to a sales channel", async () => {
|
||||
const id = fixtures.get("sales_channel").id
|
||||
const productId = fixtures.get("product").id
|
||||
|
||||
const { result, waitFor } = renderHook(
|
||||
() => useAdminAddProductsToSalesChannel(id),
|
||||
{ wrapper: createWrapper() }
|
||||
)
|
||||
|
||||
result.current.mutate({ product_ids: [
|
||||
{ id: productId }
|
||||
]})
|
||||
|
||||
await waitFor(() => result.current.isSuccess)
|
||||
|
||||
expect(result.current.data).toEqual(expect.objectContaining({
|
||||
sales_channel: fixtures.get("sales_channel"),
|
||||
}))
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user