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.

1.9 KB
Raw Blame
import { HttpTypes } from "@medusajs/types";
import { useLinkQuery } from "@mercurjs/dashboard-shared";
import { useQueryParams } from "../../use-query-params";

type UseProductTableQueryProps = {
  prefix?: string;
  pageSize?: number;
};

export const DEFAULT_FIELDS =
  // TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
  "id,title,handle,status,*collection,categories.id,categories.name,variants.id,thumbnail,-type,-tags,-images,-variants";

export const useProductTableQuery = ({
  prefix,
  pageSize = 20,
}: UseProductTableQueryProps) => {
  const linkQuery = useLinkQuery("product", DEFAULT_FIELDS);
  const queryObject = useQueryParams(
    [
      "offset",
      "order",
      "q",
      "created_at",
      "updated_at",
      "sales_channel_id",
      "category_id",
      "collection_id",
      "is_giftcard",
      "tag_id",
      "type_id",
      "status",
      "id",
    ],
    prefix,
  );

  const {
    offset,
    sales_channel_id,
    created_at,
    updated_at,
    category_id,
    collection_id,
    tag_id,
    type_id,
    is_giftcard,
    status,
    order,
    q,
  } = queryObject;

  const searchParams: HttpTypes.AdminProductListParams = {
    limit: pageSize,
    offset: offset ? Number(offset) : 0,
    sales_channel_id: sales_channel_id?.split(","),
    created_at: created_at ? JSON.parse(created_at) : undefined,
    updated_at: updated_at ? JSON.parse(updated_at) : undefined,
    category_id: category_id?.split(","),
    collection_id: collection_id?.split(","),
    is_giftcard: is_giftcard ? is_giftcard === "true" : undefined,
    order: order || "-created_at",
    tag_id: tag_id ? tag_id.split(",") : undefined,
    type_id: type_id?.split(","),
    status: status?.split(",") as HttpTypes.AdminProductStatus[],
    q,
    fields: linkQuery.fields,
  };

  return {
    searchParams,
    raw: queryObject,
  };
};