1 import { HttpTypes } from "@medusajs/types"
2 import { TFunction } from "i18next"
3 import { useMemo } from "react"
4 import { useTranslation } from "react-i18next"
5 import { useStockLocations } from "../../../hooks/api/stock-locations"
6 import {
7 createTableAdapter,
8 TableAdapter,
9 } from "../../../lib/table/table-adapters"
10 import { LocationListTableActions } from "./location-list-table-actions"
11 // Registers the stock_location_* renderers (address, fulfillment, sales channels).
12 import "./location-table-renderers"
13
14 export function createLocationTableAdapter({
15 t,
16 }: {
17 t: TFunction<"translation", undefined>
18 }): TableAdapter<HttpTypes.AdminStockLocation> {
19 return createTableAdapter<HttpTypes.AdminStockLocation>({
20 entity: "stock-locations",
21 queryPrefix: "loc",
22 pageSize: 20,
23 emptyState: {
24 empty: {
25 heading: t("stockLocations.list.noRecordsMessage"),
26 description: t("stockLocations.list.noRecordsMessageEmpty"),
27 },
28 filtered: {
29 heading: t("stockLocations.list.noRecordsMessage"),
30 description: t("stockLocations.list.noRecordsMessageFiltered"),
31 },
32 },
33 useData: (fields, params) => {
34 const { stock_locations, count, isError, error, isLoading } =
35 useStockLocations({ fields, ...params })
36 return { data: stock_locations, count, isLoading, isError, error }
37 },
38 getRowHref: (row) => `/settings/locations/${row.id}`,
39 renderRowActions: (row) => <LocationListTableActions location={row} />,
40 transformColumns: (columns) => {
41 const ALLOWED_FILTERS = [
42 "id",
43 "name",
44 "created_at",
45 "updated_at",
46 "deleted_at",
47 "sales_channels.id",
48 "address.id",
49 ]
50 return columns.map((column) => ({
51 ...column,
52 filter: !ALLOWED_FILTERS.includes(column.field)
53 ? { ...column.filter, enabled: false }
54 : column.filter,
55 }))
56 },
57 })
58 }
59
60 // eslint-disable-next-line max-len
61 export function useLocationTableAdapter(): TableAdapter<HttpTypes.AdminStockLocation> {
62 const { t } = useTranslation()
63 return useMemo(() => createLocationTableAdapter({ t }), [t])
64 }