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): enhance customer detail and list pages with context and … (#759)

Jakub Sieradzki0d63719946b8
diff --git a/packages/admin/src/get-route-map.tsx b/packages/admin/src/get-route-map.tsx
index 0034ba238..2987724ea 100644
--- a/packages/admin/src/get-route-map.tsx
+++ b/packages/admin/src/get-route-map.tsx
@@ -628,11 +628,11 @@ export function getRouteMap({
{
path: ":id",
lazy: async () => {
- const { Component, Breadcrumb, loader } =
+ const { Breadcrumb, loader } =
await import("./pages/customers/customer-detail");
return {
- Component,
+ Component: Outlet,
loader,
handle: {
breadcrumb: (
@@ -643,28 +643,34 @@ export function getRouteMap({
},
children: [
{
- path: "edit",
- lazy: () => import("./pages/customers/customer-edit"),
- },
- {
- path: "create-address",
- lazy: () =>
- import("./pages/customers/customer-create-address"),
- },
- {
- path: "add-customer-groups",
- lazy: () =>
- import("./pages/customers/customers-add-customer-group"),
- },
- {
- path: ":order_id/transfer",
- lazy: () =>
- import("./pages/orders/order-request-transfer"),
- },
- {
- path: "metadata/edit",
- lazy: () =>
- import("./pages/customers/customer-metadata"),
+ path: "",
+ lazy: () => import("./pages/customers/customer-detail"),
+ children: [
+ {
+ path: "edit",
+ lazy: () => import("./pages/customers/customer-edit"),
+ },
+ {
+ path: "create-address",
+ lazy: () =>
+ import("./pages/customers/customer-create-address"),
+ },
+ {
+ path: "add-customer-groups",
+ lazy: () =>
+ import("./pages/customers/customers-add-customer-group"),
+ },
+ {
+ path: ":order_id/transfer",
+ lazy: () =>
+ import("./pages/orders/order-request-transfer"),
+ },
+ {
+ path: "metadata/edit",
+ lazy: () =>
+ import("./pages/customers/customer-metadata"),
+ },
+ ],
},
],
},
diff --git a/packages/admin/src/lib/compound-composition.ts b/packages/admin/src/lib/compound-composition.ts
new file mode 100644
index 000000000..0553b0655
--- /dev/null
+++ b/packages/admin/src/lib/compound-composition.ts
@@ -0,0 +1,38 @@
+import { Children, Fragment, isValidElement, ReactNode } from "react"
+
+const hasAllowedCompositionChild = (
+ node: ReactNode,
+ allowedTypes: ReadonlySet<unknown>
+): boolean => {
+ if (!isValidElement(node)) {
+ return false
+ }
+
+ if (allowedTypes.has(node.type)) {
+ return true
+ }
+
+ if (node.type === Fragment) {
+ const fragmentChildren = Children.toArray(node.props.children)
+ return fragmentChildren.some((child) =>
+ hasAllowedCompositionChild(child, allowedTypes)
+ )
+ }
+
+ return false
+}
+
+export const hasExplicitCompoundComposition = (
+ children: ReactNode,
+ allowedTypes: readonly unknown[]
+) => {
+ if (Children.count(children) === 0) {
+ return false
+ }
+
+ const allowedTypesSet = new Set(allowedTypes)
+ return Children.toArray(children).some((child) =>
+ hasAllowedCompositionChild(child, allowedTypesSet)
+ )
+}
+
diff --git a/packages/admin/src/pages/customers/customer-detail/components/customer-address-section/customer-address-section.tsx b/packages/admin/src/pages/customers/customer-detail/components/customer-address-section/customer-address-section.tsx
index 78d9a798d..5a2aea017 100644
--- a/packages/admin/src/pages/customers/customer-detail/components/customer-address-section/customer-address-section.tsx
+++ b/packages/admin/src/pages/customers/customer-detail/components/customer-address-section/customer-address-section.tsx
@@ -9,13 +9,11 @@ import { NoRecords } from "../../../../../components/common/empty-table-content"
import { Listicle } from "../../../../../components/common/listicle"
import { useDeleteCustomerAddress } from "../../../../../hooks/api/customers"
-type CustomerAddressSectionProps = {
- customer: HttpTypes.AdminCustomer
-}
-
export const CustomerAddressSection = ({
customer,
-}: CustomerAddressSectionProps) => {
+}: {
+ customer: HttpTypes.AdminCustomer
+}) => {
const { t } = useTranslation()
const prompt = usePrompt()
const navigate = useNavigate()
diff --git a/packages/admin/src/pages/customers/customer-detail/components/customer-general-section/customer-general-section.tsx b/packages/admin/src/pages/customers/customer-detail/components/customer-general-section/customer-general-section.tsx
index b68b5e5b6..4be5241d2 100644
--- a/packages/admin/src/pages/customers/customer-detail/components/customer-general-section/customer-general-section.tsx
+++ b/packages/admin/src/pages/customers/customer-detail/components/customer-general-section/customer-general-section.tsx
@@ -14,13 +14,11 @@ import { useNavigate } from "react-router-dom"
import { ActionMenu } from "../../../../../components/common/action-menu"
import { useDeleteCustomer } from "../../../../../hooks/api/customers"
-type CustomerGeneralSectionProps = {
- customer: HttpTypes.AdminCustomer
-}
-
export const CustomerGeneralSection = ({
customer,
-}: CustomerGeneralSectionProps) => {
+}: {
+ customer: HttpTypes.AdminCustomer
+}) => {
const { t } = useTranslation()
const prompt = usePrompt()
const navigate = useNavigate()
diff --git a/packages/admin/src/pages/customers/customer-detail/components/customer-group-section/customer-group-section.tsx b/packages/admin/src/pages/customers/customer-detail/components/customer-group-section/customer-group-section.tsx
index b3f780f6a..494c760bb 100644
--- a/packages/admin/src/pages/customers/customer-detail/components/customer-group-section/customer-group-section.tsx
+++ b/packages/admin/src/pages/customers/customer-detail/components/customer-group-section/customer-group-section.tsx
@@ -28,16 +28,14 @@ import { useCustomerGroupTableFilters } from "../../../../../hooks/table/filters
import { useCustomerGroupTableQuery } from "../../../../../hooks/table/query/use-customer-group-table-query"
import { useDataTable } from "../../../../../hooks/use-data-table"
-type CustomerGroupSectionProps = {
- customer: HttpTypes.AdminCustomer
-}
-
const PAGE_SIZE = 10
const PREFIX = "cusgr"
export const CustomerGroupSection = ({
customer,
-}: CustomerGroupSectionProps) => {
+}: {
+ customer: HttpTypes.AdminCustomer
+}) => {
const prompt = usePrompt()
const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
diff --git a/packages/admin/src/pages/customers/customer-detail/components/customer-order-section/customer-order-section.tsx b/packages/admin/src/pages/customers/customer-detail/components/customer-order-section/customer-order-section.tsx
index a3a7f71d9..6a8fa42de 100644
--- a/packages/admin/src/pages/customers/customer-detail/components/customer-order-section/customer-order-section.tsx
+++ b/packages/admin/src/pages/customers/customer-detail/components/customer-order-section/customer-order-section.tsx
@@ -13,10 +13,6 @@ import { useOrderTableFilters } from "../../../../../hooks/table/filters/use-ord
import { useOrderTableQuery } from "../../../../../hooks/table/query/use-order-table-query"
import { useDataTable } from "../../../../../hooks/use-data-table"
-type CustomerGeneralSectionProps = {
- customer: HttpTypes.AdminCustomer
-}
-
const PREFIX = "cusord"
const PAGE_SIZE = 10
const DEFAULT_RELATIONS = "*customer,*items,*sales_channel"
@@ -25,7 +21,9 @@ const DEFAULT_FIELDS =
export const CustomerOrderSection = ({
customer,
-}: CustomerGeneralSectionProps) => {
+}: {
+ customer: HttpTypes.AdminCustomer
+}) => {
const { t } = useTranslation()
const { searchParams, raw } = useOrderTableQuery({
diff --git a/packages/admin/src/pages/customers/customer-detail/customer-detail.tsx b/packages/admin/src/pages/customers/customer-detail/customer-detail.tsx
index ffdd597b6..355bd1dce 100644
--- a/packages/admin/src/pages/customers/customer-detail/customer-detail.tsx
+++ b/packages/admin/src/pages/customers/customer-detail/customer-detail.tsx
@@ -1,3 +1,4 @@
+import { Children, ReactNode } from "react"
import { useLoaderData, useParams } from "react-router-dom"
import { SingleColumnPageSkeleton } from "../../../components/common/skeleton"
@@ -9,7 +10,7 @@ import { CustomerGroupSection } from "./components/customer-group-section"
import { CustomerOrderSection } from "./components/customer-order-section"
import { customerLoader } from "./loader"
-export const CustomerDetail = () => {
+const Root = ({ children }: { children?: ReactNode }) => {
const { id } = useParams()
const initialData = useLoaderData() as Awaited<
@@ -29,13 +30,10 @@ export const CustomerDetail = () => {
throw error
}
- return (
- <TwoColumnPage
- data={customer}
- hasOutlet
- showJSON
- showMetadata
- >
+ return Children.count(children) > 0 ? (
+ <>{children}</>
+ ) : (
+ <TwoColumnPage data={customer} hasOutlet showJSON showMetadata>
<TwoColumnPage.Main>
<CustomerGeneralSection customer={customer} />
<CustomerOrderSection customer={customer} />
@@ -47,3 +45,12 @@ export const CustomerDetail = () => {
</TwoColumnPage>
)
}
+
+export const CustomerDetailPage = Object.assign(Root, {
+ Main: TwoColumnPage.Main,
+ Sidebar: TwoColumnPage.Sidebar,
+ MainGeneralSection: CustomerGeneralSection,
+ MainOrderSection: CustomerOrderSection,
+ MainGroupSection: CustomerGroupSection,
+ SidebarAddressSection: CustomerAddressSection,
+})
diff --git a/packages/admin/src/pages/customers/customer-detail/index.ts b/packages/admin/src/pages/customers/customer-detail/index.ts
index edd694dd1..9fa81bade 100644
--- a/packages/admin/src/pages/customers/customer-detail/index.ts
+++ b/packages/admin/src/pages/customers/customer-detail/index.ts
@@ -1,3 +1,3 @@
export { CustomerDetailBreadcrumb as Breadcrumb } from "./breadcrumb"
-export { CustomerDetail as Component } from "./customer-detail"
+export { CustomerDetailPage as Component } from "./customer-detail"
export { customerLoader as loader } from "./loader"
diff --git a/packages/admin/src/pages/customers/customer-list/components/customer-list-table/customer-list-table.tsx b/packages/admin/src/pages/customers/customer-list/components/customer-list-table/customer-list-table.tsx
index 5716433fa..51d765a6f 100644
--- a/packages/admin/src/pages/customers/customer-list/components/customer-list-table/customer-list-table.tsx
+++ b/packages/admin/src/pages/customers/customer-list/components/customer-list-table/customer-list-table.tsx
@@ -2,7 +2,7 @@ import { PencilSquare } from "@medusajs/icons"
import { Button, Container, Heading } from "@medusajs/ui"
import { keepPreviousData } from "@tanstack/react-query"
import { createColumnHelper } from "@tanstack/react-table"
-import { useMemo } from "react"
+import { Children, ReactNode, useMemo } from "react"
import { useTranslation } from "react-i18next"
import { Link } from "react-router-dom"
@@ -17,7 +17,57 @@ import { useDataTable } from "../../../../../hooks/use-data-table"
const PAGE_SIZE = 20
-export const CustomerListTable = () => {
+export const CustomerListTitle = () => {
+ const { t } = useTranslation()
+
+ return (
+ <Heading data-testid="customer-list-heading">{t("customers.domain")}</Heading>
+ )
+}
+
+export const CustomerListCreateButton = () => {
+ const { t } = useTranslation()
+
+ return (
+ <Link to="/customers/create" data-testid="customer-list-create-link">
+ <Button size="small" variant="secondary" data-testid="customer-list-create-button">
+ {t("actions.create")}
+ </Button>
+ </Link>
+ )
+}
+
+export const CustomerListActions = ({ children }: { children?: ReactNode }) => {
+ return (
+ <div className="flex items-center gap-x-2">
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <CustomerListCreateButton />
+ )}
+ </div>
+ )
+}
+
+export const CustomerListHeader = ({ children }: { children?: ReactNode }) => {
+ return (
+ <div
+ className="flex items-center justify-between px-6 py-4"
+ data-testid="customer-list-header"
+ >
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <CustomerListTitle />
+ <CustomerListActions />
+ </>
+ )}
+ </div>
+ )
+}
+
+export const CustomerListDataTable = () => {
const { t } = useTranslation()
const { searchParams, raw } = useCustomerTableQuery({ pageSize: PAGE_SIZE })
@@ -46,38 +96,43 @@ export const CustomerListTable = () => {
throw error
}
+ return (
+ <_DataTable
+ table={table}
+ columns={columns}
+ pageSize={PAGE_SIZE}
+ count={count}
+ filters={filters}
+ orderBy={[
+ { key: "email", label: t("fields.email") },
+ { key: "first_name", label: t("fields.firstName") },
+ { key: "last_name", label: t("fields.lastName") },
+ { key: "has_account", label: t("customers.hasAccount") },
+ { key: "created_at", label: t("fields.createdAt") },
+ { key: "updated_at", label: t("fields.updatedAt") },
+ ]}
+ isLoading={isLoading}
+ navigateTo={(row) => row.original.id}
+ search
+ queryObject={raw}
+ noRecords={{
+ message: t("customers.list.noRecordsMessage"),
+ }}
+ />
+ )
+}
+
+export const CustomerListTable = ({ children }: { children?: ReactNode }) => {
return (
<Container className="divide-y p-0" data-testid="customer-list-container">
- <div className="flex items-center justify-between px-6 py-4" data-testid="customer-list-header">
- <Heading data-testid="customer-list-heading">{t("customers.domain")}</Heading>
- <Link to="/customers/create" data-testid="customer-list-create-link">
- <Button size="small" variant="secondary" data-testid="customer-list-create-button">
- {t("actions.create")}
- </Button>
- </Link>
- </div>
- <_DataTable
- table={table}
- columns={columns}
- pageSize={PAGE_SIZE}
- count={count}
- filters={filters}
- orderBy={[
- { key: "email", label: t("fields.email") },
- { key: "first_name", label: t("fields.firstName") },
- { key: "last_name", label: t("fields.lastName") },
- { key: "has_account", label: t("customers.hasAccount") },
- { key: "created_at", label: t("fields.createdAt") },
- { key: "updated_at", label: t("fields.updatedAt") },
- ]}
- isLoading={isLoading}
- navigateTo={(row) => row.original.id}
- search
- queryObject={raw}
- noRecords={{
- message: t("customers.list.noRecordsMessage"),
- }}
- />
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <CustomerListHeader />
+ <CustomerListDataTable />
+ </>
+ )}
</Container>
)
}
diff --git a/packages/admin/src/pages/customers/customer-list/customer-list.tsx b/packages/admin/src/pages/customers/customer-list/customer-list.tsx
index 4c9e78f5a..8863a90a5 100644
--- a/packages/admin/src/pages/customers/customer-list/customer-list.tsx
+++ b/packages/admin/src/pages/customers/customer-list/customer-list.tsx
@@ -1,10 +1,27 @@
+import { Children, ReactNode } from "react"
import { SingleColumnPage } from "../../../components/layout/pages"
-import { CustomerListTable } from "./components/customer-list-table"
+import {
+ CustomerListTable,
+ CustomerListHeader,
+ CustomerListTitle,
+ CustomerListActions,
+ CustomerListCreateButton,
+ CustomerListDataTable,
+} from "./components/customer-list-table"
-export const CustomersList = () => {
+const Root = ({ children }: { children?: ReactNode }) => {
return (
<SingleColumnPage>
- <CustomerListTable />
+ {Children.count(children) > 0 ? children : <CustomerListTable />}
</SingleColumnPage>
)
}
+
+export const CustomerListPage = Object.assign(Root, {
+ Table: CustomerListTable,
+ Header: CustomerListHeader,
+ HeaderTitle: CustomerListTitle,
+ HeaderActions: CustomerListActions,
+ HeaderCreateButton: CustomerListCreateButton,
+ DataTable: CustomerListDataTable,
+})
diff --git a/packages/admin/src/pages/customers/customer-list/index.ts b/packages/admin/src/pages/customers/customer-list/index.ts
index f945dbca5..cd38a8bf1 100644
--- a/packages/admin/src/pages/customers/customer-list/index.ts
+++ b/packages/admin/src/pages/customers/customer-list/index.ts
@@ -1 +1 @@
-export { CustomersList as Component } from "./customer-list"
+export { CustomerListPage as Component } from "./customer-list"
diff --git a/packages/admin/src/pages/index.ts b/packages/admin/src/pages/index.ts
index 8fd1f0779..818e14ddb 100644
--- a/packages/admin/src/pages/index.ts
+++ b/packages/admin/src/pages/index.ts
@@ -1,3 +1,5 @@
+export { CustomerDetailPage } from "./customers/customer-detail/customer-detail"
+export { CustomerListPage } from "./customers/customer-list/customer-list"
export { ProductDetailPage } from "./products/product-detail/product-detail"
export { ProductListPage } from "./products/product-list/product-list"
export { ProductVariantDetailPage } from "./product-variants/product-variant-detail/product-variant-detail"