|
1
|
--- |
|
2
|
products: |
|
3
|
- product |
|
4
|
--- |
|
5
|
|
|
6
|
import { Prerequisites } from "docs-ui" |
|
7
|
|
|
8
|
export const metadata = { |
|
9
|
title: `${pageNumber} Guide: Add Product's Brand Widget in Admin`, |
|
10
|
} |
|
11
|
|
|
12
|
# {metadata.title} |
|
13
|
|
|
14
|
In this chapter, you'll customize the product details page of the Medusa Admin dashboard to show the product's [brand](../../custom-features/module/page.mdx). You'll create a widget that is injected into a pre-defined zone in the page, and in the widget you'll retrieve the product's brand from the server and display it. |
|
15
|
|
|
16
|
<Prerequisites |
|
17
|
items={[ |
|
18
|
{ |
|
19
|
text: "Brands linked to products", |
|
20
|
link: "/learn/customization/extend-features/define-link" |
|
21
|
} |
|
22
|
]} |
|
23
|
/> |
|
24
|
|
|
25
|
## 1. Initialize JS SDK |
|
26
|
|
|
27
|
In your custom widget, you'll retrieve the product's brand by sending a request to the Medusa server. Medusa has a [JS SDK](!resources!/js-sdk) that simplifies sending requests to the server's API routes. |
|
28
|
|
|
29
|
So, you'll start by configuring the JS SDK. Create the file `src/admin/lib/sdk.ts` with the following content: |
|
30
|
|
|
31
|
 |
|
32
|
|
|
33
|
```ts title="src/admin/lib/sdk.ts" |
|
34
|
import Medusa from "@medusajs/js-sdk" |
|
35
|
|
|
36
|
export const sdk = new Medusa({ |
|
37
|
baseUrl: import.meta.env.VITE_BACKEND_URL || "/", |
|
38
|
debug: import.meta.env.DEV, |
|
39
|
auth: { |
|
40
|
type: "session", |
|
41
|
}, |
|
42
|
}) |
|
43
|
``` |
|
44
|
|
|
45
|
You initialize the SDK passing it the following options: |
|
46
|
|
|
47
|
- `baseUrl`: The URL to the Medusa server. |
|
48
|
- `debug`: Whether to enable logging debug messages. This should only be enabled in development. |
|
49
|
- `auth.type`: The authentication method used in the client application, which is `session` in the Medusa Admin dashboard. |
|
50
|
|
|
51
|
Notice that you use `import.meta.env` to access environment variables in your customizations because the Medusa Admin is built on top of Vite. Learn more in the [Admin Environment Variables](../../../fundamentals/admin/environment-variables/page.mdx) chapter. |
|
52
|
|
|
53
|
You can now use the SDK to send requests to the Medusa server. |
|
54
|
|
|
55
|
<Note> |
|
56
|
|
|
57
|
Refer to the [JS SDK Reference](!resources!/js-sdk) to learn more about the it, its options, and how to use it to send requests to Medusa's API routes. |
|
58
|
|
|
59
|
</Note> |
|
60
|
|
|
61
|
--- |
|
62
|
|
|
63
|
## 2. Add Widget to Product Details Page |
|
64
|
|
|
65
|
You'll now add a widget to the product-details page. A [widget](../../../fundamentals/admin/widgets/page.mdx) is a React component that's injected into pre-defined zones in the Medusa Admin dashboard. It's created in a `.tsx` file under the `src/admin/widgets` directory. |
|
66
|
|
|
67
|
To create a widget that shows a product's brand in its details page, create the file `src/admin/widgets/product-brand.tsx` with the following content: |
|
68
|
|
|
69
|
 |
