|
1
|
import { |
|
2
|
QueryKey, |
|
3
|
UseMutationOptions, |
|
4
|
UseQueryOptions, |
|
5
|
useMutation, |
|
6
|
useQuery, |
|
7
|
} from "@tanstack/react-query" |
|
8
|
|
|
9
|
import { FetchError } from "@medusajs/js-sdk" |
|
10
|
import { HttpTypes } from "@medusajs/types" |
|
11
|
import { sdk } from "../../lib/client" |
|
12
|
import { queryClient } from "../../lib/query-client" |
|
13
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
14
|
import { fulfillmentProvidersQueryKeys } from "./fulfillment-providers" |
|
15
|
|
|
16
|
const STOCK_LOCATIONS_QUERY_KEY = "stock_locations" as const |
|
17
|
export const stockLocationsQueryKeys = queryKeysFactory( |
|
18
|
STOCK_LOCATIONS_QUERY_KEY |
|
19
|
) |
|
20
|
|
|
21
|
export const useStockLocation = ( |
|
22
|
id: string, |
|
23
|
query?: HttpTypes.SelectParams, |
|
24
|
options?: Omit< |
|
25
|
UseQueryOptions< |
|
26
|
HttpTypes.AdminStockLocationResponse, |
|
27
|
FetchError, |
|
28
|
HttpTypes.AdminStockLocationResponse, |
|
29
|
QueryKey |
|
30
|
>, |
|
31
|
"queryKey" | "queryFn" |
|
32
|
> |
|
33
|
) => { |
|
34
|
const { data, ...rest } = useQuery({ |
|
35
|
queryFn: () => sdk.admin.stockLocation.retrieve(id, query), |
|
36
|
queryKey: stockLocationsQueryKeys.detail(id, query), |
|
37
|
...options, |
|
38
|
}) |
|
39
|
|
|
40
|
return { ...data, ...rest } |
|
41
|
} |
|
42
|
|
|
43
|
export const useStockLocations = ( |
|
44
|
query?: HttpTypes.AdminStockLocationListParams, |
|
45
|
options?: Omit< |
|
46
|
UseQueryOptions< |
|
47
|
HttpTypes.AdminStockLocationListResponse, |
|
48
|
FetchError, |
|
49
|
HttpTypes.AdminStockLocationListResponse, |
|
50
|
QueryKey |
|
51
|
>, |
|
52
|
"queryKey" | "queryFn" |
|
53
|
> |
|
54
|
) => { |
|
55
|
const { data, ...rest } = useQuery({ |
|
56
|
queryFn: () => sdk.admin.stockLocation.list(query), |
|
57
|
queryKey: stockLocationsQueryKeys.list(query), |
|
58
|
...options, |
|
59
|
}) |
|
60
|
|
|
61
|
return { ...data, ...rest } |
|
62
|
} |
|
63
|
|
|
64
|
export const useCreateStockLocation = ( |
|
65
|
options?: UseMutationOptions< |
|
66
|
HttpTypes.AdminStockLocationResponse, |
|
67
|
FetchError, |
|
68
|
HttpTypes.AdminCreateStockLocation |
|
69
|
> |
|
70
|
) => { |
|
71
|
return useMutation({ |
|
72
|
mutationFn: (payload) => sdk.admin.stockLocation.create(payload), |
|
73
|
onSuccess: async (data, variables, context) => { |
|
74
|
await queryClient.invalidateQueries({ |
|
75
|
queryKey: stockLocationsQueryKeys.lists(), |
|
76
|
}) |
|
77
|
|
|
78
|
options?.onSuccess?.(data, variables, context) |
|
79
|
}, |
|
80
|
...options, |
|
81
|
}) |
|
82
|
} |
|
83
|
|
|
84
|
export const useUpdateStockLocation = ( |
|
85
|
id: string, |
|
86
|
options?: UseMutationOptions< |
|
87
|
HttpTypes.AdminStockLocationResponse, |
|
88
|
FetchError, |
|
89
|
HttpTypes.AdminUpdateStockLocation |
|
90
|
> |
|
91
|
) => { |
|
92
|
return useMutation({ |
|
93
|
mutationFn: (payload) => sdk.admin.stockLocation.update(id, payload), |
|
94
|
onSuccess: async (data, variables, context) => { |
|
95
|
await queryClient.invalidateQueries({ |
|
96
|
queryKey: stockLocationsQueryKeys.details(), |
|
97
|
}) |
|
98
|
await queryClient.invalidateQueries({ |
|
99
|
queryKey: stockLocationsQueryKeys.lists(), |
|
100
|
}) |
|
101
|
|
|
102
|
options?.onSuccess?.(data, variables, context) |
|
103
|
}, |
|
104
|
...options, |
|
105
|
}) |
|
106
|
} |
|
107
|
|
|
108
|
export const useUpdateStockLocationSalesChannels = ( |
|
109
|
id: string, |
|
110
|
options?: UseMutationOptions< |
|
111
|
HttpTypes.AdminStockLocationResponse, |
|
112
|
FetchError, |
|
113
|
HttpTypes.AdminUpdateStockLocationSalesChannels |
|
114
|
> |
|
115
|
) => { |
|
116
|
return useMutation({ |
|
117
|
mutationFn: (payload) => |
|
118
|
sdk.admin.stockLocation.updateSalesChannels(id, payload), |
|
119
|
onSuccess: async (data, variables, context) => { |
|
120
|
await queryClient.invalidateQueries({ |
|
121
|
queryKey: stockLocationsQueryKeys.details(), |
|
122
|
}) |
|
123
|
await queryClient.invalidateQueries({ |
|
124
|
queryKey: stockLocationsQueryKeys.lists(), |
|
125
|
}) |
|
126
|
|
|
127
|
options?.onSuccess?.(data, variables, context) |
|
128
|
}, |
|
129
|
...options, |
|
130
|
}) |
|
131
|
} |
|
132
|
|
|
133
|
export const useDeleteStockLocation = ( |
|
134
|
id: string, |
|
135
|
options?: UseMutationOptions< |
|
136
|
HttpTypes.AdminStockLocationDeleteResponse, |
|
137
|
FetchError, |
|
138
|
void |
|
139
|
> |
|
140
|
) => { |
|
141
|
return useMutation({ |
|
142
|
mutationFn: () => sdk.admin.stockLocation.delete(id), |
|
143
|
onSuccess: async (data, variables, context) => { |
|
144
|
await queryClient.invalidateQueries({ |
|
145
|
queryKey: stockLocationsQueryKeys.lists(), |
|
146
|
}) |
|
147
|
await queryClient.invalidateQueries({ |
|
148
|
queryKey: stockLocationsQueryKeys.detail(id), |
|
149
|
}) |
|
150
|
|
|
151
|
options?.onSuccess?.(data, variables, context) |
|
152
|
}, |
|
153
|
...options, |
|
154
|
}) |
|
155
|
} |
|
156
|
|
|
157
|
export const useCreateStockLocationFulfillmentSet = ( |
|
158
|
locationId: string, |
|
159
|
options?: UseMutationOptions< |
|
160
|
HttpTypes.AdminStockLocationResponse, |
|
161
|
FetchError, |
|
162
|
HttpTypes.AdminCreateStockLocationFulfillmentSet |
|
163
|
> |
|
164
|
) => { |
|
165
|
return useMutation({ |
|
166
|
mutationFn: (payload) => |
|
167
|
sdk.admin.stockLocation.createFulfillmentSet(locationId, payload), |
|
168
|
onSuccess: async (data, variables, context) => { |
|
169
|
await queryClient.invalidateQueries({ |
|
170
|
queryKey: stockLocationsQueryKeys.all, |
|
171
|
}) |
|
172
|
|
|
173
|
options?.onSuccess?.(data, variables, context) |
|
174
|
}, |
|
175
|
...options, |
|
176
|
}) |
|
177
|
} |
|
178
|
|
|
179
|
export const useUpdateStockLocationFulfillmentProviders = ( |
|
180
|
id: string, |
|
181
|
options?: UseMutationOptions< |
|
182
|
HttpTypes.AdminStockLocationResponse, |
|
183
|
FetchError, |
|
184
|
HttpTypes.AdminBatchLink |
|
185
|
> |
|
186
|
) => { |
|
187
|
return useMutation({ |
|
188
|
mutationFn: async (payload) => |
|
189
|
await sdk.admin.stockLocation.updateFulfillmentProviders(id, payload), |
|
190
|
onSuccess: async (data, variables, context) => { |
|
191
|
await queryClient.invalidateQueries({ |
|
192
|
queryKey: stockLocationsQueryKeys.details(), |
|
193
|
}) |
|
194
|
await queryClient.invalidateQueries({ |
|
195
|
queryKey: fulfillmentProvidersQueryKeys.all, |
|
196
|
}) |
|
197
|
|
|
198
|
options?.onSuccess?.(data, variables, context) |
|
199
|
}, |
|
200
|
...options, |
|
201
|
}) |
|
202
|
} |