1 import { HttpTypes } from "@medusajs/types"
2 import { Button } from "@medusajs/ui"
3 import { Table } from "@tanstack/react-table"
4 import { ReactNode } from "react"
5 import { Link } from "react-router-dom"
6 import {
7 NoRecords,
8 NoResults,
9 } from "../../../../../components/common/empty-table-content"
10 import { TableFooterSkeleton } from "../../../../../components/common/skeleton"
11 import { LocalizedTablePagination } from "../../../../../components/localization/localized-table-pagination"
12 import { DataTableOrderBy } from "../../../../../components/table/data-table/data-table-order-by"
13 import { TaxRegionCard } from "../tax-region-card"
14
15 type TaxRegionTableProps = {
16 variant?: "country" | "province"
17 isPending: boolean
18 queryObject: Record<string, any>
19 count?: number
20 table: Table<HttpTypes.AdminTaxRegion>
21 action: { label: string; to: string }
22 prefix?: string
23 children?: ReactNode
24 }
25
26 export const TaxRegionTable = ({
27 variant = "country",
28 isPending,
29 action,
30 count = 0,
31 table,
32 queryObject,
33 prefix,
34 children,
35 }: TaxRegionTableProps) => {
36 if (isPending) {
37 return (
38 <div className="flex flex-col divide-y">
39 {Array.from({ length: 3 }).map((_, index) => {
40 return (
41 <div
42 key={index}
43 className="bg-ui-bg-field-component h-[52px] w-full animate-pulse"
44 />
45 )
46 })}
47 <TableFooterSkeleton layout="fit" />
48 </div>
49 )
50 }
51
52 const noQuery =
53 Object.values(queryObject).filter((v) => Boolean(v)).length === 0
54 const noResults = !isPending && count === 0 && !noQuery
55 const noRecords = !isPending && count === 0 && noQuery
56
57 const { pageIndex, pageSize } = table.getState().pagination
58
59 return (
60 <div className="flex flex-col divide-y">
61 <div className="flex flex-col justify-between gap-x-4 gap-y-3 px-6 py-4 md:flex-row md:items-center">
62 <div>{children}</div>
63 <div className="flex items-center gap-x-2">
64 {!noRecords && (
65 <div className="flex w-full items-center gap-x-2 md:w-fit">
66 {/* Re-enable when we allow searching tax regions by country name rather than country_code */}
67 {/* <div className="w-full md:w-fit">
68 <DataTableSearch prefix={prefix} />
69 </div> */}
70 <DataTableOrderBy
71 keys={["updated_at", "created_at"] as any}
72 prefix={prefix}
73 />
74 </div>
75 )}
76 <Link to={action.to}>
77 <Button size="small" variant="secondary">
78 {action.label}
79 </Button>
80 </Link>
81 </div>
82 </div>
83 {noResults && <NoResults />}
84 {noRecords && <NoRecords />}
85 {!noRecords && !noResults
86 ? !isPending
87 ? table.getRowModel().rows.map((row) => {
88 return (
89 <TaxRegionCard
90 variant={variant}
91 key={row.id}
92 taxRegion={row.original}
93 role="row"
94 aria-rowindex={row.index}
95 />
96 )
97 })
98 : Array.from({ length: 3 }).map((_, index) => {
99 return (
100 <div
101 key={index}
102 className="bg-ui-bg-field-component h-[60px] w-full animate-pulse"
103 />
104 )
105 })
106 : null}
107 {!noRecords && (
108 <LocalizedTablePagination
109 prefix={prefix}
110 canNextPage={table.getCanNextPage()}
111 canPreviousPage={table.getCanPreviousPage()}
112 count={count}
113 nextPage={table.nextPage}
114 previousPage={table.previousPage}
115 pageCount={table.getPageCount()}
116 pageIndex={pageIndex}
117 pageSize={pageSize}
118 />
119 )}
120 </div>
121 )
122 }