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 } from "@tanstack/react-query"
14 import { sdk } from "../../lib/client"
15 import { queryClient } from "../../lib/query-client"
16 import { queryKeysFactory } from "../../lib/query-key-factory"
17 import { productsQueryKeys } from "./products"
18
19 const SALES_CHANNELS_QUERY_KEY = "sales-channels" as const
20 export const salesChannelsQueryKeys = queryKeysFactory(SALES_CHANNELS_QUERY_KEY)
21
22 export const useSalesChannel = (
23 id: string,
24 options?: Omit<
25 UseQueryOptions<
26 AdminSalesChannelResponse,
27 FetchError,
28 AdminSalesChannelResponse,
29 QueryKey
30 >,
31 "queryFn" | "queryKey"
32 >
33 ) => {
34 const { data, ...rest } = useQuery({
35 queryKey: salesChannelsQueryKeys.detail(id),
36 queryFn: async () => sdk.admin.salesChannel.retrieve(id),
37 ...options,
38 })
39
40 return { ...data, ...rest }
41 }
42
43 export const useSalesChannels = (
44 query?: HttpTypes.AdminSalesChannelListParams,
45 options?: Omit<
46 UseQueryOptions<
47 AdminSalesChannelListResponse,
48 FetchError,
49 AdminSalesChannelListResponse,
50 QueryKey
51 >,
52 "queryFn" | "queryKey"
53 >
54 ) => {
55 const { data, ...rest } = useQuery({
56 queryFn: () => sdk.admin.salesChannel.list(query),
57 queryKey: salesChannelsQueryKeys.list(query),
58 ...options,
59 })
60
61 return { ...data, ...rest }
62 }
63
64 export const useCreateSalesChannel = (
65 options?: UseMutationOptions<
66 AdminSalesChannelResponse,
67 FetchError,
68 HttpTypes.AdminCreateSalesChannel
69 >
70 ) => {
71 return useMutation({
72 mutationFn: (payload) => sdk.admin.salesChannel.create(payload),
73 onSuccess: (data, variables, context) => {
74 queryClient.invalidateQueries({
75 queryKey: salesChannelsQueryKeys.lists(),
76 })
77 options?.onSuccess?.(data, variables, context)
78 },
79 ...options,
80 })
81 }
82
83 export const useUpdateSalesChannel = (
84 id: string,
85 options?: UseMutationOptions<
86 AdminSalesChannelResponse,
87 FetchError,
88 HttpTypes.AdminUpdateSalesChannel
89 >
90 ) => {
91 return useMutation({
92 mutationFn: (payload) => sdk.admin.salesChannel.update(id, payload),
93 onSuccess: (data, variables, context) => {
94 queryClient.invalidateQueries({
95 queryKey: salesChannelsQueryKeys.lists(),
96 })
97 queryClient.invalidateQueries({
98 queryKey: salesChannelsQueryKeys.detail(id),
99 })
100
101 options?.onSuccess?.(data, variables, context)
102 },
103 ...options,
104 })
105 }
106
107 export const useDeleteSalesChannel = (
108 id: string,
109 options?: UseMutationOptions<
110 HttpTypes.AdminSalesChannelDeleteResponse,
111 FetchError,
112 void
113 >
114 ) => {
115 return useMutation({
116 mutationFn: () => sdk.admin.salesChannel.delete(id),
117 onSuccess: (data, variables, context) => {
118 queryClient.invalidateQueries({
119 queryKey: salesChannelsQueryKeys.lists(),
120 })
121 queryClient.invalidateQueries({
122 queryKey: salesChannelsQueryKeys.detail(id),
123 })
124
125 // Invalidate all products to ensure they are updated if they were linked to the sales channel
126 queryClient.invalidateQueries({
127 queryKey: productsQueryKeys.all,
128 })
129
130 options?.onSuccess?.(data, variables, context)
131 },
132 ...options,
133 })
134 }
135
136 export const useDeleteSalesChannelLazy = (
137 options?: UseMutationOptions<
138 HttpTypes.AdminSalesChannelDeleteResponse,
139 FetchError,
140 string
141 >
142 ) => {
143 return useMutation({
144 mutationFn: (id: string) => sdk.admin.salesChannel.delete(id),
145 onSuccess: (data, variables, context) => {
146 queryClient.invalidateQueries({
147 queryKey: salesChannelsQueryKeys.lists(),
148 })
149 queryClient.invalidateQueries({
150 queryKey: salesChannelsQueryKeys.detail(variables),
151 })
152
153 // Invalidate all products to ensure they are updated if they were linked to the sales channel
154 queryClient.invalidateQueries({
155 queryKey: productsQueryKeys.all,
156 })
157
158 options?.onSuccess?.(data, variables, context)
159 },
160 ...options,
161 })
162 }
163
164 export const useSalesChannelRemoveProducts = (
165 id: string,
166 options?: UseMutationOptions<
167 AdminSalesChannelResponse,
168 FetchError,
169 HttpTypes.AdminBatchLink["remove"]
170 >
171 ) => {
172 return useMutation({
173 mutationFn: (payload) =>
174 sdk.admin.salesChannel.batchProducts(id, { remove: payload }),
175 onSuccess: (data, variables, context) => {
176 queryClient.invalidateQueries({
177 queryKey: salesChannelsQueryKeys.lists(),
178 })
179 queryClient.invalidateQueries({
180 queryKey: salesChannelsQueryKeys.detail(id),
181 })
182
183 // Invalidate the products that were removed
184 for (const product of variables || []) {
185 queryClient.invalidateQueries({
186 queryKey: productsQueryKeys.detail(product),
187 })
188 }
189
190 // Invalidate the products list query
191 queryClient.invalidateQueries({
192 queryKey: productsQueryKeys.lists(),
193 })
194
195 options?.onSuccess?.(data, variables, context)
196 },
197 ...options,
198 })
199 }
200
201 export const useSalesChannelAddProducts = (
202 id: string,
203 options?: UseMutationOptions<
204 AdminSalesChannelResponse,
205 FetchError,
206 HttpTypes.AdminBatchLink["add"]
207 >
208 ) => {
209 return useMutation({
210 mutationFn: (payload) =>
211 sdk.admin.salesChannel.batchProducts(id, { add: payload }),
212 onSuccess: (data, variables, context) => {
213 queryClient.invalidateQueries({
214 queryKey: salesChannelsQueryKeys.lists(),
215 })
216 queryClient.invalidateQueries({
217 queryKey: salesChannelsQueryKeys.detail(id),
218 })
219
220 // Invalidate the products that were removed
221 for (const product of variables || []) {
222 queryClient.invalidateQueries({
223 queryKey: productsQueryKeys.detail(product),
224 })
225 }
226
227 // Invalidate the products list query
228 queryClient.invalidateQueries({
229 queryKey: productsQueryKeys.lists(),
230 })
231
232 options?.onSuccess?.(data, variables, context)
233 },
234 ...options,
235 })
236 }