|
1
|
--- |
|
2
|
sidebar_label: "Promotions" |
|
3
|
--- |
|
4
|
|
|
5
|
import { TypeList } from "docs-ui" |
|
6
|
|
|
7
|
export const metadata = { |
|
8
|
title: `Promotion 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 promotion hooks fetch promotions from the Medusa backend. Use them to show promotion data in your admin customizations. |
|
20
|
|
|
21
|
```tsx |
|
22
|
import { |
|
23
|
promotionsQueryKeys, |
|
24
|
usePromotions, |
|
25
|
} from "@medusajs/dashboard/hooks" |
|
26
|
``` |
|
27
|
|
|
28
|
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). |
|
29
|
|
|
30
|
--- |
|
31
|
|
|
32
|
## usePromotions |
|
33
|
|
|
34
|
Fetches a paginated list of promotions. |
|
35
|
|
|
36
|
```tsx |
|
37
|
const { promotions, count, isPending } = usePromotions({ |
|
38
|
limit: 20, |
|
39
|
}) |
|
40
|
``` |
|
41
|
|
|
42
|
### Parameters |
|
43
|
|
|
44
|
<TypeList |
|
45
|
types={[ |
|
46
|
{ |
|
47
|
name: "query", |
|
48
|
type: "`AdminGetPromotionsParams`", |
|
49
|
optional: true, |
|
50
|
description: "Optional query parameters, such as `limit`, `offset`, `q`, or `fields`.", |
|
51
|
}, |
|
52
|
{ |
|
53
|
name: "options", |
|
54
|
type: "`UseQueryOptions`", |
|
55
|
optional: true, |
|
56
|
description: "Optional TanStack Query options, excluding `queryKey` and `queryFn`.", |
|
57
|
}, |
|
58
|
]} |
|
59
|
/> |
|
60
|
|
|
61
|
### Returns |
|
62
|
|
|
63
|
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. |
|
64
|
|
|
65
|
<TypeList |
|
66
|
types={[ |
|
67
|
{ |
|
68
|
name: "promotions", |
|
69
|
type: "`AdminPromotion[]`", |
|
70
|
description: "The list of promotions. It's `undefined` while the request is loading.", |
|
71
|
}, |
|
72
|
{ |
|
73
|
name: "count", |
|
74
|
type: "`number`", |
|
75
|
description: "The total number of promotions matching the query.", |
|
76
|
}, |
|
77
|
{ |
|
78
|
name: "limit", |
|
79
|
type: "`number`", |
|
80
|
description: "The maximum number of promotions returned in the request.", |
|
81
|
}, |
|
82
|
{ |
|
83
|
name: "offset", |
|
84
|
type: "`number`", |
|
85
|
description: "The number of promotions skipped before the returned ones.", |
|
86
|
}, |
|
87
|
]} |
|
88
|
/> |
|
89
|
|
|
90
|
--- |
|
91
|
|
|
92
|
## promotionsQueryKeys |
|
93
|
|
|
94
|
A TanStack Query key factory for promotion queries. Besides `all`, `lists()`, `list(query)`, `details()`, and `detail(id, query)`, it adds the promotion-specific keys `listRules(id, ruleType, query)`, `listRuleAttributes(ruleType, promotionType, applicationMethodTargetType)`, and `listRuleValues(ruleType, ruleValue, query)`. |
|
95
|
|
|
96
|
```tsx |
|
97
|
import { useQueryClient } from "@tanstack/react-query" |
|
98
|
import { |
|
99
|
promotionsQueryKeys, |
|
100
|
} from "@medusajs/dashboard/hooks" |
|
101
|
|
|
102
|
const Widget = () => { |
|
103
|
const queryClient = useQueryClient() |
|
104
|
|
|
105
|
const refreshPromotions = () => { |
|
106
|
queryClient.invalidateQueries({ |
|
107
|
queryKey: promotionsQueryKeys.lists(), |
|
108
|
}) |
|
109
|
} |
|
110
|
|
|
111
|
// ... |
|
112
|
} |
|
113
|
``` |