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 collection list and detail pages

vholik09f327ac4438
diff --git a/packages/admin/src/pages/collections/collection-detail/collection-detail.tsx b/packages/admin/src/pages/collections/collection-detail/collection-detail.tsx
index 0141d5338..cc97d6b2b 100644
--- a/packages/admin/src/pages/collections/collection-detail/collection-detail.tsx
+++ b/packages/admin/src/pages/collections/collection-detail/collection-detail.tsx
@@ -1,3 +1,4 @@
+import { Children, ReactNode } from "react"
import { useLoaderData, useParams } from "react-router-dom"
import { SingleColumnPageSkeleton } from "../../../components/common/skeleton"
@@ -7,7 +8,7 @@ import { CollectionGeneralSection } from "./components/collection-general-sectio
import { CollectionProductSection } from "./components/collection-product-section"
import { collectionLoader } from "./loader"
-export const CollectionDetail = () => {
+const Root = ({ children }: { children?: ReactNode }) => {
const initialData = useLoaderData() as Awaited<
ReturnType<typeof collectionLoader>
>
@@ -26,13 +27,20 @@ export const CollectionDetail = () => {
}
return (
- <SingleColumnPage
- showJSON
- showMetadata
- data={collection}
- >
- <CollectionGeneralSection collection={collection} />
- <CollectionProductSection collection={collection} />
- </SingleColumnPage>
+ <>
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <SingleColumnPage showJSON showMetadata data={collection}>
+ <CollectionGeneralSection collection={collection} />
+ <CollectionProductSection collection={collection} />
+ </SingleColumnPage>
+ )}
+ </>
)
}
+
+export const CollectionDetail = Object.assign(Root, {
+ GeneralSection: CollectionGeneralSection,
+ ProductSection: CollectionProductSection,
+})
diff --git a/packages/admin/src/pages/collections/collection-list/collection-list.tsx b/packages/admin/src/pages/collections/collection-list/collection-list.tsx
index 6f04eb41c..2f6d19169 100644
--- a/packages/admin/src/pages/collections/collection-list/collection-list.tsx
+++ b/packages/admin/src/pages/collections/collection-list/collection-list.tsx
@@ -1,10 +1,26 @@
+import { Children, ReactNode } from "react"
+
import { SingleColumnPage } from "../../../components/layout/pages"
-import { CollectionListTable } from "./components/collection-list-table"
+import {
+ CollectionListTable,
+ CollectionListDataTable,
+ CollectionListHeader,
+ CollectionListActions,
+ CollectionListTitle,
+} from "./components/collection-list-table"
-export const CollectionList = () => {
+const Root = ({ children }: { children?: ReactNode }) => {
return (
<SingleColumnPage>
- <CollectionListTable />
+ {Children.count(children) > 0 ? children : <CollectionListTable />}
</SingleColumnPage>
)
}
+
+export const CollectionList = Object.assign(Root, {
+ Table: CollectionListTable,
+ Header: CollectionListHeader,
+ HeaderTitle: CollectionListTitle,
+ HeaderActions: CollectionListActions,
+ DataTable: CollectionListDataTable,
+})
diff --git a/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-data-table.tsx b/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-data-table.tsx
new file mode 100644
index 000000000..1c1b21f22
--- /dev/null
+++ b/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-data-table.tsx
@@ -0,0 +1,81 @@
+import { HttpTypes } 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 { _DataTable } from "../../../../../components/table/data-table"
+import { useCollections } from "../../../../../hooks/api/collections"
+import { useCollectionTableColumns } from "../../../../../hooks/table/columns/use-collection-table-columns"
+import { useCollectionTableFilters } from "../../../../../hooks/table/filters"
+import { useCollectionTableQuery } from "../../../../../hooks/table/query"
+import { useDataTable } from "../../../../../hooks/use-data-table"
+import { CollectionRowActions } from "./collection-row-actions"
+
+const PAGE_SIZE = 20
+
+export const CollectionListDataTable = () => {
+ const { t } = useTranslation()
+ const { searchParams, raw } = useCollectionTableQuery({ pageSize: PAGE_SIZE })
+ const { collections, count, isError, error, isLoading } = useCollections(
+ {
+ ...searchParams,
+ fields: "+products.id",
+ },
+ {
+ placeholderData: keepPreviousData,
+ }
+ )
+
+ const filters = useCollectionTableFilters()
+ const columns = useColumns()
+
+ const { table } = useDataTable({
+ data: collections ?? [],
+ columns,
+ count,
+ enablePagination: true,
+ getRowId: (row, index) => row.id ?? `${index}`,
+ pageSize: PAGE_SIZE,
+ })
+
+ if (isError) {
+ throw error
+ }
+
+ return (
+ <_DataTable
+ table={table}
+ columns={columns}
+ pageSize={PAGE_SIZE}
+ count={count}
+ filters={filters}
+ orderBy={[
+ { key: "title", label: t("fields.title") },
+ { key: "handle", label: t("fields.handle") },
+ { key: "created_at", label: t("fields.createdAt") },
+ { key: "updated_at", label: t("fields.updatedAt") },
+ ]}
+ search
+ navigateTo={(row) => `/collections/${row.original.id}`}
+ queryObject={raw}
+ isLoading={isLoading}
+ />
+ )
+}
+
+const columnHelper = createColumnHelper<HttpTypes.AdminCollection>()
+
+const useColumns = () => {
+ const base = useCollectionTableColumns()
+
+ return useMemo(
+ () => [
+ ...base,
+ columnHelper.display({
+ id: "actions",
+ cell: ({ row }) => <CollectionRowActions collection={row.original} />,
+ }),
+ ],
+ [base]
+ )
+}
diff --git a/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-header.tsx b/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-header.tsx
new file mode 100644
index 000000000..75914b92d
--- /dev/null
+++ b/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-header.tsx
@@ -0,0 +1,56 @@
+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 CollectionListTitle = () => {
+ const { t } = useTranslation()
+ return (
+ <div>
+ <Heading>{t("collections.domain")}</Heading>
+ <Text className="text-ui-fg-subtle" size="small">
+ {t("collections.subtitle")}
+ </Text>
+ </div>
+ )
+}
+
+export const CollectionListActions = ({
+ children,
+}: {
+ children?: ReactNode
+}) => {
+ const { t } = useTranslation()
+ return (
+ <div className="flex items-center justify-center gap-x-2">
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <Link to="/collections/create">
+ <Button size="small" variant="secondary">
+ {t("actions.create")}
+ </Button>
+ </Link>
+ )}
+ </div>
+ )
+}
+
+export const CollectionListHeader = ({
+ children,
+}: {
+ children?: ReactNode
+}) => {
+ return (
+ <div className="flex items-center justify-between px-6 py-4">
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <CollectionListTitle />
+ <CollectionListActions />
+ </>
+ )}
+ </div>
+ )
+}
diff --git a/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-table.tsx b/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-table.tsx
index 23f8a9279..d754b0739 100644
--- a/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-table.tsx
+++ b/packages/admin/src/pages/collections/collection-list/components/collection-list-table/collection-list-table.tsx
@@ -1,99 +1,27 @@
-import { Button, Container, Heading, Text } from "@medusajs/ui"
-import { useTranslation } from "react-i18next"
-import { Link } from "react-router-dom"
+import { Children, ReactNode } from "react"
+import { Container } from "@medusajs/ui"
-import { HttpTypes } from "@medusajs/types"
-import { keepPreviousData } from "@tanstack/react-query"
-import { createColumnHelper } from "@tanstack/react-table"
-import { useMemo } from "react"
-import { _DataTable } from "../../../../../components/table/data-table"
-import { useCollections } from "../../../../../hooks/api/collections"
-import { useCollectionTableColumns } from "../../../../../hooks/table/columns/use-collection-table-columns"
-import { useCollectionTableFilters } from "../../../../../hooks/table/filters"
-import { useCollectionTableQuery } from "../../../../../hooks/table/query"
-import { useDataTable } from "../../../../../hooks/use-data-table"
-import { CollectionRowActions } from "./collection-row-actions"
+import { CollectionListHeader } from "./collection-list-header"
+import { CollectionListDataTable } from "./collection-list-data-table"
-const PAGE_SIZE = 20
-
-export const CollectionListTable = () => {
- const { t } = useTranslation()
- const { searchParams, raw } = useCollectionTableQuery({ pageSize: PAGE_SIZE })
- const { collections, count, isError, error, isLoading } = useCollections(
- {
- ...searchParams,
- fields: "+products.id",
- },
- {
- placeholderData: keepPreviousData,
- }
- )
-
- const filters = useCollectionTableFilters()
- const columns = useColumns()
-
- const { table } = useDataTable({
- data: collections ?? [],
- columns,
- count,
- enablePagination: true,
- getRowId: (row, index) => row.id ?? `${index}`,
- pageSize: PAGE_SIZE,
- })
-
- if (isError) {
- throw error
- }
+export { CollectionListDataTable } from "./collection-list-data-table"
+export {
+ CollectionListHeader,
+ CollectionListTitle,
+ CollectionListActions,
+} from "./collection-list-header"
+export const CollectionListTable = ({ children }: { children?: ReactNode }) => {
return (
<Container className="divide-y p-0">
- <div className="flex items-center justify-between px-6 py-4">
- <div>
- <Heading>{t("collections.domain")}</Heading>
- <Text className="text-ui-fg-subtle" size="small">
- {t("collections.subtitle")}
- </Text>
- </div>
- <Link to="/collections/create">
- <Button size="small" variant="secondary">
- {t("actions.create")}
- </Button>
- </Link>
- </div>
- <_DataTable
- table={table}
- columns={columns}
- pageSize={PAGE_SIZE}
- count={count}
- filters={filters}
- orderBy={[
- { key: "title", label: t("fields.title") },
- { key: "handle", label: t("fields.handle") },
- { key: "created_at", label: t("fields.createdAt") },
- { key: "updated_at", label: t("fields.updatedAt") },
- ]}
- search
- navigateTo={(row) => `/collections/${row.original.id}`}
- queryObject={raw}
- isLoading={isLoading}
- />
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <CollectionListHeader />
+ <CollectionListDataTable />
+ </>
+ )}
</Container>
)
}
-
-const columnHelper = createColumnHelper<HttpTypes.AdminCollection>()
-
-const useColumns = () => {
- const base = useCollectionTableColumns()
-
- return useMemo(
- () => [
- ...base,
- columnHelper.display({
- id: "actions",
- cell: ({ row }) => <CollectionRowActions collection={row.original} />,
- }),
- ],
- [base]
- )
-}