|
1
|
import { FetchError } from "@medusajs/js-sdk" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { |
|
4
|
QueryKey, |
|
5
|
useMutation, |
|
6
|
UseMutationOptions, |
|
7
|
useQuery, |
|
8
|
useQueryClient, |
|
9
|
UseQueryOptions, |
|
10
|
} from "@tanstack/react-query" |
|
11
|
import { sdk } from "../../lib/queries/sdk" |
|
12
|
import { ordersQueryKeys } from "./orders" |
|
13
|
import { shippingOptionsQueryKeys } from "./shipping-options" |
|
14
|
const DRAFT_ORDERS_QUERY_KEY = "draft-orders" |
|
15
|
|
|
16
|
export const draftOrdersQueryKeys = { |
|
17
|
detail: (id: string, query?: Record<string, any>) => [ |
|
18
|
DRAFT_ORDERS_QUERY_KEY, |
|
19
|
"details", |
|
20
|
id, |
|
21
|
query ? { query } : undefined, |
|
22
|
], |
|
23
|
details: () => [DRAFT_ORDERS_QUERY_KEY, "details"], |
|
24
|
list: (query?: Record<string, any>) => [ |
|
25
|
DRAFT_ORDERS_QUERY_KEY, |
|
26
|
"lists", |
|
27
|
query ? { query } : undefined, |
|
28
|
], |
|
29
|
lists: () => [DRAFT_ORDERS_QUERY_KEY, "lists"], |
|
30
|
} |
|
31
|
|
|
32
|
export const useDraftOrders = ( |
|
33
|
query?: HttpTypes.AdminOrderFilters, |
|
34
|
options?: Omit< |
|
35
|
UseQueryOptions< |
|
36
|
HttpTypes.AdminDraftOrderListResponse, |
|
37
|
FetchError, |
|
38
|
HttpTypes.AdminDraftOrderListResponse, |
|
39
|
QueryKey |
|
40
|
>, |
|
41
|
"queryFn" | "queryKey" |
|
42
|
> |
|
43
|
) => { |
|
44
|
const { data, ...rest } = useQuery({ |
|
45
|
queryFn: async () => { |
|
46
|
return await sdk.admin.draftOrder.list(query) |
|
47
|
}, |
|
48
|
queryKey: draftOrdersQueryKeys.list(query), |
|
49
|
...options, |
|
50
|
}) |
|
51
|
|
|
52
|
return { ...data, ...rest } |
|
53
|
} |
|
54
|
|
|
55
|
export const useDraftOrder = ( |
|
56
|
id: string, |
|
57
|
query?: HttpTypes.AdminDraftOrderParams, |
|
58
|
options?: Omit< |
|
59
|
UseQueryOptions< |
|
60
|
HttpTypes.AdminDraftOrderResponse, |
|
61
|
FetchError, |
|
62
|
HttpTypes.AdminDraftOrderResponse, |
|
63
|
QueryKey |
|
64
|
>, |
|
65
|
"queryFn" | "queryKey" |
|
66
|
> |
|
67
|
) => { |
|
68
|
const { data, ...rest } = useQuery({ |
|
69
|
queryFn: async () => { |
|
70
|
return await sdk.admin.draftOrder.retrieve(id, query) |
|
71
|
}, |
|
72
|
queryKey: draftOrdersQueryKeys.detail(id, query), |
|
73
|
...options, |
|
74
|
}) |
|
75
|
|
|
76
|
return { ...data, ...rest } |
|
77
|
} |
|
78
|
|
|
79
|
export const useCreateDraftOrder = ( |
|
80
|
options?: Omit< |
|
81
|
UseMutationOptions< |
|
82
|
HttpTypes.AdminDraftOrderResponse, |
|
83
|
FetchError, |
|
84
|
HttpTypes.AdminCreateDraftOrder |
|
85
|
>, |
|
86
|
"mutationFn" | "mutationKey" |
|
87
|
> |
|
88
|
) => { |
|
89
|
const queryClient = useQueryClient() |
|
90
|
|
|
91
|
return useMutation({ |
|
92
|
mutationFn: async (payload) => { |
|
93
|
return await sdk.admin.draftOrder.create(payload) |
|
94
|
}, |
|
95
|
onSuccess: (data, variables, context) => { |
|
96
|
queryClient.invalidateQueries({ |
|
97
|
queryKey: draftOrdersQueryKeys.lists(), |
|
98
|
}) |
|
99
|
|
|
100
|
// NOTE: Invalidate shipping options since we have a lot of places where we enable SO fetching |
|
101
|
// depending on a condition but RQ will return stale data from cache which will render wrong UI. |
|
102
|
queryClient.invalidateQueries({ |
|
103
|
queryKey: shippingOptionsQueryKeys.list(), |
|
104
|
}) |
|
105
|
|
|
106
|
options?.onSuccess?.(data, variables, context) |
|
107
|
}, |
|
108
|
...options, |
|
109
|
}) |
|
110
|
} |
|
111
|
|
|
112
|
export const useDeleteDraftOrder = ( |
|
113
|
options?: Omit< |
|
114
|
UseMutationOptions< |
|
115
|
HttpTypes.DeleteResponse<"draft-order">, |
|
116
|
FetchError, |
|
117
|
string |
|
118
|
>, |
|
119
|
"mutationFn" | "mutationKey" |
|
120
|
> |
|
121
|
) => { |
|
122
|
const queryClient = useQueryClient() |
|
123
|
|
|
124
|
return useMutation({ |
|
125
|
mutationFn: async (id: string) => { |
|
126
|
return await sdk.admin.draftOrder.delete(id) |
|
127
|
}, |
|
128
|
onSuccess: (data, undefined, context) => { |
|
129
|
queryClient.invalidateQueries({ |
|
130
|
queryKey: draftOrdersQueryKeys.lists(), |
|
131
|
}) |
|
132
|
|
|
133
|
options?.onSuccess?.(data, undefined, context) |
|
134
|
}, |
|
135
|
...options, |
|
136
|
}) |
|
137
|
} |
|
138
|
|
|
139
|
export const useUpdateDraftOrder = ( |
|
140
|
id: string, |
|
141
|
options?: Omit< |
|
142
|
UseMutationOptions< |
|
143
|
HttpTypes.AdminDraftOrderResponse, |
|
144
|
FetchError, |
|
145
|
HttpTypes.AdminUpdateDraftOrder |
|
146
|
>, |
|
147
|
"mutationFn" | "mutationKey" |
|
148
|
> |
|
149
|
) => { |
|
150
|
const queryClient = useQueryClient() |
|
151
|
|
|
152
|
return useMutation({ |
|
153
|
mutationFn: async (payload) => { |
|
154
|
return await sdk.admin.draftOrder.update(id, payload) |
|
155
|
}, |
|
156
|
onSuccess: (data, variables, context) => { |
|
157
|
queryClient.invalidateQueries({ |
|
158
|
queryKey: draftOrdersQueryKeys.details(), |
|
159
|
}) |
|
160
|
queryClient.invalidateQueries({ |
|
161
|
queryKey: ordersQueryKeys.details(), |
|
162
|
}) |
|
163
|
queryClient.invalidateQueries({ |
|
164
|
queryKey: draftOrdersQueryKeys.lists(), |
|
165
|
}) |
|
166
|
|
|
167
|
options?.onSuccess?.(data, variables, context) |
|
168
|
}, |
|
169
|
...options, |
|
170
|
}) |
|
171
|
} |
|
172
|
|
|
173
|
export const useConvertDraftOrder = ( |
|
174
|
id: string, |
|
175
|
options?: UseMutationOptions<HttpTypes.AdminOrderResponse, FetchError, void> |
|
176
|
) => { |
|
177
|
const queryClient = useQueryClient() |
|
178
|
|
|
179
|
return useMutation({ |
|
180
|
mutationFn: () => sdk.admin.draftOrder.convertToOrder(id), |
|
181
|
onSuccess: (data, variables, context) => { |
|
182
|
queryClient.invalidateQueries({ |
|
183
|
queryKey: draftOrdersQueryKeys.detail(id), |
|
184
|
}) |
|
185
|
queryClient.invalidateQueries({ |
|
186
|
queryKey: draftOrdersQueryKeys.lists(), |
|
187
|
}) |
|
188
|
|
|
189
|
queryClient.invalidateQueries({ |
|
190
|
queryKey: ordersQueryKeys.detail(id), |
|
191
|
}) |
|
192
|
queryClient.invalidateQueries({ |
|
193
|
queryKey: ordersQueryKeys.lists(), |
|
194
|
}) |
|
195
|
|
|
196
|
options?.onSuccess?.(data, variables, context) |
|
197
|
}, |
|
198
|
...options, |
|
199
|
}) |
|
200
|
} |
|
201
|
|
|
202
|
export const useDraftOrderAddItems = ( |
|
203
|
id: string, |
|
204
|
options?: UseMutationOptions< |
|
205
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
206
|
FetchError, |
|
207
|
HttpTypes.AdminAddDraftOrderItems |
|
208
|
> |
|
209
|
) => { |
|
210
|
const queryClient = useQueryClient() |
|
211
|
|
|
212
|
return useMutation({ |
|
213
|
mutationFn: (payload) => sdk.admin.draftOrder.addItems(id, payload), |
|
214
|
onSuccess: (data, variables, context) => { |
|
215
|
queryClient.invalidateQueries({ |
|
216
|
queryKey: ordersQueryKeys.preview(id), |
|
217
|
}) |
|
218
|
|
|
219
|
options?.onSuccess?.(data, variables, context) |
|
220
|
}, |
|
221
|
...options, |
|
222
|
}) |
|
223
|
} |
|
224
|
|
|
225
|
export const useDraftOrderUpdateItem = ( |
|
226
|
id: string, |
|
227
|
options?: UseMutationOptions< |
|
228
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
229
|
FetchError, |
|
230
|
HttpTypes.AdminUpdateDraftOrderItem & { item_id: string } |
|
231
|
> |
|
232
|
) => { |
|
233
|
const queryClient = useQueryClient() |
|
234
|
|
|
235
|
return useMutation({ |
|
236
|
mutationFn: ({ item_id, ...payload }) => |
|
237
|
sdk.admin.draftOrder.updateItem(id, item_id, payload), |
|
238
|
onSuccess: (data, variables, context) => { |
|
239
|
queryClient.invalidateQueries({ |
|
240
|
queryKey: ordersQueryKeys.preview(id), |
|
241
|
}) |
|
242
|
|
|
243
|
options?.onSuccess?.(data, variables, context) |
|
244
|
}, |
|
245
|
...options, |
|
246
|
}) |
|
247
|
} |
|
248
|
|
|
249
|
export const useDraftOrderRemoveActionItem = ( |
|
250
|
id: string, |
|
251
|
options?: UseMutationOptions< |
|
252
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
253
|
FetchError, |
|
254
|
string |
|
255
|
> |
|
256
|
) => { |
|
257
|
const queryClient = useQueryClient() |
|
258
|
|
|
259
|
return useMutation({ |
|
260
|
mutationFn: (action_id: string) => |
|
261
|
sdk.admin.draftOrder.removeActionItem(id, action_id), |
|
262
|
onSuccess: (data, variables, context) => { |
|
263
|
queryClient.invalidateQueries({ |
|
264
|
queryKey: ordersQueryKeys.preview(id), |
|
265
|
}) |
|
266
|
|
|
267
|
options?.onSuccess?.(data, variables, context) |
|
268
|
}, |
|
269
|
...options, |
|
270
|
}) |
|
271
|
} |
|
272
|
|
|
273
|
export const useDraftOrderUpdateActionItem = ( |
|
274
|
id: string, |
|
275
|
options?: UseMutationOptions< |
|
276
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
277
|
FetchError, |
|
278
|
HttpTypes.AdminUpdateDraftOrderItem & { action_id: string } |
|
279
|
> |
|
280
|
) => { |
|
281
|
const queryClient = useQueryClient() |
|
282
|
|
|
283
|
return useMutation({ |
|
284
|
mutationFn: ({ action_id, ...payload }) => |
|
285
|
sdk.admin.draftOrder.updateActionItem(id, action_id, payload), |
|
286
|
onSuccess: (data, variables, context) => { |
|
287
|
queryClient.invalidateQueries({ |
|
288
|
queryKey: ordersQueryKeys.preview(id), |
|
289
|
}) |
|
290
|
|
|
291
|
options?.onSuccess?.(data, variables, context) |
|
292
|
}, |
|
293
|
...options, |
|
294
|
}) |
|
295
|
} |
|
296
|
|
|
297
|
export const useDraftOrderAddPromotions = ( |
|
298
|
id: string, |
|
299
|
options?: UseMutationOptions< |
|
300
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
301
|
FetchError, |
|
302
|
HttpTypes.AdminAddDraftOrderPromotions |
|
303
|
> |
|
304
|
) => { |
|
305
|
const queryClient = useQueryClient() |
|
306
|
|
|
307
|
return useMutation({ |
|
308
|
mutationFn: (payload) => sdk.admin.draftOrder.addPromotions(id, payload), |
|
309
|
onSuccess: (data, variables, context) => { |
|
310
|
queryClient.invalidateQueries({ |
|
311
|
queryKey: ordersQueryKeys.preview(id), |
|
312
|
}) |
|
313
|
|
|
314
|
options?.onSuccess?.(data, variables, context) |
|
315
|
}, |
|
316
|
...options, |
|
317
|
}) |
|
318
|
} |
|
319
|
|
|
320
|
export const useDraftOrderRemovePromotions = ( |
|
321
|
id: string, |
|
322
|
options?: UseMutationOptions< |
|
323
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
324
|
FetchError, |
|
325
|
HttpTypes.AdminRemoveDraftOrderPromotions |
|
326
|
> |
|
327
|
) => { |
|
328
|
const queryClient = useQueryClient() |
|
329
|
|
|
330
|
return useMutation({ |
|
331
|
mutationFn: (payload) => sdk.admin.draftOrder.removePromotions(id, payload), |
|
332
|
onSuccess: (data, variables, context) => { |
|
333
|
queryClient.invalidateQueries({ |
|
334
|
queryKey: ordersQueryKeys.preview(id), |
|
335
|
}) |
|
336
|
|
|
337
|
options?.onSuccess?.(data, variables, context) |
|
338
|
}, |
|
339
|
...options, |
|
340
|
}) |
|
341
|
} |
|
342
|
|
|
343
|
export const useDraftOrderAddShippingMethod = ( |
|
344
|
id: string, |
|
345
|
options?: UseMutationOptions< |
|
346
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
347
|
FetchError, |
|
348
|
HttpTypes.AdminAddDraftOrderShippingMethod |
|
349
|
> |
|
350
|
) => { |
|
351
|
const queryClient = useQueryClient() |
|
352
|
|
|
353
|
return useMutation({ |
|
354
|
mutationFn: (payload) => |
|
355
|
sdk.admin.draftOrder.addShippingMethod(id, payload), |
|
356
|
onSuccess: (data, variables, context) => { |
|
357
|
queryClient.invalidateQueries({ |
|
358
|
queryKey: ordersQueryKeys.preview(id), |
|
359
|
}) |
|
360
|
|
|
361
|
options?.onSuccess?.(data, variables, context) |
|
362
|
}, |
|
363
|
...options, |
|
364
|
}) |
|
365
|
} |
|
366
|
|
|
367
|
export const useDraftOrderUpdateActionShippingMethod = ( |
|
368
|
id: string, |
|
369
|
options?: UseMutationOptions< |
|
370
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
371
|
FetchError, |
|
372
|
HttpTypes.AdminUpdateDraftOrderActionShippingMethod & { action_id: string } |
|
373
|
> |
|
374
|
) => { |
|
375
|
const queryClient = useQueryClient() |
|
376
|
|
|
377
|
return useMutation({ |
|
378
|
mutationFn: ({ action_id, ...payload }) => |
|
379
|
sdk.admin.draftOrder.updateActionShippingMethod(id, action_id, payload), |
|
380
|
onSuccess: (data, variables, context) => { |
|
381
|
queryClient.invalidateQueries({ |
|
382
|
queryKey: ordersQueryKeys.preview(id), |
|
383
|
}) |
|
384
|
|
|
385
|
options?.onSuccess?.(data, variables, context) |
|
386
|
}, |
|
387
|
...options, |
|
388
|
}) |
|
389
|
} |
|
390
|
|
|
391
|
export const useDraftOrderRemoveActionShippingMethod = ( |
|
392
|
id: string, |
|
393
|
options?: UseMutationOptions< |
|
394
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
395
|
FetchError, |
|
396
|
string |
|
397
|
> |
|
398
|
) => { |
|
399
|
const queryClient = useQueryClient() |
|
400
|
|
|
401
|
return useMutation({ |
|
402
|
mutationFn: (action_id: string) => |
|
403
|
sdk.admin.draftOrder.removeActionShippingMethod(id, action_id), |
|
404
|
onSuccess: (data, variables, context) => { |
|
405
|
queryClient.invalidateQueries({ |
|
406
|
queryKey: ordersQueryKeys.preview(id), |
|
407
|
}) |
|
408
|
|
|
409
|
options?.onSuccess?.(data, variables, context) |
|
410
|
}, |
|
411
|
...options, |
|
412
|
}) |
|
413
|
} |
|
414
|
|
|
415
|
export const useDraftOrderRemoveShippingMethod = ( |
|
416
|
id: string, |
|
417
|
options?: UseMutationOptions< |
|
418
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
419
|
FetchError, |
|
420
|
string |
|
421
|
> |
|
422
|
) => { |
|
423
|
const queryClient = useQueryClient() |
|
424
|
|
|
425
|
return useMutation({ |
|
426
|
mutationFn: (shipping_method_id: string) => |
|
427
|
sdk.admin.draftOrder.removeShippingMethod(id, shipping_method_id), |
|
428
|
onSuccess: (data, variables, context) => { |
|
429
|
queryClient.invalidateQueries({ |
|
430
|
queryKey: ordersQueryKeys.preview(id), |
|
431
|
}) |
|
432
|
|
|
433
|
options?.onSuccess?.(data, variables, context) |
|
434
|
}, |
|
435
|
...options, |
|
436
|
}) |
|
437
|
} |
|
438
|
|
|
439
|
export const useDraftOrderUpdateShippingMethod = ( |
|
440
|
id: string, |
|
441
|
options?: UseMutationOptions< |
|
442
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
443
|
FetchError, |
|
444
|
HttpTypes.AdminUpdateDraftOrderShippingMethod & { method_id: string } |
|
445
|
> |
|
446
|
) => { |
|
447
|
const queryClient = useQueryClient() |
|
448
|
|
|
449
|
return useMutation({ |
|
450
|
mutationFn: ({ method_id, ...payload }) => |
|
451
|
sdk.admin.draftOrder.updateShippingMethod(id, method_id, payload), |
|
452
|
onSuccess: (data, variables, context) => { |
|
453
|
queryClient.invalidateQueries({ |
|
454
|
queryKey: ordersQueryKeys.preview(id), |
|
455
|
}) |
|
456
|
|
|
457
|
options?.onSuccess?.(data, variables, context) |
|
458
|
}, |
|
459
|
...options, |
|
460
|
}) |
|
461
|
} |
|
462
|
|
|
463
|
export const useDraftOrderBeginEdit = ( |
|
464
|
id: string, |
|
465
|
options?: UseMutationOptions< |
|
466
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
467
|
FetchError, |
|
468
|
void |
|
469
|
> |
|
470
|
) => { |
|
471
|
const queryClient = useQueryClient() |
|
472
|
|
|
473
|
return useMutation({ |
|
474
|
mutationFn: () => sdk.admin.draftOrder.beginEdit(id), |
|
475
|
onSuccess: (data, variables, context) => { |
|
476
|
queryClient.invalidateQueries({ |
|
477
|
queryKey: ordersQueryKeys.preview(id), |
|
478
|
}) |
|
479
|
|
|
480
|
options?.onSuccess?.(data, variables, context) |
|
481
|
}, |
|
482
|
...options, |
|
483
|
}) |
|
484
|
} |
|
485
|
|
|
486
|
export const useDraftOrderCancelEdit = ( |
|
487
|
id: string, |
|
488
|
options?: UseMutationOptions< |
|
489
|
HttpTypes.DeleteResponse<"draft-order-edit">, |
|
490
|
FetchError, |
|
491
|
void |
|
492
|
> |
|
493
|
) => { |
|
494
|
const queryClient = useQueryClient() |
|
495
|
|
|
496
|
return useMutation({ |
|
497
|
mutationFn: () => sdk.admin.draftOrder.cancelEdit(id), |
|
498
|
onSuccess: (data, variables, context) => { |
|
499
|
queryClient.invalidateQueries({ |
|
500
|
queryKey: ordersQueryKeys.preview(id), |
|
501
|
}) |
|
502
|
|
|
503
|
queryClient.invalidateQueries({ |
|
504
|
queryKey: ordersQueryKeys.details(), |
|
505
|
}) |
|
506
|
|
|
507
|
queryClient.invalidateQueries({ |
|
508
|
queryKey: draftOrdersQueryKeys.details(), |
|
509
|
}) |
|
510
|
|
|
511
|
options?.onSuccess?.(data, variables, context) |
|
512
|
}, |
|
513
|
...options, |
|
514
|
}) |
|
515
|
} |
|
516
|
|
|
517
|
export const useDraftOrderRequestEdit = ( |
|
518
|
id: string, |
|
519
|
options?: UseMutationOptions< |
|
520
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
521
|
FetchError, |
|
522
|
void |
|
523
|
> |
|
524
|
) => { |
|
525
|
const queryClient = useQueryClient() |
|
526
|
|
|
527
|
return useMutation({ |
|
528
|
mutationFn: () => sdk.admin.draftOrder.requestEdit(id), |
|
529
|
onSuccess: (data, variables, context) => { |
|
530
|
queryClient.invalidateQueries({ |
|
531
|
queryKey: ordersQueryKeys.preview(id), |
|
532
|
}) |
|
533
|
|
|
534
|
options?.onSuccess?.(data, variables, context) |
|
535
|
}, |
|
536
|
...options, |
|
537
|
}) |
|
538
|
} |
|
539
|
|
|
540
|
export const useDraftOrderConfirmEdit = ( |
|
541
|
id: string, |
|
542
|
options?: UseMutationOptions< |
|
543
|
HttpTypes.AdminDraftOrderPreviewResponse, |
|
544
|
FetchError, |
|
545
|
void |
|
546
|
> |
|
547
|
) => { |
|
548
|
const queryClient = useQueryClient() |
|
549
|
|
|
550
|
return useMutation({ |
|
551
|
mutationFn: () => sdk.admin.draftOrder.confirmEdit(id), |
|
552
|
onSuccess: (data, variables, context) => { |
|
553
|
queryClient.invalidateQueries({ |
|
554
|
queryKey: ordersQueryKeys.preview(id), |
|
555
|
}) |
|
556
|
|
|
557
|
queryClient.invalidateQueries({ |
|
558
|
queryKey: ordersQueryKeys.changes(id), |
|
559
|
}) |
|
560
|
|
|
561
|
queryClient.invalidateQueries({ |
|
562
|
queryKey: ordersQueryKeys.details(), |
|
563
|
}) |
|
564
|
|
|
565
|
queryClient.invalidateQueries({ |
|
566
|
queryKey: draftOrdersQueryKeys.detail(id), |
|
567
|
}) |
|
568
|
|
|
569
|
queryClient.invalidateQueries({ |
|
570
|
queryKey: draftOrdersQueryKeys.details(), |
|
571
|
}) |
|
572
|
|
|
573
|
queryClient.invalidateQueries({ |
|
574
|
queryKey: draftOrdersQueryKeys.lists(), |
|
575
|
}) |
|
576
|
|
|
577
|
options?.onSuccess?.(data, variables, context) |
|
578
|
}, |
|
579
|
...options, |
|
580
|
}) |
|
581
|
} |