|
1
|
--- |
|
2
|
sidebar_label: "Orders" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Order Hooks - Admin Components`, |
|
9
|
} |
|
10
|
|
|
11
|
# {metadata.title} |
|
12
|
|
|
13
|
<Note> |
|
14
|
|
|
15
|
These hooks are available since Medusa [v2.21.0](https://github.com/medusajs/medusa/releases/tag/v2.21.0). |
|
16
|
|
|
17
|
</Note> |
|
18
|
|
|
19
|
The order hooks fetch an order, its changes, and its preview from the Medusa backend. They also update an order or request an order edit. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { |
|
23
|
ordersQueryKeys, |
|
24
|
useOrder, |
|
25
|
useOrderChanges, |
|
26
|
useOrderPreview, |
|
27
|
useRequestOrderEdit, |
|
28
|
useUpdateOrder, |
|
29
|
} from "@medusajs/dashboard/hooks" |
|
30
|
``` |
|
31
|
|
|
32
|
These hooks are built on [TanStack Query](https://tanstack.com/query/latest). Use them in your [widgets](!docs!/learn/fundamentals/admin/widgets) and [UI routes](!docs!/learn/fundamentals/admin/ui-routes). |
|
33
|
|
|
34
|
--- |
|
35
|
|
|
36
|
## useOrder |
|
37
|
|
|
38
|
Fetches a single order by ID. |
|
39
|
|
|
40
|
```tsx |
|
41
|
const { order, isPending } = useOrder(orderId, { |
|
42
|
fields: "+items.*", |
|
43
|
}) |
|
44
|
``` |
|
45
|
|
|
46
|
### Parameters |
|
47
|
|
|
48
|
<TypeList |
|
49
|
types={[ |
|
50
|
{ |
|
51
|
name: "id", |
|
52
|
type: "`string`", |
|
53
|
optional: false, |
|
54
|
description: "The order's ID.", |
|
55
|
}, |
|
56
|
{ |
|
57
|
name: "query", |
|
58
|
type: "`Record<string, any>`", |
|
59
|
optional: true, |
|
60
|
description: "Optional query parameters, such as `fields`.", |
|
61
|
}, |
|
62
|
{ |
|
63
|
name: "options", |
|
64
|
type: "`UseQueryOptions`", |
|
65
|
optional: true, |
|
66
|
description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.", |
|
67
|
}, |
|
68
|
]} |
|
69
|
/> |
|
70
|
|
|
71
|
### Returns |
|
72
|
|
|
73
|
Along with the following, the hook returns the rest of TanStack Query's [`useQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) result. |
|
74
|
|
|
75
|
<TypeList |
|
76
|
types={[ |
|
77
|
{ |
|
78
|
name: "order", |
|
79
|
type: "`AdminOrder`", |
|
80
|
description: "The order's details. It's `undefined` while the request is loading.", |
|
81
|
}, |
|
82
|
]} |
|
83
|
/> |
|
84
|
|
|
85
|
--- |
|
86
|
|
|
87
|
## useOrderChanges |
|
88
|
|
|
89
|
Fetches the list of changes applied to an order, such as edits, returns, or exchanges. |
|
90
|
|
|
91
|
```tsx |
|
92
|
const { order_changes } = useOrderChanges(orderId) |
|
93
|
``` |
|
94
|
|
|
95
|
### Parameters |
|
96
|
|
|
97
|
<TypeList |
|
98
|
types={[ |
|
99
|
{ |
|
100
|
name: "id", |
|
101
|
type: "`string`", |
|
102
|
optional: false, |
|
103
|
description: "The order's ID.", |
|
104
|
}, |
|
105
|
{ |
|
106
|
name: "query", |
|
107
|
type: "`AdminOrderChangesFilters`", |
|
108
|
optional: true, |
|
109
|
description: "Optional filters, such as `change_type` or `status`. The query key doesn't include these filters, so pass a custom `queryKey` in `options` if you fetch different filters for the same order.", |
|
110
|
}, |
|
111
|
{ |
|
112
|
name: "options", |
|
113
|
type: "`UseQueryOptions`", |
|
114
|
optional: true, |
|
115
|
description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.", |
|
116
|
}, |
|
117
|
]} |
|
118
|
/> |
|
119
|
|
|
120
|
### Returns |
|
121
|
|
|
122
|
Along with the following, the hook returns the rest of TanStack Query's [`useQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) result. |
|
123
|
|
|
124
|
<TypeList |
|
125
|
types={[ |
|
126
|
{ |
|
127
|
name: "order_changes", |
|
128
|
type: "`AdminOrderChange[]`", |
|
129
|
description: "The order's changes. It's `undefined` while the request is loading.", |
|
130
|
}, |
|
131
|
]} |
|
132
|
/> |
|
133
|
|
|
134
|
--- |
|
135
|
|
|
136
|
## useOrderPreview |
|
137
|
|
|
138
|
Fetches a preview of the order with the latest pending change applied, such as an edit, return, exchange, or claim. |
|
139
|
|
|
140
|
```tsx |
|
141
|
const { order: preview } = useOrderPreview(orderId) |
|
142
|
``` |
|
143
|
|
|
144
|
### Parameters |
|
145
|
|
|
146
|
<TypeList |
|
147
|
types={[ |
|
148
|
{ |
|
149
|
name: "id", |
|
150
|
type: "`string`", |
|
151
|
optional: false, |
|
152
|
description: "The order's ID.", |
|
153
|
}, |
|
154
|
{ |
|
155
|
name: "query", |
|
156
|
type: "`AdminOrderFilters`", |
|
157
|
optional: true, |
|
158
|
description: "Optional query parameters, such as `fields`.", |
|
159
|
}, |
|
160
|
{ |
|
161
|
name: "options", |
|
162
|
type: "`UseQueryOptions`", |
|
163
|
optional: true, |
|
164
|
description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.", |
|
165
|
}, |
|
166
|
]} |
|
167
|
/> |
|
168
|
|
|
169
|
### Returns |
|
170
|
|
|
171
|
Along with the following, the hook returns the rest of TanStack Query's [`useQuery`](https://tanstack.com/query/latest/docs/framework/react/reference/useQuery) result. |
|
172
|
|
|
173
|
<TypeList |
|
174
|
types={[ |
|
175
|
{ |
|
176
|
name: "order", |
|
177
|
type: "`AdminOrderPreview`", |
|
178
|
description: "The order's preview, including its pending changes. It's `undefined` while the request is loading.", |
|
179
|
}, |
|
180
|
]} |
|
181
|
/> |
|
182
|
|
|
183
|
--- |
|
184
|
|
|
185
|
## useUpdateOrder |
|
186
|
|
|
187
|
Updates an order. On success, the hook invalidates the order's detail and changes queries. |
|
188
|
|
|
189
|
```tsx |
|
190
|
const { mutateAsync, isPending } = useUpdateOrder(orderId) |
|
191
|
|
|
192
|
const handleUpdate = async () => { |
|
193
|
await mutateAsync({ |
|
194
|
email: "customer@example.com", |
|
195
|
}) |
|
196
|
} |
|
197
|
``` |
|
198
|
|
|
199
|
### Parameters |
|
200
|
|
|
201
|
<TypeList |
|
202
|
types={[ |
|
203
|
{ |
|
204
|
name: "id", |
|
205
|
type: "`string`", |
|
206
|
optional: false, |
|
207
|
description: "The order's ID.", |
|
208
|
}, |
|
209
|
{ |
|
210
|
name: "options", |
|
211
|
type: "`UseMutationOptions`", |
|
212
|
optional: true, |
|
213
|
description: "Optional TanStack mutation options, such as `onSuccess` or `onError`.", |
|
214
|
}, |
|
215
|
]} |
|
216
|
/> |
|
217
|
|
|
218
|
### Returns |
|
219
|
|
|
220
|
Along with the following, the hook returns the rest of TanStack Query's [`useMutation`](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation) result. |
|
221
|
|
|
222
|
<TypeList |
|
223
|
types={[ |
|
224
|
{ |
|
225
|
name: "mutate", |
|
226
|
type: "`(payload: AdminUpdateOrder) => void`", |
|
227
|
description: "Sends the update request with the order's new data.", |
|
228
|
}, |
|
229
|
{ |
|
230
|
name: "mutateAsync", |
|
231
|
type: "`(payload: AdminUpdateOrder) => Promise`", |
|
232
|
description: "The promise-based version of `mutate`. It resolves to an object with an `order` property.", |
|
233
|
}, |
|
234
|
]} |
|
235
|
/> |
|
236
|
|
|
237
|
--- |
|
238
|
|
|
239
|
## useRequestOrderEdit |
|
240
|
|
|
241
|
Requests an order edit, which moves the order edit into a requested state for confirmation. On success, the hook invalidates the order's details, preview, changes, and line items queries. |
|
242
|
|
|
243
|
```tsx |
|
244
|
const { mutateAsync } = useRequestOrderEdit(orderId) |
|
245
|
|
|
246
|
const handleRequest = async () => { |
|
247
|
const { order_preview } = await mutateAsync({}) |
|
248
|
} |
|
249
|
``` |
|
250
|
|
|
251
|
### Parameters |
|
252
|
|
|
253
|
<TypeList |
|
254
|
types={[ |
|
255
|
{ |
|
256
|
name: "id", |
|
257
|
type: "`string`", |
|
258
|
optional: false, |
|
259
|
description: "The order's ID.", |
|
260
|
}, |
|
261
|
{ |
|
262
|
name: "options", |
|
263
|
type: "`UseMutationOptions`", |
|
264
|
optional: true, |
|
265
|
description: "Optional TanStack mutation options, such as `onSuccess` or `onError`.", |
|
266
|
}, |
|
267
|
]} |
|
268
|
/> |
|
269
|
|
|
270
|
### Returns |
|
271
|
|
|
272
|
Along with the following, the hook returns the rest of TanStack Query's [`useMutation`](https://tanstack.com/query/latest/docs/framework/react/reference/useMutation) result. |
|
273
|
|
|
274
|
<TypeList |
|
275
|
types={[ |
|
276
|
{ |
|
277
|
name: "mutate", |
|
278
|
type: "`(payload: AdminRequestOrderEdit) => void`", |
|
279
|
description: "Sends the request order edit request.", |
|
280
|
}, |
|
281
|
{ |
|
282
|
name: "mutateAsync", |
|
283
|
type: "`(payload: AdminRequestOrderEdit) => Promise`", |
|
284
|
description: "The promise-based version of `mutate`. It resolves to an object with an `order_preview` property.", |
|
285
|
}, |
|
286
|
]} |
|
287
|
/> |
|
288
|
|
|
289
|
--- |
|
290
|
|
|
291
|
## ordersQueryKeys |
|
292
|
|
|
293
|
A TanStack Query key factory for order queries. Besides `all`, `lists()`, `list(query)`, `details()`, and `detail(id, query)`, it adds the order-specific keys `preview(id)`, `changes(id)`, `lineItems(id, query)`, and `shippingOptions(id)`. |
|
294
|
|
|
295
|
```tsx |
|
296
|
import { useQueryClient } from "@tanstack/react-query" |
|
297
|
import { ordersQueryKeys } from "@medusajs/dashboard/hooks" |
|
298
|
|
|
299
|
const Widget = () => { |
|
300
|
const queryClient = useQueryClient() |
|
301
|
|
|
302
|
const refreshPreview = (id: string) => { |
|
303
|
queryClient.invalidateQueries({ |
|
304
|
queryKey: ordersQueryKeys.preview(id), |
|
305
|
}) |
|
306
|
} |
|
307
|
|
|
308
|
// ... |
|
309
|
} |
|
310
|
``` |