|
1
|
import { |
|
2
|
QueryKey, |
|
3
|
useMutation, |
|
4
|
useQuery, |
|
5
|
useQueryClient, |
|
6
|
UseQueryOptions, |
|
7
|
} from "@tanstack/react-query" |
|
8
|
|
|
9
|
import { FetchError } from "@medusajs/js-sdk" |
|
10
|
import { HttpTypes } from "@medusajs/types" |
|
11
|
import { UseMutationOptions } from "@tanstack/react-query" |
|
12
|
|
|
13
|
import { |
|
14
|
AdminOrderEditAddShippingMethod, |
|
15
|
AdminOrderEditUpdateShippingMethod, |
|
16
|
} from "../../../types/http/orders/requests" |
|
17
|
import { sdk } from "../../lib/queries/sdk" |
|
18
|
import { draftOrdersQueryKeys } from "./draft-orders" |
|
19
|
|
|
20
|
const ORDERS_QUERY_KEY = "orders" |
|
21
|
|
|
22
|
export const ordersQueryKeys = { |
|
23
|
detail: (id: string, query?: Record<string, any>) => [ |
|
24
|
ORDERS_QUERY_KEY, |
|
25
|
"details", |
|
26
|
id, |
|
27
|
query ? { query } : undefined, |
|
28
|
], |
|
29
|
details: () => [ORDERS_QUERY_KEY, "details"], |
|
30
|
list: (query?: Record<string, any>) => [ |
|
31
|
ORDERS_QUERY_KEY, |
|
32
|
"lists", |
|
33
|
query ? { query } : undefined, |
|
34
|
], |
|
35
|
lists: () => [ORDERS_QUERY_KEY, "lists"], |
|
36
|
preview: (id: string) => [ORDERS_QUERY_KEY, "preview", id], |
|
37
|
changes: (id: string) => [ORDERS_QUERY_KEY, "changes", id], |
|
38
|
} |
|
39
|
|
|
40
|
export const useOrder = ( |
|
41
|
id: string, |
|
42
|
query?: HttpTypes.AdminOrderFilters, |
|
43
|
options?: Omit< |
|
44
|
UseQueryOptions< |
|
45
|
HttpTypes.AdminOrderResponse, |
|
46
|
FetchError, |
|
47
|
HttpTypes.AdminOrderResponse, |
|
48
|
QueryKey |
|
49
|
>, |
|
50
|
"queryFn" | "queryKey" |
|
51
|
> |
|
52
|
) => { |
|
53
|
const { data, ...rest } = useQuery({ |
|
54
|
queryKey: ordersQueryKeys.detail(id, query), |
|
55
|
queryFn: async () => sdk.admin.order.retrieve(id, query), |
|
56
|
...options, |
|
57
|
}) |
|
58
|
|
|
59
|
return { ...data, ...rest } |
|
60
|
} |
|
61
|
|
|
62
|
export const useOrderChanges = ( |
|
63
|
id: string, |
|
64
|
query?: HttpTypes.AdminOrderChangesFilters, |
|
65
|
options?: Omit< |
|
66
|
UseQueryOptions< |
|
67
|
HttpTypes.PaginatedResponse<HttpTypes.AdminOrderChangesResponse>, |
|
68
|
FetchError, |
|
69
|
HttpTypes.PaginatedResponse<HttpTypes.AdminOrderChangesResponse>, |
|
70
|
QueryKey |
|
71
|
>, |
|
72
|
"queryFn" | "queryKey" |
|
73
|
> |
|
74
|
) => { |
|
75
|
const { data, ...rest } = useQuery({ |
|
76
|
queryFn: async () => sdk.admin.order.listChanges(id, query), |
|
77
|
queryKey: ordersQueryKeys.changes(id), |
|
78
|
...options, |
|
79
|
}) |
|
80
|
|
|
81
|
return { ...data, ...rest } |
|
82
|
} |
|
83
|
|
|
84
|
export const useUpdateOrder = ( |
|
85
|
id: string, |
|
86
|
options?: UseMutationOptions< |
|
87
|
HttpTypes.AdminOrderResponse, |
|
88
|
FetchError, |
|
89
|
HttpTypes.AdminUpdateOrder |
|
90
|
> |
|
91
|
) => { |
|
92
|
const queryClient = useQueryClient() |
|
93
|
|
|
94
|
return useMutation({ |
|
95
|
mutationFn: (payload) => sdk.admin.order.update(id, payload), |
|
96
|
onSuccess: (data: any, variables: any, context: any) => { |
|
97
|
queryClient.invalidateQueries({ |
|
98
|
queryKey: ordersQueryKeys.detail(id), |
|
99
|
}) |
|
100
|
queryClient.invalidateQueries({ |
|
101
|
queryKey: ordersQueryKeys.changes(id), |
|
102
|
}) |
|
103
|
queryClient.invalidateQueries({ |
|
104
|
queryKey: ordersQueryKeys.lists(), |
|
105
|
}) |
|
106
|
|
|
107
|
options?.onSuccess?.(data, variables, context) |
|
108
|
}, |
|
109
|
...options, |
|
110
|
}) |
|
111
|
} |
|
112
|
|
|
113
|
export const useOrderPreview = ( |
|
114
|
id: string, |
|
115
|
query?: HttpTypes.AdminOrderFilters, |
|
116
|
options?: Omit< |
|
117
|
UseQueryOptions< |
|
118
|
HttpTypes.AdminOrderPreviewResponse, |
|
119
|
FetchError, |
|
120
|
HttpTypes.AdminOrderPreviewResponse, |
|
121
|
QueryKey |
|
122
|
>, |
|
123
|
"queryFn" | "queryKey" |
|
124
|
> |
|
125
|
) => { |
|
126
|
const { data, ...rest } = useQuery({ |
|
127
|
queryFn: async () => sdk.admin.order.retrievePreview(id, query), |
|
128
|
queryKey: ordersQueryKeys.preview(id), |
|
129
|
...options, |
|
130
|
}) |
|
131
|
|
|
132
|
return { ...data, ...rest } |
|
133
|
} |
|
134
|
|
|
135
|
export const useOrderEditCreate = ( |
|
136
|
id: string, |
|
137
|
options?: UseMutationOptions< |
|
138
|
HttpTypes.AdminOrderEditResponse, |
|
139
|
FetchError, |
|
140
|
HttpTypes.AdminInitiateOrderEditRequest |
|
141
|
> |
|
142
|
) => { |
|
143
|
const queryClient = useQueryClient() |
|
144
|
|
|
145
|
return useMutation({ |
|
146
|
mutationFn: async (payload) => sdk.admin.orderEdit.initiateRequest(payload), |
|
147
|
onSuccess: (data, variables, context) => { |
|
148
|
queryClient.invalidateQueries({ |
|
149
|
queryKey: ordersQueryKeys.detail(id), |
|
150
|
}) |
|
151
|
queryClient.invalidateQueries({ |
|
152
|
queryKey: ordersQueryKeys.preview(id), |
|
153
|
}) |
|
154
|
|
|
155
|
options?.onSuccess?.(data, variables, context) |
|
156
|
}, |
|
157
|
...options, |
|
158
|
}) |
|
159
|
} |
|
160
|
|
|
161
|
export const useOrderEditCancel = ( |
|
162
|
id: string, |
|
163
|
options?: UseMutationOptions< |
|
164
|
HttpTypes.AdminOrderEditDeleteResponse, |
|
165
|
FetchError, |
|
166
|
void |
|
167
|
> |
|
168
|
) => { |
|
169
|
const queryClient = useQueryClient() |
|
170
|
|
|
171
|
return useMutation({ |
|
172
|
mutationFn: async () => await sdk.admin.orderEdit.cancelRequest(id), |
|
173
|
onSuccess: (data, variables, context) => { |
|
174
|
queryClient.invalidateQueries({ |
|
175
|
queryKey: ordersQueryKeys.details(), |
|
176
|
}) |
|
177
|
|
|
178
|
queryClient.invalidateQueries({ |
|
179
|
queryKey: ordersQueryKeys.preview(id), |
|
180
|
}) |
|
181
|
|
|
182
|
queryClient.invalidateQueries({ |
|
183
|
queryKey: ordersQueryKeys.changes(id), |
|
184
|
}) |
|
185
|
|
|
186
|
options?.onSuccess?.(data, variables, context) |
|
187
|
}, |
|
188
|
...options, |
|
189
|
}) |
|
190
|
} |
|
191
|
|
|
192
|
export const useOrderEditRequest = ( |
|
193
|
id: string, |
|
194
|
options?: UseMutationOptions< |
|
195
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
196
|
FetchError, |
|
197
|
void |
|
198
|
> |
|
199
|
) => { |
|
200
|
const queryClient = useQueryClient() |
|
201
|
|
|
202
|
return useMutation({ |
|
203
|
mutationFn: () => sdk.admin.orderEdit.request(id), |
|
204
|
onSuccess: (data, variables, context) => { |
|
205
|
queryClient.invalidateQueries({ |
|
206
|
queryKey: ordersQueryKeys.details(), |
|
207
|
}) |
|
208
|
|
|
209
|
queryClient.invalidateQueries({ |
|
210
|
queryKey: ordersQueryKeys.preview(id), |
|
211
|
}) |
|
212
|
|
|
213
|
queryClient.invalidateQueries({ |
|
214
|
queryKey: ordersQueryKeys.changes(id), |
|
215
|
}) |
|
216
|
|
|
217
|
options?.onSuccess?.(data, variables, context) |
|
218
|
}, |
|
219
|
...options, |
|
220
|
}) |
|
221
|
} |
|
222
|
|
|
223
|
export const useOrderEditConfirm = ( |
|
224
|
id: string, |
|
225
|
options?: UseMutationOptions< |
|
226
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
227
|
FetchError, |
|
228
|
void |
|
229
|
> |
|
230
|
) => { |
|
231
|
const queryClient = useQueryClient() |
|
232
|
|
|
233
|
return useMutation({ |
|
234
|
mutationFn: () => sdk.admin.orderEdit.confirm(id), |
|
235
|
onSuccess: (data, variables, context) => { |
|
236
|
queryClient.invalidateQueries({ |
|
237
|
queryKey: ordersQueryKeys.details(), |
|
238
|
}) |
|
239
|
|
|
240
|
queryClient.invalidateQueries({ |
|
241
|
queryKey: draftOrdersQueryKeys.details(), |
|
242
|
}) |
|
243
|
|
|
244
|
queryClient.invalidateQueries({ |
|
245
|
queryKey: ordersQueryKeys.preview(id), |
|
246
|
}) |
|
247
|
|
|
248
|
queryClient.invalidateQueries({ |
|
249
|
queryKey: ordersQueryKeys.changes(id), |
|
250
|
}) |
|
251
|
|
|
252
|
options?.onSuccess?.(data, variables, context) |
|
253
|
}, |
|
254
|
...options, |
|
255
|
}) |
|
256
|
} |
|
257
|
|
|
258
|
export const useOrderEditAddItems = ( |
|
259
|
id: string, |
|
260
|
options?: UseMutationOptions< |
|
261
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
262
|
FetchError, |
|
263
|
HttpTypes.AdminAddOrderEditItems |
|
264
|
> |
|
265
|
) => { |
|
266
|
const queryClient = useQueryClient() |
|
267
|
|
|
268
|
return useMutation({ |
|
269
|
mutationFn: (payload: HttpTypes.AdminAddOrderEditItems) => |
|
270
|
sdk.admin.orderEdit.addItems(id, payload), |
|
271
|
onSuccess: (data: any, variables: any, context: any) => { |
|
272
|
queryClient.invalidateQueries({ |
|
273
|
queryKey: ordersQueryKeys.preview(id), |
|
274
|
}) |
|
275
|
queryClient.invalidateQueries({ |
|
276
|
queryKey: ordersQueryKeys.changes(id), |
|
277
|
}) |
|
278
|
|
|
279
|
options?.onSuccess?.(data, variables, context) |
|
280
|
}, |
|
281
|
...options, |
|
282
|
}) |
|
283
|
} |
|
284
|
|
|
285
|
export const useOrderEditAddShippingMethod = ( |
|
286
|
id: string, |
|
287
|
options?: UseMutationOptions< |
|
288
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
289
|
FetchError, |
|
290
|
AdminOrderEditAddShippingMethod |
|
291
|
> |
|
292
|
) => { |
|
293
|
const queryClient = useQueryClient() |
|
294
|
|
|
295
|
return useMutation({ |
|
296
|
mutationFn: (payload) => { |
|
297
|
return sdk.client.fetch(`/admin/order-edits/${id}/shipping-method`, { |
|
298
|
method: "POST", |
|
299
|
body: payload, |
|
300
|
headers: { |
|
301
|
"Content-Type": "application/json", |
|
302
|
}, |
|
303
|
credentials: "include", |
|
304
|
}) |
|
305
|
}, |
|
306
|
onSuccess: (data, variables, context) => { |
|
307
|
queryClient.invalidateQueries({ |
|
308
|
queryKey: ordersQueryKeys.preview(id), |
|
309
|
}) |
|
310
|
options?.onSuccess?.(data, variables, context) |
|
311
|
}, |
|
312
|
...options, |
|
313
|
}) |
|
314
|
} |
|
315
|
|
|
316
|
export const useOrderEditDeleteShippingMethod = ( |
|
317
|
id: string, |
|
318
|
options?: UseMutationOptions< |
|
319
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
320
|
FetchError, |
|
321
|
string |
|
322
|
> |
|
323
|
) => { |
|
324
|
const queryClient = useQueryClient() |
|
325
|
|
|
326
|
return useMutation({ |
|
327
|
mutationFn: (action_id: string) => { |
|
328
|
return sdk.client.fetch( |
|
329
|
`/admin/order-edits/${id}/shipping-method/${action_id}`, |
|
330
|
{ |
|
331
|
method: "DELETE", |
|
332
|
credentials: "include", |
|
333
|
} |
|
334
|
) as Promise<HttpTypes.AdminOrderEditPreviewResponse> |
|
335
|
}, |
|
336
|
onSuccess: (data, variables, context) => { |
|
337
|
queryClient.invalidateQueries({ |
|
338
|
queryKey: ordersQueryKeys.preview(id), |
|
339
|
}) |
|
340
|
options?.onSuccess?.(data, variables, context) |
|
341
|
}, |
|
342
|
...options, |
|
343
|
}) |
|
344
|
} |
|
345
|
|
|
346
|
export const useOrderEditUpdateShippingMethod = ( |
|
347
|
id: string, |
|
348
|
options?: UseMutationOptions< |
|
349
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
350
|
FetchError, |
|
351
|
AdminOrderEditUpdateShippingMethod & { action_id: string } |
|
352
|
> |
|
353
|
) => { |
|
354
|
const queryClient = useQueryClient() |
|
355
|
|
|
356
|
return useMutation({ |
|
357
|
mutationFn: ({ action_id, ...payload }) => { |
|
358
|
return sdk.client.fetch( |
|
359
|
`/admin/order-edits/${id}/shipping-method/${action_id}`, |
|
360
|
{ |
|
361
|
method: "POST", |
|
362
|
body: payload, |
|
363
|
headers: { |
|
364
|
"Content-Type": "application/json", |
|
365
|
}, |
|
366
|
credentials: "include", |
|
367
|
} |
|
368
|
) |
|
369
|
}, |
|
370
|
onSuccess: (data, variables, context) => { |
|
371
|
queryClient.invalidateQueries({ |
|
372
|
queryKey: ordersQueryKeys.preview(id), |
|
373
|
}) |
|
374
|
options?.onSuccess?.(data, variables, context) |
|
375
|
}, |
|
376
|
...options, |
|
377
|
}) |
|
378
|
} |
|
379
|
|
|
380
|
export const useOrderEditUpdateActionItem = ( |
|
381
|
id: string, |
|
382
|
options?: UseMutationOptions< |
|
383
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
384
|
FetchError, |
|
385
|
HttpTypes.AdminUpdateOrderEditItem & { |
|
386
|
action_id: string |
|
387
|
unit_price?: number | null |
|
388
|
compare_at_unit_price?: number | null |
|
389
|
} |
|
390
|
> |
|
391
|
) => { |
|
392
|
const queryClient = useQueryClient() |
|
393
|
|
|
394
|
return useMutation({ |
|
395
|
mutationFn: ({ action_id, ...payload }) => { |
|
396
|
return sdk.admin.orderEdit.updateAddedItem(id, action_id, payload) |
|
397
|
}, |
|
398
|
onSuccess: (data, variables, context) => { |
|
399
|
queryClient.invalidateQueries({ |
|
400
|
queryKey: ordersQueryKeys.preview(id), |
|
401
|
}) |
|
402
|
options?.onSuccess?.(data, variables, context) |
|
403
|
}, |
|
404
|
...options, |
|
405
|
}) |
|
406
|
} |
|
407
|
|
|
408
|
export const useOrderEditUpdateOriginalItem = ( |
|
409
|
id: string, |
|
410
|
options?: UseMutationOptions< |
|
411
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
412
|
FetchError, |
|
413
|
HttpTypes.AdminUpdateOrderEditItem & { |
|
414
|
item_id: string |
|
415
|
unit_price?: number | null |
|
416
|
compare_at_unit_price?: number | null |
|
417
|
} |
|
418
|
> |
|
419
|
) => { |
|
420
|
const queryClient = useQueryClient() |
|
421
|
|
|
422
|
return useMutation({ |
|
423
|
mutationFn: ({ item_id, ...payload }) => { |
|
424
|
return sdk.admin.orderEdit.updateOriginalItem(id, item_id, payload) |
|
425
|
}, |
|
426
|
onSuccess: (data, variables, context) => { |
|
427
|
queryClient.invalidateQueries({ |
|
428
|
queryKey: ordersQueryKeys.preview(id), |
|
429
|
}) |
|
430
|
options?.onSuccess?.(data, variables, context) |
|
431
|
}, |
|
432
|
...options, |
|
433
|
}) |
|
434
|
} |
|
435
|
|
|
436
|
export const useOrderEditRemoveActionItem = ( |
|
437
|
id: string, |
|
438
|
options?: UseMutationOptions< |
|
439
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
440
|
FetchError, |
|
441
|
string |
|
442
|
> |
|
443
|
) => { |
|
444
|
const queryClient = useQueryClient() |
|
445
|
|
|
446
|
return useMutation({ |
|
447
|
mutationFn: (action_id: string) => |
|
448
|
sdk.admin.orderEdit.removeAddedItem(id, action_id), |
|
449
|
onSuccess: (data, variables, context) => { |
|
450
|
queryClient.invalidateQueries({ |
|
451
|
queryKey: ordersQueryKeys.preview(id), |
|
452
|
}) |
|
453
|
options?.onSuccess?.(data, variables, context) |
|
454
|
}, |
|
455
|
...options, |
|
456
|
}) |
|
457
|
} |