Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

feat(admin): add compound components for category list and detail pages

vholik0d3251e4422f
diff --git a/packages/admin/src/pages/categories/category-detail/category-detail.tsx b/packages/admin/src/pages/categories/category-detail/category-detail.tsx
index 75f7e84ea..ecb1554a2 100644
--- a/packages/admin/src/pages/categories/category-detail/category-detail.tsx
+++ b/packages/admin/src/pages/categories/category-detail/category-detail.tsx
@@ -1,27 +1,28 @@
-import { useLoaderData, useParams } from "react-router-dom";
-import { useProductCategory } from "../../../hooks/api/categories";
-import { CategoryGeneralSection } from "./components/category-general-section";
-import { CategoryOrganizeSection } from "./components/category-organize-section";
-import { CategoryProductSection } from "./components/category-product-section";
-import { categoryLoader } from "./loader";
+import { Children, ReactNode } from "react"
+import { useLoaderData, useParams } from "react-router-dom"
-import { TwoColumnPageSkeleton } from "../../../components/common/skeleton";
-import { TwoColumnPage } from "../../../components/layout/pages";
+import { TwoColumnPageSkeleton } from "../../../components/common/skeleton"
+import { TwoColumnPage } from "../../../components/layout/pages"
+import { useProductCategory } from "../../../hooks/api/categories"
+import { CategoryGeneralSection } from "./components/category-general-section"
+import { CategoryOrganizeSection } from "./components/category-organize-section"
+import { CategoryProductSection } from "./components/category-product-section"
+import { categoryLoader } from "./loader"
-export const CategoryDetail = () => {
- const { id } = useParams();
+const Root = ({ children }: { children?: ReactNode }) => {
+ const { id } = useParams()
const initialData = useLoaderData() as Awaited<
ReturnType<typeof categoryLoader>
- >;
+ >
const { product_category, isLoading, isError, error } = useProductCategory(
id!,
undefined,
{
initialData,
- },
- );
+ }
+ )
if (isLoading || !product_category) {
return (
@@ -31,22 +32,36 @@ export const CategoryDetail = () => {
showJSON
showMetadata
/>
- );
+ )
}
if (isError) {
- throw error;
+ throw error
}
return (
- <TwoColumnPage showJSON showMetadata data={product_category}>
- <TwoColumnPage.Main>
- <CategoryGeneralSection category={product_category} />
- <CategoryProductSection category={product_category} />
- </TwoColumnPage.Main>
- <TwoColumnPage.Sidebar>
- <CategoryOrganizeSection category={product_category} />
- </TwoColumnPage.Sidebar>
- </TwoColumnPage>
- );
-};
+ <>
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <TwoColumnPage showJSON showMetadata data={product_category}>
+ <TwoColumnPage.Main>
+ <CategoryGeneralSection category={product_category} />
+ <CategoryProductSection category={product_category} />
+ </TwoColumnPage.Main>
+ <TwoColumnPage.Sidebar>
+ <CategoryOrganizeSection category={product_category} />
+ </TwoColumnPage.Sidebar>
+ </TwoColumnPage>
+ )}
+ </>
+ )
+}
+
+export const CategoryDetail = Object.assign(Root, {
+ Main: TwoColumnPage.Main,
+ Sidebar: TwoColumnPage.Sidebar,
+ MainGeneralSection: CategoryGeneralSection,
+ MainProductSection: CategoryProductSection,
+ SidebarOrganizeSection: CategoryOrganizeSection,
+})
diff --git a/packages/admin/src/pages/categories/category-list/category-list.tsx b/packages/admin/src/pages/categories/category-list/category-list.tsx
index 145eef625..be8c95f8b 100644
--- a/packages/admin/src/pages/categories/category-list/category-list.tsx
+++ b/packages/admin/src/pages/categories/category-list/category-list.tsx
@@ -1,12 +1,26 @@
+import { Children, ReactNode } from "react"
+
import { SingleColumnPage } from "../../../components/layout/pages"
-import { CategoryListTable } from "./components/category-list-table"
+import {
+ CategoryListTable,
+ CategoryListDataTable,
+ CategoryListHeader,
+ CategoryListActions,
+ CategoryListTitle,
+} from "./components/category-list-table"
-export const CategoryList = () => {
+const Root = ({ children }: { children?: ReactNode }) => {
return (
- <SingleColumnPage
- hasOutlet
- >
- <CategoryListTable />
+ <SingleColumnPage hasOutlet>
+ {Children.count(children) > 0 ? children : <CategoryListTable />}
</SingleColumnPage>
)
}
+
+export const CategoryList = Object.assign(Root, {
+ Table: CategoryListTable,
+ Header: CategoryListHeader,
+ HeaderTitle: CategoryListTitle,
+ HeaderActions: CategoryListActions,
+ DataTable: CategoryListDataTable,
+})
diff --git a/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-data-table.tsx b/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-data-table.tsx
new file mode 100644
index 000000000..ca8a38a4b
--- /dev/null
+++ b/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-data-table.tsx
@@ -0,0 +1,127 @@
+import { PencilSquare, Trash } from "@medusajs/icons"
+import { AdminProductCategoryResponse } from "@medusajs/types"
+import { keepPreviousData } from "@tanstack/react-query"
+import { createColumnHelper } from "@tanstack/react-table"
+import { useMemo } from "react"
+import { useTranslation } from "react-i18next"
+
+import { ActionMenu } from "../../../../../components/common/action-menu"
+import { _DataTable } from "../../../../../components/table/data-table"
+import { useProductCategories } from "../../../../../hooks/api/categories"
+import { useDataTable } from "../../../../../hooks/use-data-table"
+import { useDeleteProductCategoryAction } from "../../../common/hooks/use-delete-product-category-action"
+import { useCategoryTableColumns } from "./use-category-table-columns"
+import { useCategoryTableQuery } from "./use-category-table-query"
+
+const PAGE_SIZE = 20
+
+export const CategoryListDataTable = () => {
+ const { raw, searchParams } = useCategoryTableQuery({ pageSize: PAGE_SIZE })
+
+ const query = raw.q
+ ? {
+ include_ancestors_tree: true,
+ fields: "id,name,handle,is_active,is_internal,parent_category",
+ ...searchParams,
+ }
+ : {
+ include_descendants_tree: true,
+ parent_category_id: "null",
+ fields: "id,name,category_children,handle,is_internal,is_active",
+ ...searchParams,
+ }
+
+ const { product_categories, count, isLoading, isError, error } =
+ useProductCategories(
+ {
+ ...query,
+ },
+ {
+ placeholderData: keepPreviousData,
+ }
+ )
+
+ const columns = useColumns()
+
+ const { table } = useDataTable({
+ data: product_categories || [],
+ columns,
+ count,
+ getRowId: (original) => original.id,
+ getSubRows: (original) => original.category_children,
+ enableExpandableRows: true,
+ pageSize: PAGE_SIZE,
+ })
+
+ if (isError) {
+ throw error
+ }
+
+ return (
+ <_DataTable
+ table={table}
+ columns={columns}
+ count={count}
+ pageSize={PAGE_SIZE}
+ isLoading={isLoading}
+ navigateTo={(row) => row.id}
+ queryObject={raw}
+ search
+ pagination
+ />
+ )
+}
+
+const CategoryRowActions = ({
+ category,
+}: {
+ category: AdminProductCategoryResponse["product_category"]
+}) => {
+ const { t } = useTranslation()
+ const handleDelete = useDeleteProductCategoryAction(category)
+
+ return (
+ <ActionMenu
+ groups={[
+ {
+ actions: [
+ {
+ label: t("actions.edit"),
+ icon: <PencilSquare />,
+ to: `${category.id}/edit`,
+ },
+ ],
+ },
+ {
+ actions: [
+ {
+ label: t("actions.delete"),
+ icon: <Trash />,
+ onClick: handleDelete,
+ },
+ ],
+ },
+ ]}
+ />
+ )
+}
+
+const columnHelper =
+ createColumnHelper<AdminProductCategoryResponse["product_category"]>()
+
+const useColumns = () => {
+ const base = useCategoryTableColumns()
+
+ return useMemo(
+ () => [
+ ...base,
+ columnHelper.display({
+ id: "actions",
+ cell: ({ row }) => {
+ return <CategoryRowActions category={row.original} />
+ },
+ }),
+ ],
+ [base]
+ )
+}
diff --git a/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-header.tsx b/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-header.tsx
new file mode 100644
index 000000000..df828a65d
--- /dev/null
+++ b/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-header.tsx
@@ -0,0 +1,59 @@
+import { Children, ReactNode } from "react"
+import { Button, Heading, Text } from "@medusajs/ui"
+import { useTranslation } from "react-i18next"
+import { Link } from "react-router-dom"
+
+export const CategoryListTitle = () => {
+ const { t } = useTranslation()
+ return (
+ <div>
+ <Heading>{t("categories.domain")}</Heading>
+ <Text className="text-ui-fg-subtle" size="small">
+ {t("categories.subtitle")}
+ </Text>
+ </div>
+ )
+}
+
+export const CategoryListActions = ({
+ children,
+}: {
+ children?: ReactNode
+}) => {
+ const { t } = useTranslation()
+ return (
+ <div className="flex items-center gap-x-2">
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <Button size="small" variant="secondary" asChild>
+ <Link to="organize">{t("categories.organize.action")}</Link>
+ </Button>
+ <Button size="small" variant="secondary" asChild>
+ <Link to="create">{t("actions.create")}</Link>
+ </Button>
+ </>
+ )}
+ </div>
+ )
+}
+
+export const CategoryListHeader = ({
+ children,
+}: {
+ children?: ReactNode
+}) => {
+ return (
+ <div className="flex items-center justify-between px-6 py-4">
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <CategoryListTitle />
+ <CategoryListActions />
+ </>
+ )}
+ </div>
+ )
+}
diff --git a/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-table.tsx b/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-table.tsx
index df9643d23..8f63e9bce 100644
--- a/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-table.tsx
+++ b/packages/admin/src/pages/categories/category-list/components/category-list-table/category-list-table.tsx
@@ -1,154 +1,27 @@
-import { PencilSquare, Trash } from "@medusajs/icons"
-import { AdminProductCategoryResponse } from "@medusajs/types"
-import { Button, Container, Heading, Text } from "@medusajs/ui"
-import { keepPreviousData } from "@tanstack/react-query"
-import { createColumnHelper } from "@tanstack/react-table"
-import { useMemo } from "react"
-import { useTranslation } from "react-i18next"
+import { Children, ReactNode } from "react"
+import { Container } from "@medusajs/ui"
-import { Link } from "react-router-dom"
-import { ActionMenu } from "../../../../../components/common/action-menu"
-import { _DataTable } from "../../../../../components/table/data-table"
-import { useProductCategories } from "../../../../../hooks/api/categories"
-import { useDataTable } from "../../../../../hooks/use-data-table"
-import { useDeleteProductCategoryAction } from "../../../common/hooks/use-delete-product-category-action"
-import { useCategoryTableColumns } from "./use-category-table-columns"
-import { useCategoryTableQuery } from "./use-category-table-query"
+import { CategoryListHeader } from "./category-list-header"
+import { CategoryListDataTable } from "./category-list-data-table"
-const PAGE_SIZE = 20
-
-export const CategoryListTable = () => {
- const { t } = useTranslation()
-
- const { raw, searchParams } = useCategoryTableQuery({ pageSize: PAGE_SIZE })
-
- const query = raw.q
- ? {
- include_ancestors_tree: true,
- fields: "id,name,handle,is_active,is_internal,parent_category",
- ...searchParams,
- }
- : {
- include_descendants_tree: true,
- parent_category_id: "null",
- fields: "id,name,category_children,handle,is_internal,is_active",
- ...searchParams,
- }
-
- const { product_categories, count, isLoading, isError, error } =
- useProductCategories(
- {
- ...query,
- },
- {
- placeholderData: keepPreviousData,
- }
- )
-
- const columns = useColumns()
-
- const { table } = useDataTable({
- data: product_categories || [],
- columns,
- count,
- getRowId: (original) => original.id,
- getSubRows: (original) => original.category_children,
- enableExpandableRows: true,
- pageSize: PAGE_SIZE,
- })
-
- const showRankingAction =
- !!product_categories && product_categories.length > 0
-
- if (isError) {
- throw error
- }
+export { CategoryListDataTable } from "./category-list-data-table"
+export {
+ CategoryListHeader,
+ CategoryListTitle,
+ CategoryListActions,
+} from "./category-list-header"
+export const CategoryListTable = ({ children }: { children?: ReactNode }) => {
return (
<Container className="divide-y p-0">
- <div className="flex items-center justify-between px-6 py-4">
- <div>
- <Heading>{t("categories.domain")}</Heading>
- <Text className="text-ui-fg-subtle" size="small">
- {t("categories.subtitle")}
- </Text>
- </div>
- <div className="flex items-center gap-x-2">
- {showRankingAction && (
- <Button size="small" variant="secondary" asChild>
- <Link to="organize">{t("categories.organize.action")}</Link>
- </Button>
- )}
- <Button size="small" variant="secondary" asChild>
- <Link to="create">{t("actions.create")}</Link>
- </Button>
- </div>
- </div>
- <_DataTable
- table={table}
- columns={columns}
- count={count}
- pageSize={PAGE_SIZE}
- isLoading={isLoading}
- navigateTo={(row) => row.id}
- queryObject={raw}
- search
- pagination
- />
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <CategoryListHeader />
+ <CategoryListDataTable />
+ </>
+ )}
</Container>
)
}
-
-const CategoryRowActions = ({
- category,
-}: {
- category: AdminProductCategoryResponse["product_category"]
-}) => {
- const { t } = useTranslation()
- const handleDelete = useDeleteProductCategoryAction(category)
-
- return (
- <ActionMenu
- groups={[
- {
- actions: [
- {
- label: t("actions.edit"),
- icon: <PencilSquare />,
- to: `${category.id}/edit`,
- },
- ],
- },
- {
- actions: [
- {
- label: t("actions.delete"),
- icon: <Trash />,
- onClick: handleDelete,
- },
- ],
- },
- ]}
- />
- )
-}
-
-const columnHelper =
- createColumnHelper<AdminProductCategoryResponse["product_category"]>()
-
-const useColumns = () => {
- const base = useCategoryTableColumns()
-
- return useMemo(
- () => [
- ...base,
- columnHelper.display({
- id: "actions",
- cell: ({ row }) => {
- return <CategoryRowActions category={row.original} />
- },
- }),
- ],
- [base]
- )
-}