Raw
1 import { FetchError } from "@medusajs/js-sdk";
2 import {
3 AdminSalesChannelListResponse,
4 AdminSalesChannelResponse,
5 HttpTypes,
6 } from "@medusajs/types";
7 import {
8 QueryKey,
9 UseMutationOptions,
10 UseQueryOptions,
11 useMutation,
12 useQuery,
13 useQueryClient,
14 } from "@tanstack/react-query";
15 import { queryKeysFactory } from "../../lib/query-key";
16 import { sdk } from "../../lib/sdk";
17 import { productsQueryKeys } from "./products";
18
19 const SALES_CHANNELS_QUERY_KEY = "sales-channels" as const;
20 export const salesChannelsQueryKeys = queryKeysFactory(
21 SALES_CHANNELS_QUERY_KEY
22 );
23
24 export const useSalesChannel = (
25 id: string,
26 options?: Omit<
27 UseQueryOptions<
28 AdminSalesChannelResponse,
29 FetchError,
30 AdminSalesChannelResponse,
31 QueryKey
32 >,
33 "queryFn" | "queryKey"
34 >
35 ) => {
36 const { data, ...rest } = useQuery({
37 queryKey: salesChannelsQueryKeys.detail(id),
38 queryFn: async () => sdk.admin.salesChannel.retrieve(id),
39 ...options,
40 });
41
42 return { ...data, ...rest };
43 };
44
45 export const useSalesChannels = (
46 query?: HttpTypes.AdminSalesChannelListParams,
47 options?: Omit<
48 UseQueryOptions<
49 AdminSalesChannelListResponse,
50 FetchError,
51 AdminSalesChannelListResponse,
52 QueryKey
53 >,
54 "queryFn" | "queryKey"
55 >
56 ) => {
57 const { data, ...rest } = useQuery({
58 queryFn: () => sdk.admin.salesChannel.list(query),
59 queryKey: salesChannelsQueryKeys.list(query),
60 ...options,
61 });
62
63 return { ...data, ...rest };
64 };
65
66 export const useSalesChannelRemoveProducts = (
67 id: string,
68 options?: UseMutationOptions<
69 AdminSalesChannelResponse,
70 FetchError,
71 HttpTypes.AdminBatchLink["remove"]
72 >
73 ) => {
74 const queryClient = useQueryClient();
75
76 return useMutation({
77 mutationFn: (payload) =>
78 sdk.admin.salesChannel.batchProducts(id, { remove: payload }),
79 onSuccess: (data, variables, context) => {
80 queryClient.invalidateQueries({
81 queryKey: productsQueryKeys.lists(),
82 });
83
84 queryClient.invalidateQueries({
85 queryKey: productsQueryKeys.details(),
86 });
87
88 options?.onSuccess?.(data, variables, context);
89 },
90 ...options,
91 });
92 };
93
94 export const useSalesChannelAddProducts = (
95 id: string,
96 options?: UseMutationOptions<
97 AdminSalesChannelResponse,
98 FetchError,
99 HttpTypes.AdminBatchLink["add"]
100 >
101 ) => {
102 return useMutation({
103 mutationFn: (payload) =>
104 sdk.admin.salesChannel.batchProducts(id, { add: payload }),
105 onSuccess: (data, variables, context) => {
106 const queryClient = useQueryClient();
107
108 queryClient.invalidateQueries({
109 queryKey: salesChannelsQueryKeys.lists(),
110 });
111 queryClient.invalidateQueries({
112 queryKey: salesChannelsQueryKeys.detail(id),
113 });
114
115 // Invalidate the products that were removed
116 for (const product of variables || []) {
117 queryClient.invalidateQueries({
118 queryKey: productsQueryKeys.detail(product),
119 });
120 }
121
122 // Invalidate the products list query
123 queryClient.invalidateQueries({
124 queryKey: productsQueryKeys.lists(),
125 });
126
127 options?.onSuccess?.(data, variables, context);
128 },
129 ...options,
130 });
131 };