|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { |
|
4
|
QueryKey, |
|
5
|
UseMutationOptions, |
|
6
|
UseQueryOptions, |
|
7
|
useMutation, |
|
8
|
useQuery, |
|
9
|
} from "@tanstack/react-query" |
|
10
|
|
|
11
|
import { sdk } from "../../lib/client" |
|
12
|
import { queryClient } from "../../lib/query-client" |
|
13
|
import { queryKeysFactory } from "../../lib/query-key-factory" |
|
14
|
import { variantsQueryKeys } from "./products" |
|
15
|
|
|
16
|
const INVENTORY_ITEMS_QUERY_KEY = "inventory_items" as const |
|
17
|
export const inventoryItemsQueryKeys = queryKeysFactory( |
|
18
|
INVENTORY_ITEMS_QUERY_KEY |
|
19
|
) |
|
20
|
|
|
21
|
const INVENTORY_ITEM_LEVELS_QUERY_KEY = "inventory_item_levels" as const |
|
22
|
export const inventoryItemLevelsQueryKeys = queryKeysFactory( |
|
23
|
INVENTORY_ITEM_LEVELS_QUERY_KEY |
|
24
|
) |
|
25
|
|
|
26
|
export const useInventoryItems = ( |
|
27
|
query?: HttpTypes.AdminInventoryItemsParams, |
|
28
|
options?: Omit< |
|
29
|
UseQueryOptions< |
|
30
|
HttpTypes.AdminInventoryItemListResponse, |
|
31
|
FetchError, |
|
32
|
HttpTypes.AdminInventoryItemListResponse, |
|
33
|
QueryKey |
|
34
|
>, |
|
35
|
"queryKey" | "queryFn" |
|
36
|
> |
|
37
|
) => { |
|
38
|
const { data, ...rest } = useQuery({ |
|
39
|
queryFn: () => sdk.admin.inventoryItem.list(query), |
|
40
|
queryKey: inventoryItemsQueryKeys.list(query), |
|
41
|
...options, |
|
42
|
}) |
|
43
|
|
|
44
|
return { ...data, ...rest } |
|
45
|
} |
|
46
|
|
|
47
|
export const useInventoryItem = ( |
|
48
|
id: string, |
|
49
|
query?: Record<string, any>, |
|
50
|
options?: Omit< |
|
51
|
UseQueryOptions< |
|
52
|
HttpTypes.AdminInventoryItemResponse, |
|
53
|
FetchError, |
|
54
|
HttpTypes.AdminInventoryItemResponse, |
|
55
|
QueryKey |
|
56
|
>, |
|
57
|
"queryKey" | "queryFn" |
|
58
|
> |
|
59
|
) => { |
|
60
|
const { data, ...rest } = useQuery({ |
|
61
|
queryFn: () => sdk.admin.inventoryItem.retrieve(id, query), |
|
62
|
queryKey: inventoryItemsQueryKeys.detail(id, query), |
|
63
|
...options, |
|
64
|
}) |
|
65
|
|
|
66
|
return { ...data, ...rest } |
|
67
|
} |
|
68
|
|
|
69
|
export const useExportInventoryItems = ( |
|
70
|
options?: UseMutationOptions< |
|
71
|
HttpTypes.AdminExportInventoryItemResponse, |
|
72
|
FetchError, |
|
73
|
{ |
|
74
|
payload?: HttpTypes.AdminExportInventoryItemRequest |
|
75
|
query?: HttpTypes.AdminInventoryItemsParams |
|
76
|
} |
|
77
|
> |
|
78
|
) => { |
|
79
|
return useMutation({ |
|
80
|
mutationFn: ({ payload = {}, query }) => |
|
81
|
sdk.admin.inventoryItem.export(payload, query), |
|
82
|
onSuccess: (data, variables, context) => { |
|
83
|
options?.onSuccess?.(data, variables, context) |
|
84
|
}, |
|
85
|
...options, |
|
86
|
}) |
|
87
|
} |
|
88
|
|
|
89
|
export const useCreateInventoryItem = ( |
|
90
|
options?: UseMutationOptions< |
|
91
|
HttpTypes.AdminInventoryItemResponse, |
|
92
|
FetchError, |
|
93
|
HttpTypes.AdminCreateInventoryItem |
|
94
|
> |
|
95
|
) => { |
|
96
|
return useMutation({ |
|
97
|
mutationFn: (payload: HttpTypes.AdminCreateInventoryItem) => |
|
98
|
sdk.admin.inventoryItem.create(payload), |
|
99
|
onSuccess: (data, variables, context) => { |
|
100
|
queryClient.invalidateQueries({ |
|
101
|
queryKey: inventoryItemsQueryKeys.lists(), |
|
102
|
}) |
|
103
|
options?.onSuccess?.(data, variables, context) |
|
104
|
}, |
|
105
|
...options, |
|
106
|
}) |
|
107
|
} |
|
108
|
|
|
109
|
export const useUpdateInventoryItem = ( |
|
110
|
id: string, |
|
111
|
options?: UseMutationOptions< |
|
112
|
HttpTypes.AdminInventoryItemResponse, |
|
113
|
FetchError, |
|
114
|
HttpTypes.AdminUpdateInventoryItem |
|
115
|
> |
|
116
|
) => { |
|
117
|
return useMutation({ |
|
118
|
mutationFn: (payload: HttpTypes.AdminUpdateInventoryItem) => |
|
119
|
sdk.admin.inventoryItem.update(id, payload), |
|
120
|
onSuccess: (data, variables, context) => { |
|
121
|
queryClient.invalidateQueries({ |
|
122
|
queryKey: inventoryItemsQueryKeys.lists(), |
|
123
|
}) |
|
124
|
queryClient.invalidateQueries({ |
|
125
|
queryKey: inventoryItemsQueryKeys.detail(id), |
|
126
|
}) |
|
127
|
options?.onSuccess?.(data, variables, context) |
|
128
|
}, |
|
129
|
...options, |
|
130
|
}) |
|
131
|
} |
|
132
|
|
|
133
|
export const useDeleteInventoryItem = ( |
|
134
|
id: string, |
|
135
|
options?: UseMutationOptions< |
|
136
|
HttpTypes.AdminInventoryItemDeleteResponse, |
|
137
|
FetchError, |
|
138
|
void |
|
139
|
> |
|
140
|
) => { |
|
141
|
return useMutation({ |
|
142
|
mutationFn: () => sdk.admin.inventoryItem.delete(id), |
|
143
|
onSuccess: (data, variables, context) => { |
|
144
|
queryClient.invalidateQueries({ |
|
145
|
queryKey: inventoryItemsQueryKeys.lists(), |
|
146
|
}) |
|
147
|
queryClient.invalidateQueries({ |
|
148
|
queryKey: inventoryItemsQueryKeys.detail(id), |
|
149
|
}) |
|
150
|
options?.onSuccess?.(data, variables, context) |
|
151
|
}, |
|
152
|
...options, |
|
153
|
}) |
|
154
|
} |
|
155
|
|
|
156
|
export const useDeleteInventoryItemLevel = ( |
|
157
|
inventoryItemId: string, |
|
158
|
locationId: string, |
|
159
|
options?: UseMutationOptions< |
|
160
|
HttpTypes.AdminInventoryLevelDeleteResponse, |
|
161
|
FetchError, |
|
162
|
void |
|
163
|
> |
|
164
|
) => { |
|
165
|
return useMutation({ |
|
166
|
mutationFn: () => |
|
167
|
sdk.admin.inventoryItem.deleteLevel(inventoryItemId, locationId), |
|
168
|
onSuccess: (data, variables, context) => { |
|
169
|
queryClient.invalidateQueries({ |
|
170
|
queryKey: inventoryItemsQueryKeys.lists(), |
|
171
|
}) |
|
172
|
queryClient.invalidateQueries({ |
|
173
|
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId), |
|
174
|
}) |
|
175
|
queryClient.invalidateQueries({ |
|
176
|
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId), |
|
177
|
}) |
|
178
|
options?.onSuccess?.(data, variables, context) |
|
179
|
}, |
|
180
|
...options, |
|
181
|
}) |
|
182
|
} |
|
183
|
|
|
184
|
export const useInventoryItemLevels = ( |
|
185
|
inventoryItemId: string, |
|
186
|
query?: Record<string, any>, |
|
187
|
options?: Omit< |
|
188
|
UseQueryOptions< |
|
189
|
HttpTypes.AdminInventoryLevelListResponse, |
|
190
|
FetchError, |
|
191
|
HttpTypes.AdminInventoryLevelListResponse, |
|
192
|
QueryKey |
|
193
|
>, |
|
194
|
"queryKey" | "queryFn" |
|
195
|
> |
|
196
|
) => { |
|
197
|
const { data, ...rest } = useQuery({ |
|
198
|
queryFn: () => sdk.admin.inventoryItem.listLevels(inventoryItemId, query), |
|
199
|
queryKey: inventoryItemLevelsQueryKeys.list({ |
|
200
|
...(query || {}), |
|
201
|
inventoryItemId, |
|
202
|
}), |
|
203
|
...options, |
|
204
|
}) |
|
205
|
|
|
206
|
return { ...data, ...rest } |
|
207
|
} |
|
208
|
|
|
209
|
export const useUpdateInventoryLevel = ( |
|
210
|
inventoryItemId: string, |
|
211
|
locationId: string, |
|
212
|
options?: UseMutationOptions< |
|
213
|
HttpTypes.AdminInventoryItemResponse, |
|
214
|
FetchError, |
|
215
|
HttpTypes.AdminUpdateInventoryLevel |
|
216
|
> |
|
217
|
) => { |
|
218
|
return useMutation({ |
|
219
|
mutationFn: (payload: HttpTypes.AdminUpdateInventoryLevel) => |
|
220
|
sdk.admin.inventoryItem.updateLevel(inventoryItemId, locationId, payload), |
|
221
|
onSuccess: (data, variables, context) => { |
|
222
|
queryClient.invalidateQueries({ |
|
223
|
queryKey: inventoryItemsQueryKeys.lists(), |
|
224
|
}) |
|
225
|
queryClient.invalidateQueries({ |
|
226
|
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId), |
|
227
|
}) |
|
228
|
queryClient.invalidateQueries({ |
|
229
|
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId), |
|
230
|
}) |
|
231
|
queryClient.invalidateQueries({ |
|
232
|
queryKey: variantsQueryKeys.details(), |
|
233
|
}) |
|
234
|
options?.onSuccess?.(data, variables, context) |
|
235
|
}, |
|
236
|
...options, |
|
237
|
}) |
|
238
|
} |
|
239
|
|
|
240
|
export const useBatchInventoryItemLocationLevels = ( |
|
241
|
inventoryItemId: string, |
|
242
|
options?: UseMutationOptions< |
|
243
|
HttpTypes.AdminBatchInventoryItemLocationLevelsResponse, |
|
244
|
FetchError, |
|
245
|
HttpTypes.AdminBatchInventoryItemLocationLevels |
|
246
|
> |
|
247
|
) => { |
|
248
|
return useMutation({ |
|
249
|
mutationFn: (payload) => |
|
250
|
sdk.admin.inventoryItem.batchInventoryItemLocationLevels( |
|
251
|
inventoryItemId, |
|
252
|
payload |
|
253
|
), |
|
254
|
onSuccess: (data, variables, context) => { |
|
255
|
queryClient.invalidateQueries({ |
|
256
|
queryKey: inventoryItemsQueryKeys.lists(), |
|
257
|
}) |
|
258
|
queryClient.invalidateQueries({ |
|
259
|
queryKey: inventoryItemsQueryKeys.detail(inventoryItemId), |
|
260
|
}) |
|
261
|
queryClient.invalidateQueries({ |
|
262
|
queryKey: inventoryItemLevelsQueryKeys.detail(inventoryItemId), |
|
263
|
}) |
|
264
|
queryClient.invalidateQueries({ |
|
265
|
queryKey: inventoryItemLevelsQueryKeys.list({ inventoryItemId }), |
|
266
|
}) |
|
267
|
options?.onSuccess?.(data, variables, context) |
|
268
|
}, |
|
269
|
...options, |
|
270
|
}) |
|
271
|
} |
|
272
|
|
|
273
|
export const useBatchInventoryItemsLocationLevels = ( |
|
274
|
options?: UseMutationOptions< |
|
275
|
HttpTypes.AdminBatchInventoryItemsLocationLevelsResponse, |
|
276
|
FetchError, |
|
277
|
HttpTypes.AdminBatchInventoryItemsLocationLevels |
|
278
|
> |
|
279
|
) => { |
|
280
|
return useMutation({ |
|
281
|
mutationFn: (payload) => |
|
282
|
sdk.admin.inventoryItem.batchInventoryItemsLocationLevels(payload), |
|
283
|
onSuccess: (data, variables, context) => { |
|
284
|
queryClient.invalidateQueries({ |
|
285
|
queryKey: inventoryItemsQueryKeys.all, |
|
286
|
}) |
|
287
|
queryClient.invalidateQueries({ |
|
288
|
queryKey: variantsQueryKeys.lists(), |
|
289
|
}) |
|
290
|
options?.onSuccess?.(data, variables, context) |
|
291
|
}, |
|
292
|
...options, |
|
293
|
}) |
|
294
|
} |