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(vendor): enhance product list table with new components and data handling

Jakub Sieradzki0543985a89fd
diff --git a/packages/dashboard-sdk/src/plugin.ts b/packages/dashboard-sdk/src/plugin.ts
index 8fee44dcd..e5a57df49 100644
--- a/packages/dashboard-sdk/src/plugin.ts
+++ b/packages/dashboard-sdk/src/plugin.ts
@@ -1,7 +1,7 @@
import type * as Vite from "vite"
import path from "path"
import { getFileExports } from "./utils"
-import { CONFIG_NAME, VALID_FILE_EXTENSIONS } from "./constants"
+import { CONFIG_NAME, RESOLVED_ROUTES_MODULE } from "./constants"
import { isVirtualModule, resolveVirtualModule, loadVirtualModule } from "./virtual-modules"
import type { MercurConfig, BuiltMercurConfig } from "./types"
@@ -28,6 +28,11 @@ async function loadMercurConfig(root: string): Promise<BuiltMercurConfig> {
}
}
+function isPageFile(file: string): boolean {
+ const basename = path.basename(file, path.extname(file))
+ return basename === "page"
+}
+
export function dashboardPlugin(): Vite.Plugin {
let root: string
let config: BuiltMercurConfig
@@ -50,8 +55,18 @@ export function dashboardPlugin(): Vite.Plugin {
return loadVirtualModule({ cwd: root, id, mercurConfig: config })
},
handleHotUpdate({ file, server }) {
- if (VALID_FILE_EXTENSIONS.includes(path.extname(file))) {
+ const configPath = path.resolve(root, CONFIG_NAME)
+
+ if (file === configPath) {
server.restart()
+ return
+ }
+
+ if (isPageFile(file)) {
+ const mod = server.moduleGraph.getModuleById(RESOLVED_ROUTES_MODULE)
+ if (mod) {
+ server.moduleGraph.invalidateModule(mod)
+ }
}
},
}
diff --git a/packages/vendor/src/hooks/api/inventory.tsx b/packages/vendor/src/hooks/api/inventory.tsx
index a02275135..8a932bbc7 100644
--- a/packages/vendor/src/hooks/api/inventory.tsx
+++ b/packages/vendor/src/hooks/api/inventory.tsx
@@ -199,7 +199,10 @@ export const useInventoryItemLevels = (
(options?.enabled !== undefined ? options.enabled : true),
});
- return { ...data, ...rest };
+ // API returns `inventory_levels` but consumers expect `location_levels`
+ const { inventory_levels, ...restData } = (data ?? {}) as any
+
+ return { ...restData, location_levels: inventory_levels, ...rest };
};
export const useUpdateInventoryLevel = (
diff --git a/packages/vendor/src/index.ts b/packages/vendor/src/index.ts
index a6272f66d..c99191663 100644
--- a/packages/vendor/src/index.ts
+++ b/packages/vendor/src/index.ts
@@ -1,2 +1,14 @@
export { default } from './app'
+// Compound components for page overrides
+export {
+ ProductDetailPage,
+ useProductDetailContext,
+ ProductListPage,
+ ProductCreatePage,
+ useProductCreateContext,
+} from './pages/products/exports'
+export type {
+ ProductDetailContextValue,
+ ProductCreateContextValue,
+} from './pages/products/exports'
diff --git a/packages/vendor/src/pages/inventory/[id]/_components/location-levels-table/location-list-table.tsx b/packages/vendor/src/pages/inventory/[id]/_components/location-levels-table/location-list-table.tsx
index a5cc6945a..ed932add3 100644
--- a/packages/vendor/src/pages/inventory/[id]/_components/location-levels-table/location-list-table.tsx
+++ b/packages/vendor/src/pages/inventory/[id]/_components/location-levels-table/location-list-table.tsx
@@ -25,7 +25,7 @@ export const ItemLocationListTable = ({
const columns = useLocationListTableColumns()
const filteredLocationLevels = location_levels?.filter(
- (level) => level.stock_locations.length > 0
+ (level) => level.stock_locations?.length > 0
)
const { table } = useDataTable({
diff --git a/packages/vendor/src/pages/products/_components/product-list-table/index.ts b/packages/vendor/src/pages/products/_components/product-list-table/index.ts
index ee665245d..e1da12d31 100644
--- a/packages/vendor/src/pages/products/_components/product-list-table/index.ts
+++ b/packages/vendor/src/pages/products/_components/product-list-table/index.ts
@@ -1 +1,3 @@
export * from "./product-list-table"
+export * from "./product-list-header"
+export * from "./product-list-data-table"
diff --git a/packages/vendor/src/pages/products/_components/product-list-table/product-list-data-table.tsx b/packages/vendor/src/pages/products/_components/product-list-table/product-list-data-table.tsx
new file mode 100644
index 000000000..b8867e2a5
--- /dev/null
+++ b/packages/vendor/src/pages/products/_components/product-list-table/product-list-data-table.tsx
@@ -0,0 +1,256 @@
+import { Checkbox, toast, usePrompt } from "@medusajs/ui"
+import { keepPreviousData } from "@tanstack/react-query"
+import {
+ createColumnHelper,
+ OnChangeFn,
+ RowSelectionState,
+} from "@tanstack/react-table"
+import { useMemo, useState } from "react"
+import { useTranslation } from "react-i18next"
+import { Trash } from "@medusajs/icons"
+
+import { ExtendedAdminProduct } from "@custom-types/products"
+import { ActionMenu } from "@components/common/action-menu"
+import { _DataTable } from "@components/table/data-table"
+import {
+ useDeleteProduct,
+ useBulkDeleteProducts,
+ useProducts,
+} from "@hooks/api/products"
+import { useProductTableColumns } from "@hooks/table/columns/use-product-table-columns"
+import { useProductTableFilters } from "@hooks/table/filters/use-product-table-filters"
+import { useProductTableQuery } from "@hooks/table/query/use-product-table-query"
+import { useDataTable } from "@hooks/use-data-table"
+
+export const PAGE_SIZE = 10
+
+export const ProductListDataTable = () => {
+ const { t } = useTranslation()
+
+ const [rowSelection, setRowSelection] = useState<RowSelectionState>({})
+
+ const updater: OnChangeFn<RowSelectionState> = (newSelection) => {
+ const update =
+ typeof newSelection === "function"
+ ? newSelection(rowSelection)
+ : newSelection
+ setRowSelection(update)
+ }
+
+ const { searchParams, raw } = useProductTableQuery({
+ pageSize: PAGE_SIZE,
+ })
+
+ const options = {
+ placeholderData: keepPreviousData,
+ }
+
+ const { products, count, isLoading, isError, error } = useProducts(
+ searchParams,
+ options
+ )
+
+ const filters = useProductTableFilters()
+ const columns = useColumns()
+
+ const { table } = useDataTable({
+ data: products,
+ columns,
+ count,
+ enablePagination: true,
+ enableRowSelection: true,
+ pageSize: PAGE_SIZE,
+ getRowId: (row) => row?.id || "",
+ rowSelection: {
+ state: rowSelection,
+ updater,
+ },
+ })
+
+ const { mutateAsync } = useBulkDeleteProducts()
+ const prompt = usePrompt()
+
+ const handleDelete = async () => {
+ const keys = Object.keys(rowSelection)
+
+ if (keys.length === 0) {
+ return
+ }
+
+ const res = await prompt({
+ title: t("products.bulkDelete.title"),
+ description: t("products.bulkDelete.description", {
+ count: keys.length,
+ }),
+ confirmText: t("actions.delete"),
+ cancelText: t("actions.cancel"),
+ })
+
+ if (!res) {
+ return
+ }
+
+ await mutateAsync(keys, {
+ onSuccess: () => {
+ setRowSelection({})
+ toast.success(
+ t("products.bulkDelete.success", {
+ count: keys.length,
+ })
+ )
+ },
+ onError: (error) => {
+ toast.error(t("products.bulkDelete.error"), {
+ description: error.message,
+ })
+ },
+ })
+ }
+
+ if (isError) {
+ throw error
+ }
+
+ return (
+ <_DataTable
+ table={table}
+ columns={columns}
+ count={count}
+ pageSize={PAGE_SIZE}
+ filters={filters}
+ search
+ pagination
+ isLoading={isLoading}
+ queryObject={raw}
+ navigateTo={(row) => `${row.original.id}`}
+ orderBy={[
+ { key: "title", label: t("fields.title") },
+ {
+ key: "created_at",
+ label: t("fields.createdAt"),
+ },
+ {
+ key: "updated_at",
+ label: t("fields.updatedAt"),
+ },
+ ]}
+ commands={[
+ {
+ action: handleDelete,
+ label: t("actions.delete"),
+ shortcut: "d",
+ },
+ ]}
+ noRecords={{
+ title: t("products.list.noRecordsTitle"),
+ message: t("products.list.noRecordsMessage"),
+ action: {
+ to: "/products/create",
+ label: t("actions.add"),
+ },
+ }}
+ />
+ )
+}
+
+const ProductActions = ({ product }: { product: ExtendedAdminProduct }) => {
+ const { t } = useTranslation()
+ const prompt = usePrompt()
+ const { mutateAsync } = useDeleteProduct(product.id)
+
+ const handleDelete = async () => {
+ const res = await prompt({
+ title: t("general.areYouSure"),
+ description: t("products.deleteWarning", {
+ title: product.title,
+ }),
+ confirmText: t("actions.delete"),
+ cancelText: t("actions.cancel"),
+ })
+
+ if (!res) {
+ return
+ }
+
+ await mutateAsync(undefined, {
+ onSuccess: () => {
+ toast.success(t("products.toasts.delete.success.header"), {
+ description: t("products.toasts.delete.success.description", {
+ title: product.title,
+ }),
+ })
+ },
+ onError: (e) => {
+ toast.error(t("products.toasts.delete.error.header"), {
+ description: e.message,
+ })
+ },
+ })
+ }
+
+ return (
+ <ActionMenu
+ groups={[
+ {
+ actions: [
+ {
+ icon: <Trash />,
+ label: t("actions.delete"),
+ onClick: handleDelete,
+ },
+ ],
+ },
+ ]}
+ />
+ )
+}
+
+const columnHelper = createColumnHelper<ExtendedAdminProduct>()
+
+const useColumns = () => {
+ const { t } = useTranslation()
+ const base = useProductTableColumns()
+
+ const columns = useMemo(
+ () => [
+ columnHelper.display({
+ id: "select",
+ header: ({ table }) => {
+ return (
+ <Checkbox
+ checked={
+ table.getIsSomePageRowsSelected()
+ ? "indeterminate"
+ : table.getIsAllPageRowsSelected()
+ }
+ onCheckedChange={(value) =>
+ table.toggleAllPageRowsSelected(!!value)
+ }
+ />
+ )
+ },
+ cell: ({ row }) => {
+ return (
+ <Checkbox
+ checked={row.getIsSelected()}
+ onCheckedChange={(value) => row.toggleSelected(!!value)}
+ onClick={(e) => {
+ e.stopPropagation()
+ }}
+ />
+ )
+ },
+ }),
+ ...base,
+ columnHelper.display({
+ id: "actions",
+ cell: ({ row }) => {
+ return <ProductActions product={row.original} />
+ },
+ }),
+ ],
+ [base, t]
+ )
+
+ return columns
+}
diff --git a/packages/vendor/src/pages/products/_components/product-list-table/product-list-header.tsx b/packages/vendor/src/pages/products/_components/product-list-table/product-list-header.tsx
new file mode 100644
index 000000000..e66b66fd8
--- /dev/null
+++ b/packages/vendor/src/pages/products/_components/product-list-table/product-list-header.tsx
@@ -0,0 +1,75 @@
+import { Children, ReactNode } from "react"
+import { useTranslation } from "react-i18next"
+import { Link } from "react-router-dom"
+import { Button, Heading } from "@medusajs/ui"
+
+export const ProductListTitle = () => {
+ const { t } = useTranslation()
+ return <Heading level="h2">{t("products.domain")}</Heading>
+}
+
+export const ProductListCreateButton = () => {
+ const { t } = useTranslation()
+ return (
+ <Button size="small" variant="primary" asChild>
+ <Link to="create">{t("actions.create")}</Link>
+ </Button>
+ )
+}
+
+export const ProductListImportButton = () => {
+ const { t } = useTranslation()
+ return (
+ <Button size="small" variant="secondary" asChild>
+ <Link to="import">{t("actions.import")}</Link>
+ </Button>
+ )
+}
+
+export const ProductListExportButton = () => {
+ const { t } = useTranslation()
+ return (
+ <Button size="small" variant="secondary" asChild>
+ <Link to={`export${location.search}`}>{t("actions.export")}</Link>
+ </Button>
+ )
+}
+
+export const ProductListActions = ({
+ children,
+}: {
+ children?: ReactNode
+}) => {
+ return (
+ <div className="flex items-center justify-center gap-x-2">
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <ProductListExportButton />
+ <ProductListImportButton />
+ <ProductListCreateButton />
+ </>
+ )}
+ </div>
+ )
+}
+
+export const ProductListHeader = ({
+ children,
+}: {
+ children?: ReactNode
+}) => {
+ return (
+ <div className="flex items-center justify-between px-6 py-4">
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <ProductListTitle />
+ <ProductListActions />
+ </>
+ )}
+ </div>
+ )
+}
diff --git a/packages/vendor/src/pages/products/_components/product-list-table/product-list-table.tsx b/packages/vendor/src/pages/products/_components/product-list-table/product-list-table.tsx
index bb3f237b9..363194327 100644
--- a/packages/vendor/src/pages/products/_components/product-list-table/product-list-table.tsx
+++ b/packages/vendor/src/pages/products/_components/product-list-table/product-list-table.tsx
@@ -1,274 +1,22 @@
-import { Trash } from "@medusajs/icons";
-import {
- Button,
- Container,
- Heading,
- toast,
- usePrompt,
- Checkbox,
-} from "@medusajs/ui";
-import { keepPreviousData } from "@tanstack/react-query";
-import {
- createColumnHelper,
- OnChangeFn,
- RowSelectionState,
-} from "@tanstack/react-table";
-import { useMemo, useState } from "react";
-import { useTranslation } from "react-i18next";
-import { Link, Outlet } from "react-router-dom";
+import { Children, ReactNode } from "react"
+import { Container } from "@medusajs/ui"
+import { Outlet } from "react-router-dom"
-import { ExtendedAdminProduct } from "@custom-types/products";
-import { ActionMenu } from "@components/common/action-menu";
-import { _DataTable } from "@components/table/data-table";
-import {
- useDeleteProduct,
- useBulkDeleteProducts,
- useProducts,
-} from "@hooks/api/products";
-import { useProductTableColumns } from "@hooks/table/columns/use-product-table-columns";
-import { useProductTableFilters } from "@hooks/table/filters/use-product-table-filters";
-import { useProductTableQuery } from "@hooks/table/query/use-product-table-query";
-import { useDataTable } from "@hooks/use-data-table";
-
-export const PAGE_SIZE = 10;
-
-export const ProductListTable = () => {
- const { t } = useTranslation();
-
- const [rowSelection, setRowSelection] = useState<RowSelectionState>({});
-
- const updater: OnChangeFn<RowSelectionState> = (newSelection) => {
- const update =
- typeof newSelection === "function"
- ? newSelection(rowSelection)
- : newSelection;
-
- setRowSelection(update);
- };
-
- const { searchParams, raw } = useProductTableQuery({
- pageSize: PAGE_SIZE,
- });
-
- const options = {
- placeholderData: keepPreviousData,
- };
-
- const { products, count, isLoading, isError, error } = useProducts(
- searchParams,
- options,
- );
-
- const filters = useProductTableFilters();
- const columns = useColumns();
-
- const { table } = useDataTable({
- data: products,
- columns,
- count,
- enablePagination: true,
- enableRowSelection: true,
- pageSize: PAGE_SIZE,
- getRowId: (row) => row?.id || "",
- rowSelection: {
- state: rowSelection,
- updater,
- },
- });
-
- const { mutateAsync } = useBulkDeleteProducts();
- const prompt = usePrompt();
-
- const handleDelete = async () => {
- const keys = Object.keys(rowSelection);
-
- if (keys.length === 0) {
- return;
- }
-
- const res = await prompt({
- title: t("products.bulkDelete.title"),
- description: t("products.bulkDelete.description", {
- count: keys.length,
- }),
- confirmText: t("actions.delete"),
- cancelText: t("actions.cancel"),
- });
-
- if (!res) {
- return;
- }
-
- await mutateAsync(keys, {
- onSuccess: () => {
- setRowSelection({});
- toast.success(
- t("products.bulkDelete.success", {
- count: keys.length,
- }),
- );
- },
- onError: (error) => {
- toast.error(t("products.bulkDelete.error"), {
- description: error.message,
- });
- },
- });
- };
-
- if (isError) {
- throw error;
- }
+import { ProductListHeader } from "./product-list-header"
+import { ProductListDataTable } from "./product-list-data-table"
+export const ProductListTable = ({ children }: { children?: ReactNode }) => {
return (
<Container className="divide-y p-0">
- <div className="flex items-center justify-between px-6 py-4">
- <Heading level="h2">{t("products.domain")}</Heading>
- <Button size="small" variant="primary" asChild>
- <Link to="create">{t("actions.create")}</Link>
- </Button>
- </div>
- <_DataTable
- table={table}
- columns={columns}
- count={count}
- pageSize={PAGE_SIZE}
- filters={filters}
- search
- pagination
- isLoading={isLoading}
- queryObject={raw}
- navigateTo={(row) => `${row.original.id}`}
- orderBy={[
- { key: "title", label: t("fields.title") },
- {
- key: "created_at",
- label: t("fields.createdAt"),
- },
- {
- key: "updated_at",
- label: t("fields.updatedAt"),
- },
- ]}
- commands={[
- {
- action: handleDelete,
- label: t("actions.delete"),
- shortcut: "d",
- },
- ]}
- noRecords={{
- title: t("products.list.noRecordsTitle"),
- message: t("products.list.noRecordsMessage"),
- action: {
- to: "/products/create",
- label: t("actions.add"),
- },
- }}
- />
+ {Children.count(children) > 0 ? (
+ children
+ ) : (
+ <>
+ <ProductListHeader />
+ <ProductListDataTable />
+ </>
+ )}
<Outlet />
</Container>
- );
-};
-
-const ProductActions = ({ product }: { product: ExtendedAdminProduct }) => {
- const { t } = useTranslation();
- const prompt = usePrompt();
- const { mutateAsync } = useDeleteProduct(product.id);
-
- const handleDelete = async () => {
- const res = await prompt({
- title: t("general.areYouSure"),
- description: t("products.deleteWarning", {
- title: product.title,
- }),
- confirmText: t("actions.delete"),
- cancelText: t("actions.cancel"),
- });
-
- if (!res) {
- return;
- }
-
- await mutateAsync(undefined, {
- onSuccess: () => {
- toast.success(t("products.toasts.delete.success.header"), {
- description: t("products.toasts.delete.success.description", {
- title: product.title,
- }),
- });
- },
- onError: (e) => {
- toast.error(t("products.toasts.delete.error.header"), {
- description: e.message,
- });
- },
- });
- };
-
- return (
- <ActionMenu
- groups={[
- {
- actions: [
- {
- icon: <Trash />,
- label: t("actions.delete"),
- onClick: handleDelete,
- },
- ],
- },
- ]}
- />
- );
-};
-
-const columnHelper = createColumnHelper<ExtendedAdminProduct>();
-
-const useColumns = () => {
- const { t } = useTranslation();
- const base = useProductTableColumns();
-
- const columns = useMemo(
- () => [
- columnHelper.display({
- id: "select",
- header: ({ table }) => {
- return (
- <Checkbox
- checked={
- table.getIsSomePageRowsSelected()
- ? "indeterminate"
- : table.getIsAllPageRowsSelected()
- }
- onCheckedChange={(value) =>
- table.toggleAllPageRowsSelected(!!value)
- }
- />
- );
- },
- cell: ({ row }) => {
- return (
- <Checkbox
- checked={row.getIsSelected()}
- onCheckedChange={(value) => row.toggleSelected(!!value)}
- onClick={(e) => {
- e.stopPropagation();
- }}
- />
- );
- },
- }),
- ...base,
- columnHelper.display({
- id: "actions",
- cell: ({ row }) => {
- return <ProductActions product={row.original} />;
- },
- }),
- ],
- [base, t],
- );
-
- return columns;
-};
+ )
+}
diff --git a/packages/vendor/src/pages/products/exports.ts b/packages/vendor/src/pages/products/exports.ts
new file mode 100644
index 000000000..e6aeb6ae3
--- /dev/null
+++ b/packages/vendor/src/pages/products/exports.ts
@@ -0,0 +1,9 @@
+export { ProductDetailPage } from "./[id]/product-detail-page"
+export { useProductDetailContext } from "./[id]/context"
+export type { ProductDetailContextValue, ProductDetailData } from "./[id]/context"
+
+export { ProductListPage } from "./product-list-page"
+
+export { ProductCreatePage } from "./create/product-create-page"
+export { useProductCreateContext } from "./create/context"
+export type { ProductCreateContextValue } from "./create/context"
diff --git a/packages/vendor/src/pages/products/product-list-page.tsx b/packages/vendor/src/pages/products/product-list-page.tsx
index b57ac3c42..38a8bf9b2 100644
--- a/packages/vendor/src/pages/products/product-list-page.tsx
+++ b/packages/vendor/src/pages/products/product-list-page.tsx
@@ -2,7 +2,16 @@ import { Children, ReactNode } from "react"
import { SingleColumnPage } from "@components/layout/pages"
-import { ProductListTable } from "./_components/product-list-table"
+import {
+ ProductListTable,
+ ProductListDataTable,
+ ProductListHeader,
+ ProductListActions,
+ ProductListTitle,
+ ProductListCreateButton,
+ ProductListImportButton,
+ ProductListExportButton,
+} from "./_components/product-list-table"
const Root = ({ children }: { children?: ReactNode }) => {
return (
@@ -13,5 +22,15 @@ const Root = ({ children }: { children?: ReactNode }) => {
}
export const ProductListPage = Object.assign(Root, {
+ // Backward compat
Table: ProductListTable,
+ // Sections
+ Header: ProductListHeader,
+ Actions: ProductListActions,
+ DataTable: ProductListDataTable,
+ // Individual elements
+ Title: ProductListTitle,
+ CreateButton: ProductListCreateButton,
+ ImportButton: ProductListImportButton,
+ ExportButton: ProductListExportButton,
})
diff --git a/packages/vendor/src/pages/promotions/[id]/[ruleType]/edit/index.tsx b/packages/vendor/src/pages/promotions/[id]/[ruleType]/edit/index.tsx
new file mode 100644
index 000000000..dc386395e
--- /dev/null
+++ b/packages/vendor/src/pages/promotions/[id]/[ruleType]/edit/index.tsx
@@ -0,0 +1 @@
+export { Component } from "../../../common/edit-rules"