|
70
|
|
|
71
|
export const highlights = [ |
|
72
|
["14", "ProductBrandWidget", "Widget to inject into the page"], |
|
73
|
["15", "data", "Receive the product's details as a prop"], |
|
74
|
["17", "useQuery", "Use Tanstack Query to send the request to the server with the JS SDK."], |
|
75
|
["18", "sdk", "Send the request to retrieve the product with the JS SDK."], |
|
76
|
["19", "fields", "Specify the product's brand to be retrieved."], |
|
77
|
["23", "brandName", "Get brand name from the query request."], |
|
78
|
["53", "defineWidgetConfig", "Export the widget's configurations"], |
|
79
|
["54", "zone", "Show the widget in the product details page."] |
|
80
|
] |
|
81
|
|
|
82
|
```tsx title="src/admin/widgets/product-brand.tsx" highlights={highlights} |
|
83
|
import { defineWidgetConfig } from "@medusajs/admin-sdk" |
|
84
|
import { DetailWidgetProps, AdminProduct } from "@medusajs/framework/types" |
|
85
|
import { clx, Container, Heading, Text } from "@medusajs/ui" |
|
86
|
import { useQuery } from "@tanstack/react-query" |
|
87
|
import { sdk } from "../lib/sdk" |
|
88
|
|
|
89
|
type AdminProductBrand = AdminProduct & { |
|
90
|
brand?: { |
|
91
|
id: string |
|
92
|
name: string |
|
93
|
} |
|
94
|
} |
|
95
|
|
|
96
|
const ProductBrandWidget = ({ |
|
97
|
data: product, |
|
98
|
}: DetailWidgetProps<AdminProduct>) => { |
|
99
|
const { data: queryResult } = useQuery({ |
|
100
|
queryFn: () => sdk.admin.product.retrieve(product.id, { |
|
101
|
fields: "+brand.*", |
|
102
|
}), |
|
103
|
queryKey: [["product", product.id]], |
|
104
|
}) |
|
105
|
const brandName = (queryResult?.product as AdminProductBrand)?.brand?.name |
|
106
|
|
|
107
|
return ( |
|
108
|
<Container className="divide-y p-0"> |
|
109
|
<div className="flex items-center justify-between px-6 py-4"> |
|
110
|
<div> |
|
111
|
<Heading level="h2">Brand</Heading> |
|
112
|
</div> |
|
113
|
</div> |
|
114
|
<div |
|
115
|
className={clx( |
|
116
|
`text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4` |
|
117
|
)} |
|
118
|
> |
|
119
|
<Text size="small" weight="plus" leading="compact"> |
|
120
|
Name |
|
121
|
</Text> |
|
122
|
|
|
123
|
<Text |
|
124
|
size="small" |
|
125
|
leading="compact" |
|
126
|
className="whitespace-pre-line text-pretty" |
|
127
|
> |
|
128
|
{brandName || "-"} |
|
129
|
</Text> |
|
130
|
</div> |
|
131
|
</Container> |
|
132
|
) |
|
133
|
} |
|
134
|
|
|
135
|
export const config = defineWidgetConfig({ |
|
136
|
zone: "product.details", |
|
137
|
}) |
|
138
|
|
|
139
|
export default ProductBrandWidget |
|
140
|
``` |
|
141
|
|
|
142
|
A widget's file must export: |
|
143
|
|
|
144
|
- A React component to be rendered in the specified injection zone. The component must be the file's default export. |
|
145
|
- A configuration object created with `defineWidgetConfig` from the Admin Extension SDK. The function receives an object as a parameter that has a `zone` property, whose value is the zone to inject the widget to. |
|
146
|
|
|
147
|
Since the widget is injected into the product details page, the widget receives the product's details as a parameter. |
|
148
|
|
|
149
|
In the widget, you use [Tanstack (React) Query](https://tanstack.com/query/latest) to query the Medusa server. Tanstack Query provides features like asynchronous state management and optimized caching. In the `queryFn` function that executes the query, you use the JS SDK to send a request to the [Get Product API Route](!api!/admin/products/get-a-product), passing `+brand.*` in the `fields` query parameter to retrieve the product's brand. |
|
150
|
|
|
151
|
<Note type="warning"> |
|
152
|
|
|
153
|
Do not install Tanstack Query as that will cause unexpected errors in your development. If you prefer installing it for better auto-completion in your code editor, make sure to install `v5.64.2` as a development dependency. |
|
154
|
|
|
155
|
</Note> |
|
156
|
|
|
157
|
You then render a section that shows the brand's name. In admin customizations, use components from the [Medusa UI package](!ui!) to maintain a consistent user interface and design in the dashboard. |
|
158
|
|
|
159
|
--- |
|
160
|
|
|
161
|
## Test it Out |
|
162
|
|
|
163
|
To test out your widget, start the Medusa application: |
|
164
|
|
|
165
|
```bash npm2yarn |
|
166
|
npm run dev |
|
167
|
``` |
|
168
|
|
|
169
|
Then, open the admin dashboard at `http://localhost:9000/app`. After you log in, open the page of a product that has a brand. You'll see a new section at the top showing the brand's name. |
|
170
|
|
|
171
|
 |
|
172
|
|
|
173
|
--- |
|
174
|
|
|
175
|
## Admin Components Guides |
|
176
|
|
|
177
|
When building your widget, you may need more complicated components. For example, you may add a form to the above widget to set the product's brand. |
|
178
|
|
|
179
|
The [Admin Components guides](!resources!/admin-components) show you how to build and use common components in the Medusa Admin, such as forms, tables, JSON data viewer, and more. The components in the guides also follow the Medusa Admin's design convention. |
|
180
|
|
|
181
|
--- |
|
182
|
|
|
183
|
## Next Chapter: Add UI Route for Brands |
|
184
|
|
|
185
|
In the next chapter, you'll add a UI route that displays the list of brands in your application and allows admin users. |