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.

fix(dashboard): refetch product list on search, filter and sort (#1708)

Viktor Holikf5007c7efbd8
diff --git a/packages/admin/src/pages/products/product-list/components/product-list-table/product-list-table.tsx b/packages/admin/src/pages/products/product-list/components/product-list-table/product-list-table.tsx
index 2a6f97930..944c6dd47 100644
--- a/packages/admin/src/pages/products/product-list/components/product-list-table/product-list-table.tsx
+++ b/packages/admin/src/pages/products/product-list/components/product-list-table/product-list-table.tsx
@@ -13,7 +13,11 @@ import {
RowSelectionState,
type ColumnDef,
} from "@tanstack/react-table";
-import { useExtendableTable, mergeListFilters } from "@mercurjs/dashboard-shared";
+import {
+ getListInitialData,
+ mergeListFilters,
+ useExtendableTable,
+} from "@mercurjs/dashboard-shared";
import { ReactNode, useMemo, useState, Children } from "react";
import { useTranslation } from "react-i18next";
import { Link, Outlet, useLoaderData, useNavigate } from "react-router-dom";
@@ -109,11 +113,12 @@ export const ProductListDataTable = () => {
const { mutateAsync: batchProducts } = useBatchProducts();
- const initialData = useLoaderData() as Awaited<
+ const loaderData = useLoaderData() as Awaited<
ReturnType<ReturnType<typeof productsLoader>>
>;
const { searchParams, raw } = useProductTableQuery({ pageSize: PAGE_SIZE });
+ const initialData = getListInitialData(raw, loaderData);
const { products, count, isLoading, isError, error } = useProducts(
{
...searchParams,
diff --git a/packages/dashboard-shared/src/index.ts b/packages/dashboard-shared/src/index.ts
index c82adbb8e..3809bdad6 100644
--- a/packages/dashboard-shared/src/index.ts
+++ b/packages/dashboard-shared/src/index.ts
@@ -37,3 +37,4 @@ export * from "./layout-composer"
export * from "./price-lists"
export * from "./utils/images-conventer"
export * from "./lib/rich-text"
+export * from "./lib/list-initial-data"
diff --git a/packages/dashboard-shared/src/lib/list-initial-data.spec.ts b/packages/dashboard-shared/src/lib/list-initial-data.spec.ts
new file mode 100644
index 000000000..812c7c868
--- /dev/null
+++ b/packages/dashboard-shared/src/lib/list-initial-data.spec.ts
@@ -0,0 +1,27 @@
+import { describe, expect, test } from "vitest"
+
+import { getListInitialData } from "./list-initial-data"
+
+const loaderData = { products: [{ id: "prod_1" }], count: 1 }
+
+describe("getListInitialData", () => {
+ test("returns the loader data when no query params are set", () => {
+ expect(
+ getListInitialData({ q: undefined, offset: undefined }, loaderData)
+ ).toBe(loaderData)
+ })
+
+ test("drops the loader data while searching", () => {
+ expect(getListInitialData({ q: "shirt" }, loaderData)).toBeUndefined()
+ })
+
+ test("drops the loader data for filters, ordering and pagination", () => {
+ expect(getListInitialData({ status: "published" }, loaderData)).toBeUndefined()
+ expect(getListInitialData({ order: "title" }, loaderData)).toBeUndefined()
+ expect(getListInitialData({ offset: "20" }, loaderData)).toBeUndefined()
+ })
+
+ test("treats empty values as unset", () => {
+ expect(getListInitialData({ q: "" }, loaderData)).toBe(loaderData)
+ })
+})
diff --git a/packages/dashboard-shared/src/lib/list-initial-data.ts b/packages/dashboard-shared/src/lib/list-initial-data.ts
new file mode 100644
index 000000000..7e865b07b
--- /dev/null
+++ b/packages/dashboard-shared/src/lib/list-initial-data.ts
@@ -0,0 +1,14 @@
+// A route loader only prefetches the unfiltered first page. Seeding any other
+// query key with it as `initialData` makes TanStack Query treat it as fresh
+// (within staleTime) and skip the request, so search, filters, sorting and
+// pagination would keep showing the loader's rows.
+export const getListInitialData = <TData>(
+ queryParams: Record<string, string | undefined>,
+ loaderData: TData | undefined
+): TData | undefined => {
+ const hasParams = Object.values(queryParams).some(
+ (value) => value !== undefined && value !== ""
+ )
+
+ return hasParams ? undefined : loaderData
+}
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
index 2f71e91fb..1f12644fb 100644
--- 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
@@ -1,7 +1,11 @@
import { toast, usePrompt } from "@medusajs/ui";
import { keepPreviousData } from "@tanstack/react-query";
import { createColumnHelper, type ColumnDef } from "@tanstack/react-table";
-import { useExtendableTable, mergeListFilters } from "@mercurjs/dashboard-shared";
+import {
+ getListInitialData,
+ mergeListFilters,
+ useExtendableTable,
+} from "@mercurjs/dashboard-shared";
import { useMemo } from "react";
import { useTranslation } from "react-i18next";
import { useLoaderData } from "react-router-dom";
@@ -30,7 +34,7 @@ export const ProductListDataTable = () => {
| Awaited<ReturnType<typeof productListLoader>>
| undefined;
- const initialData = searchParams.offset ? undefined : loaderData;
+ const initialData = getListInitialData(raw, loaderData);
const { products, count, isLoading, isError, error } = useProducts(
searchParams,