* initialized next.js project * finished markdown sections * added operation schema component * change page metadata * eslint fixes * fixes related to deployment * added response schema * resolve max stack issue * support for different property types * added support for property types * added loading for components * added more loading * type fixes * added oneOf type * removed console * fix replace with push * refactored everything * use static content for description * fixes and improvements * added code examples section * fix path name * optimizations * fixed tag navigation * add support for admin and store references * general enhancements * optimizations and fixes * fixes and enhancements * added search bar * loading enhancements * added loading * added code blocks * added margin top * add empty response text * fixed oneOf parameters * added path and query parameters * general fixes * added base path env variable * small fix for arrays * enhancements * design enhancements * general enhancements * fix isRequired * added enum values * enhancements * general fixes * general fixes * changed oas generation script * additions to the introduction section * added copy button for code + other enhancements * fix response code block * fix metadata * formatted store introduction * move sidebar logic to Tags component * added test env variables * fix code block bug * added loading animation * added expand param + loading * enhance operation loading * made responsive + improvements * added loading provider * fixed loading * adjustments for small devices * added sidebar label for endpoints * added feedback component * fixed analytics * general fixes * listen to scroll for other headings * added sample env file * update api ref files + support new fields * fix for external docs link * added new sections * fix last item in sidebar not showing * move docs content to www/docs * change redirect url * revert change * resolve build errors * configure rewrites * changed to environment variable url * revert changing environment variable name * add environment variable for API path * fix links * fix tailwind settings * remove vercel file * reconfigured api route * move api page under api * fix page metadata * fix external link in navigation bar * update api spec * updated api specs * fixed google lint error * add max-height on request samples * add padding before loading * fix for one of name * fix undefined types * general fixes * remove response schema example * redesigned navigation bar * redesigned sidebar * fixed up paddings * added feedback component + report issue * fixed up typography, padding, and general styling * redesigned code blocks * optimization * added error timeout * fixes * added indexing with algolia + fixes * fix errors with algolia script * redesign operation sections * fix heading scroll * design fixes * fix padding * fix padding + scroll issues * fix scroll issues * improve scroll performance * fixes for safari * optimization and fixes * fixes to docs + details animation * padding fixes for code block * added tab animation * fixed incorrect link * added selection styling * fix lint errors * redesigned details component * added detailed feedback form * api reference fixes * fix tabs * upgrade + fixes * updated documentation links * optimizations to sidebar items * fix spacing in sidebar item * optimizations and fixes * fix endpoint path styling * remove margin * final fixes * change margin on small devices * generated OAS * fixes for mobile * added feedback modal * optimize dark mode button * fixed color mode useeffect * minimize dom size * use new style system * radius and spacing design system * design fixes * fix eslint errors * added meta files * change cron schedule * fix docusaurus configurations * added operating system to feedback data * change content directory name * fixes to contribution guidelines * revert renaming content * added api-reference to documentation workflow * fixes for search * added dark mode + fixes * oas fixes * handle bugs * added code examples for clients * changed tooltip text * change authentication to card * change page title based on selected section * redesigned mobile navbar * fix icon colors * fix key colors * fix medusa-js installation command * change external regex in algolia * change changeset * fix padding on mobile * fix hydration error * update depedencies
10 KiB
title, description, addHowToData, badge
| title | description | addHowToData | badge | ||||
|---|---|---|---|---|---|---|---|
| How to Create an Admin Setting Page | Learn how to create a new setting page in the admin dashboard. | true |
|
In this document, you’ll learn how to create a setting page in the admin.
Overview
The admin UI routes allow you to add new pages to the admin dashboard. However, they can’t be used to add a new tab under the Setting page.
To do that, you need to create an Admin setting page. The page will automatically be shown as a tab under the Setting page in the admin. The tab leads to the content of your custom page.
A setting page is essentially a React Component created under the src/admin/settings directory.
This guide explains how to create a new setting page in the admin dashboard with some examples.
Prerequisites
It’s assumed you already have a Medusa backend with the admin plugin installed before you move forward with this guide. If not, you can follow this documentation page to install a Medusa project.
Furthermore, custom Admin Setting Pages are currently available as a beta feature. So, you must install the beta version of the @medusajs/admin and @medusajs/medusa packages:
npm install @medusajs/admin@beta @medusajs/medusa@beta
(Optional) TypeScript Preparations
Since setting pages are React components, they should be written in .tsx or .jsx files. If you’re using Typescript, you need to make some adjustments to avoid Typescript errors in your Admin files.
This section provides recommended configurations to avoid any TypeScript errors.
:::note
These changes may already be available in your Medusa project. They're included here for reference purposes.
:::
First, update your tsconfig.json with the following configurations:
{
"compilerOptions": {
"target": "es2019",
"module": "commonjs",
"allowJs": true,
"checkJs": false,
"jsx": "react-jsx",
"declaration": true,
"outDir": "./dist",
"rootDir": "./src",
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"noEmit": false,
"strict": false,
"moduleResolution": "node",
"esModuleInterop": true,
"resolveJsonModule": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": ["src/"],
"exclude": [
"dist",
"build",
".cache",
"tests",
"**/*.spec.js",
"**/*.spec.ts",
"node_modules",
".eslintrc.js"
]
}
The important changes to note here are the inclusion of the field "jsx": "react-jsx" and the addition of "build" and “.cache” to exclude.
The addition of "jsx": "react-jsx" specified how should TypeScript transform JSX, and excluding build and .cache ensures that TypeScript ignores build and development files.
Next, create the file tsconfig.server.json with the following content:
{
"extends": "./tsconfig.json",
"compilerOptions": {
/* Emit a single file with source maps instead of having a separate file. */
"inlineSourceMap": true
},
"exclude": ["src/admin", "**/*.spec.js"]
}
This is the configuration that will be used to transpile your custom backend code, such as services or entities. The important part is that it excludes src/admin as that is where your Admin code will live.
Finally, create the file tsconfig.admin.json with the following content:
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "esnext"
},
"include": ["src/admin"],
"exclude": ["**/*.spec.js"]
}
This is the configuration that will be used when transpiling your admin code.
Create the Setting Page
In this section, you’ll learn the basics of creating an admin UI route.
Step 1: Create File
Custom admin setting pages are added under the src/admin/settings directory of your Medusa project. The path of the file depends on the path you want the setting page to be available under. It is based on Next.js 13’s App Router.
All setting page paths are prefixed with /a/settings.
For example, if you want the setting page to be available in the admin dashboard under the path /a/settings/custom you should create your setting page under the file path src/admin/settings/custom/page.tsx.
Step 2: Create React Component in File
For a setting page to be valid, it must default export a React component. There are no restrictions on the content of the React component. It must also export a configuration object that indicates how the tab is shown on the Setting page.
For example, you can create the file src/admin/settings/custom/page.tsx with the following content:
import type { SettingConfig } from "@medusajs/admin"
import { CustomIcon } from "../../icons/custom"
const CustomSettingPage = () => {
return (
<div>
<h1>Custom Setting Page</h1>
</div>
)
}
export const config: SettingConfig = {
card: {
label: "Custom",
description: "Manage your custom settings",
// optional
icon: CustomIcon,
},
}
export default CustomSettingPage
This creates a setting page that will be available at /a/settings/custom. The content of the page is defined in the exported React component.
The exported configuration also indicates the details of the tab that will be added to the Setting page. You must specify a label and description, and you can optionally specify an icon. The icon is a React component.
Step 3: Test it Out
To test your admin setting page, run the following command in the root directory of the Medusa backend project:
npx medusa develop
This will build your admin and opens a window in your default browser to localhost:7001. After you log in, go to localhost:7001/a/settings. You’ll find a new tab available under a new Extensions section.
If you click on the tab, a new page will open with the content as defined in your React component.
Setting Page Props
Every route receives props of the type RouteProps, which includes the notify prop. The notify prop is an object that includes the following attributes:
success: a function that can be used to show a success message.error: a function that can be used to show an error message.warn: a function that can be used to show a warning message.info: a function that can be used to show an info message.
For example:
import type { SettingConfig } from "@medusajs/admin"
import type { SettingProps } from "@medusajs/admin-ui"
const CustomSettingPage = ({
notify,
}: SettingProps) => {
const handleClick = () => {
notify.success("Success", "You clicked the button")
}
return (
<div>
<h1>Custom Setting Page</h1>
<button onClick={handleClick}>
Click Me
</button>
</div>
)
}
export const config: SettingConfig = {
card: {
label: "Custom",
description: "Manage your custom settings",
},
}
export default CustomSettingPage
Styling Setting Page
Admin setting pages support Tailwind CSS by default.
For example, to customize the style of your custom setting page:
import type { SettingConfig } from "@medusajs/admin"
const CustomSettingPage = () => {
return (
<div
className="bg-white p-8 border border-gray-200 rounded-lg"
>
<h1>Custom Setting Page</h1>
</div>
)
}
export const config: SettingConfig = {
card: {
label: "Custom",
description: "Manage your custom settings",
},
}
export default CustomSettingPage
Use Routing Functionalities
If you want to use routing functionalities such as linking to another page or navigating between pages, you can use react-router-dom's utility hooks and functions.
:::note
💡 react-router-dom is available as one of the @medusajs/admin dependencies. You can also install it within your project using the following command:
npm install react-router-dom
If you're installing it in a plugin with admin customizations, make sure to include it in peerDependencies.
:::
For example, to add a link to another page:
import type { SettingConfig } from "@medusajs/admin"
import { Link } from "react-router-dom"
const CustomSettingPage = () => {
return (
<div>
<h1>Custom Setting Page</h1>
<Link to={"/a/products"}>
View Products
</Link>
</div>
)
}
export const config: SettingConfig = {
card: {
label: "Custom",
description: "Manage your custom settings",
},
}
export default CustomSettingPage
View react-router-dom’s documentation for other available components and hooks.
Querying and Mutating Data
You might need to interact with the Medusa backend from your admin setting page. To do so, you can utilize the Medusa React package. It contains a collection of queries and mutations built on @tanstack/react-query that lets you interact with the Medusa backend.
:::note
Make sure to also install the Medusa React package first if you’re intending to use it, as explained in the Medusa React guide.
:::
For example, you can retrieve available products and display them in your route:
import type { SettingConfig } from "@medusajs/admin"
import { useAdminProducts } from "medusa-react"
const CustomSettingPage = () => {
const { products } = useAdminProducts()
return (
<div>
<h1>Custom Setting Page</h1>
<div className="bg-white">
{products?.map((product) => product.title)}
</div>
</div>
)
}
export const config: SettingConfig = {
card: {
label: "Custom",
description: "Manage your custom settings",
},
}
export default CustomSettingPage
Custom Endpoints
You can also use medusa-react to interact with custom endpoints using Custom Hooks utility functions.