|
1
|
import { useMutation, UseMutationOptions } from "@tanstack/react-query" |
|
2
|
|
|
3
|
import { HttpTypes } from "@medusajs/types" |
|
4
|
|
|
5
|
import { sdk } from "../../lib/client" |
|
6
|
import { queryClient } from "../../lib/query-client" |
|
7
|
import { ordersQueryKeys } from "./orders" |
|
8
|
import { FetchError } from "@medusajs/js-sdk" |
|
9
|
import { reservationItemsQueryKeys } from "./reservations" |
|
10
|
import { inventoryItemsQueryKeys } from "./inventory.tsx" |
|
11
|
|
|
12
|
export const useCreateOrderEdit = ( |
|
13
|
orderId: string, |
|
14
|
options?: UseMutationOptions< |
|
15
|
HttpTypes.AdminOrderEditResponse, |
|
16
|
FetchError, |
|
17
|
HttpTypes.AdminInitiateOrderEditRequest |
|
18
|
> |
|
19
|
) => { |
|
20
|
return useMutation({ |
|
21
|
mutationFn: (payload: HttpTypes.AdminInitiateOrderEditRequest) => |
|
22
|
sdk.admin.orderEdit.initiateRequest(payload), |
|
23
|
onSuccess: (data: any, variables: any, context: any) => { |
|
24
|
queryClient.invalidateQueries({ |
|
25
|
queryKey: ordersQueryKeys.details(), |
|
26
|
}) |
|
27
|
|
|
28
|
queryClient.invalidateQueries({ |
|
29
|
queryKey: ordersQueryKeys.preview(orderId), |
|
30
|
}) |
|
31
|
options?.onSuccess?.(data, variables, context) |
|
32
|
}, |
|
33
|
...options, |
|
34
|
}) |
|
35
|
} |
|
36
|
|
|
37
|
export const useRequestOrderEdit = ( |
|
38
|
id: string, |
|
39
|
options?: UseMutationOptions< |
|
40
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
41
|
FetchError, |
|
42
|
HttpTypes.AdminRequestOrderEdit |
|
43
|
> |
|
44
|
) => { |
|
45
|
return useMutation({ |
|
46
|
mutationFn: (payload: HttpTypes.AdminRequestOrderEdit) => |
|
47
|
sdk.admin.orderEdit.request(id, payload), |
|
48
|
onSuccess: (data: any, variables: any, context: any) => { |
|
49
|
queryClient.invalidateQueries({ |
|
50
|
queryKey: ordersQueryKeys.details(), |
|
51
|
}) |
|
52
|
|
|
53
|
queryClient.invalidateQueries({ |
|
54
|
queryKey: ordersQueryKeys.preview(id), |
|
55
|
}) |
|
56
|
|
|
57
|
queryClient.invalidateQueries({ |
|
58
|
queryKey: ordersQueryKeys.changes(id), |
|
59
|
}) |
|
60
|
|
|
61
|
queryClient.invalidateQueries({ |
|
62
|
queryKey: ordersQueryKeys.lineItems(id), |
|
63
|
}) |
|
64
|
options?.onSuccess?.(data, variables, context) |
|
65
|
}, |
|
66
|
...options, |
|
67
|
}) |
|
68
|
} |
|
69
|
|
|
70
|
export const useConfirmOrderEdit = ( |
|
71
|
id: string, |
|
72
|
options?: UseMutationOptions< |
|
73
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
74
|
FetchError, |
|
75
|
void |
|
76
|
> |
|
77
|
) => { |
|
78
|
return useMutation({ |
|
79
|
mutationFn: () => sdk.admin.orderEdit.confirm(id), |
|
80
|
onSuccess: (data: any, variables: any, context: any) => { |
|
81
|
queryClient.invalidateQueries({ |
|
82
|
queryKey: ordersQueryKeys.details(), |
|
83
|
}) |
|
84
|
|
|
85
|
queryClient.invalidateQueries({ |
|
86
|
queryKey: ordersQueryKeys.preview(id), |
|
87
|
}) |
|
88
|
|
|
89
|
queryClient.invalidateQueries({ |
|
90
|
queryKey: ordersQueryKeys.changes(id), |
|
91
|
}) |
|
92
|
|
|
93
|
queryClient.invalidateQueries({ |
|
94
|
queryKey: ordersQueryKeys.lineItems(id), |
|
95
|
}) |
|
96
|
|
|
97
|
queryClient.invalidateQueries({ |
|
98
|
queryKey: reservationItemsQueryKeys.lists(), |
|
99
|
}) |
|
100
|
|
|
101
|
queryClient.invalidateQueries({ |
|
102
|
queryKey: inventoryItemsQueryKeys.lists(), |
|
103
|
}) |
|
104
|
|
|
105
|
queryClient.invalidateQueries({ |
|
106
|
queryKey: inventoryItemsQueryKeys.details(), |
|
107
|
}) |
|
108
|
|
|
109
|
options?.onSuccess?.(data, variables, context) |
|
110
|
}, |
|
111
|
...options, |
|
112
|
}) |
|
113
|
} |
|
114
|
|
|
115
|
export const useCancelOrderEdit = ( |
|
116
|
orderId: string, |
|
117
|
options?: UseMutationOptions<any, FetchError, void> |
|
118
|
) => { |
|
119
|
return useMutation({ |
|
120
|
mutationFn: () => sdk.admin.orderEdit.cancelRequest(orderId), |
|
121
|
onSuccess: (data: any, variables: any, context: any) => { |
|
122
|
queryClient.invalidateQueries({ |
|
123
|
queryKey: ordersQueryKeys.details(), |
|
124
|
}) |
|
125
|
|
|
126
|
queryClient.invalidateQueries({ |
|
127
|
queryKey: ordersQueryKeys.preview(orderId), |
|
128
|
}) |
|
129
|
|
|
130
|
queryClient.invalidateQueries({ |
|
131
|
queryKey: ordersQueryKeys.changes(orderId), |
|
132
|
}) |
|
133
|
|
|
134
|
queryClient.invalidateQueries({ |
|
135
|
queryKey: ordersQueryKeys.lineItems(orderId), |
|
136
|
}) |
|
137
|
options?.onSuccess?.(data, variables, context) |
|
138
|
}, |
|
139
|
...options, |
|
140
|
}) |
|
141
|
} |
|
142
|
|
|
143
|
export const useAddOrderEditItems = ( |
|
144
|
id: string, |
|
145
|
options?: UseMutationOptions< |
|
146
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
147
|
FetchError, |
|
148
|
HttpTypes.AdminAddOrderEditItems |
|
149
|
> |
|
150
|
) => { |
|
151
|
return useMutation({ |
|
152
|
mutationFn: (payload: HttpTypes.AdminAddOrderEditItems) => |
|
153
|
sdk.admin.orderEdit.addItems(id, payload), |
|
154
|
onSuccess: (data: any, variables: any, context: any) => { |
|
155
|
queryClient.invalidateQueries({ |
|
156
|
queryKey: ordersQueryKeys.preview(id), |
|
157
|
}) |
|
158
|
options?.onSuccess?.(data, variables, context) |
|
159
|
}, |
|
160
|
...options, |
|
161
|
}) |
|
162
|
} |
|
163
|
|
|
164
|
/** |
|
165
|
* Update (quantity) of an item that was originally on the order. |
|
166
|
*/ |
|
167
|
export const useUpdateOrderEditOriginalItem = ( |
|
168
|
id: string, |
|
169
|
options?: UseMutationOptions< |
|
170
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
171
|
FetchError, |
|
172
|
HttpTypes.AdminUpdateOrderEditItem & { itemId: string } |
|
173
|
> |
|
174
|
) => { |
|
175
|
return useMutation({ |
|
176
|
mutationFn: ({ |
|
177
|
itemId, |
|
178
|
...payload |
|
179
|
}: HttpTypes.AdminUpdateOrderEditItem & { itemId: string }) => { |
|
180
|
return sdk.admin.orderEdit.updateOriginalItem(id, itemId, payload) |
|
181
|
}, |
|
182
|
onSuccess: (data: any, variables: any, context: any) => { |
|
183
|
queryClient.invalidateQueries({ |
|
184
|
queryKey: ordersQueryKeys.preview(id), |
|
185
|
}) |
|
186
|
options?.onSuccess?.(data, variables, context) |
|
187
|
}, |
|
188
|
...options, |
|
189
|
}) |
|
190
|
} |
|
191
|
|
|
192
|
/** |
|
193
|
* Update (quantity) of an item that was added to the order edit. |
|
194
|
*/ |
|
195
|
export const useUpdateOrderEditAddedItem = ( |
|
196
|
id: string, |
|
197
|
options?: UseMutationOptions< |
|
198
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
199
|
FetchError, |
|
200
|
HttpTypes.AdminUpdateOrderEditItem & { actionId: string } |
|
201
|
> |
|
202
|
) => { |
|
203
|
return useMutation({ |
|
204
|
mutationFn: ({ |
|
205
|
actionId, |
|
206
|
...payload |
|
207
|
}: HttpTypes.AdminUpdateOrderEditItem & { actionId: string }) => { |
|
208
|
return sdk.admin.orderEdit.updateAddedItem(id, actionId, payload) |
|
209
|
}, |
|
210
|
onSuccess: (data: any, variables: any, context: any) => { |
|
211
|
queryClient.invalidateQueries({ |
|
212
|
queryKey: ordersQueryKeys.preview(id), |
|
213
|
}) |
|
214
|
options?.onSuccess?.(data, variables, context) |
|
215
|
}, |
|
216
|
...options, |
|
217
|
}) |
|
218
|
} |
|
219
|
|
|
220
|
/** |
|
221
|
* Remove item that was added to the edit. |
|
222
|
* To remove an original item on the order, set quantity to 0. |
|
223
|
*/ |
|
224
|
export const useRemoveOrderEditItem = ( |
|
225
|
id: string, |
|
226
|
options?: UseMutationOptions< |
|
227
|
HttpTypes.AdminOrderEditPreviewResponse, |
|
228
|
FetchError, |
|
229
|
string |
|
230
|
> |
|
231
|
) => { |
|
232
|
return useMutation({ |
|
233
|
mutationFn: (actionId: string) => |
|
234
|
sdk.admin.orderEdit.removeAddedItem(id, actionId), |
|
235
|
onSuccess: (data: any, variables: any, context: any) => { |
|
236
|
queryClient.invalidateQueries({ |
|
237
|
queryKey: ordersQueryKeys.preview(id), |
|
238
|
}) |
|
239
|
options?.onSuccess?.(data, variables, context) |
|
240
|
}, |
|
241
|
...options, |
|
242
|
}) |
|
243
|
} |