1 import { HttpTypes } from "@medusajs/types"
2 import { useQueryParams } from "../../use-query-params"
3
4 type UseProductTableQueryProps = {
5 prefix?: string
6 pageSize?: number
7 }
8
9 const DEFAULT_FIELDS =
10 // TODO: Remove exclusion once we avoid including unnecessary relations by default in the query config
11 "id,title,handle,status,*collection,*sales_channels,variants.id,thumbnail,-type,-options,-tags,-images,-variants"
12
13 export const useProductTableQuery = ({
14 prefix,
15 pageSize = 20,
16 }: UseProductTableQueryProps) => {
17 const queryObject = useQueryParams(
18 [
19 "offset",
20 "order",
21 "q",
22 "created_at",
23 "updated_at",
24 "sales_channel_id",
25 "category_id",
26 "collection_id",
27 "is_giftcard",
28 "tag_id",
29 "type_id",
30 "status",
31 "id",
32 ],
33 prefix
34 )
35
36 const {
37 offset,
38 sales_channel_id,
39 created_at,
40 updated_at,
41 category_id,
42 collection_id,
43 tag_id,
44 type_id,
45 is_giftcard,
46 status,
47 order,
48 q,
49 } = queryObject
50
51 const searchParams: HttpTypes.AdminProductListParams = {
52 limit: pageSize,
53 offset: offset ? Number(offset) : 0,
54 sales_channel_id: sales_channel_id?.split(","),
55 created_at: created_at ? JSON.parse(created_at) : undefined,
56 updated_at: updated_at ? JSON.parse(updated_at) : undefined,
57 category_id: category_id?.split(","),
58 collection_id: collection_id?.split(","),
59 is_giftcard: is_giftcard ? is_giftcard === "true" : undefined,
60 order: order,
61 tag_id: tag_id ? tag_id.split(",") : undefined,
62 type_id: type_id?.split(","),
63 status: status?.split(",") as HttpTypes.AdminProductStatus[],
64 q,
65 fields: DEFAULT_FIELDS,
66 }
67
68 return {
69 searchParams,
70 raw: queryObject,
71 }
72 }