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 { DataTableSearch } from "../../../../../components/table/data-table/data-table-search"
14 import { TaxOverrideCard } from "../tax-override-card"
15
16 type TaxOverrideTableProps = {
17 isPending: boolean
18 queryObject: Record<string, any>
19 count?: number
20 table: Table<HttpTypes.AdminTaxRate>
21 action: { label: string; to: string }
22 prefix?: string
23 children?: ReactNode
24 }
25
26 export const TaxOverrideTable = ({
27 isPending,
28 action,
29 count = 0,
30 table,
31 queryObject,
32 prefix,
33 children,
34 }: TaxOverrideTableProps) => {
35 if (isPending) {
36 return (
37 <div className="flex flex-col divide-y">
38 {Array.from({ length: 3 }).map((_, index) => {
39 return (
40 <div
41 key={index}
42 className="bg-ui-bg-field-component h-[52px] w-full animate-pulse"
43 />
44 )
45 })}
46 <TableFooterSkeleton layout="fit" />
47 </div>
48 )
49 }
50
51 const noQuery =
52 Object.values(queryObject).filter((v) => Boolean(v)).length === 0
53 const noResults = !isPending && count === 0 && !noQuery
54 const noRecords = !isPending && count === 0 && noQuery
55
56 const { pageIndex, pageSize } = table.getState().pagination
57
58 return (
59 <div className="flex flex-col divide-y">
60 <div className="flex flex-col justify-between gap-x-4 gap-y-3 px-6 py-4 md:flex-row md:items-center">
61 <div>{children}</div>
62 <div className="flex items-center gap-x-2">
63 {!noRecords && (
64 <div className="flex w-full items-center gap-x-2 md:w-fit">
65 <div className="w-full md:w-fit">
66 <DataTableSearch prefix={prefix} />
67 </div>
68 <DataTableOrderBy
69 keys={
70 ["name", "rate", "code", "updated_at", "created_at"] as any
71 }
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 <TaxOverrideCard
90 key={row.id}
91 taxRate={row.original}
92 role="row"
93 aria-rowindex={row.index}
94 />
95 )
96 })
97 : Array.from({ length: 3 }).map((_, index) => {
98 return (
99 <div
100 key={index}
101 className="bg-ui-bg-field-component h-[60px] w-full animate-pulse"
102 />
103 )
104 })
105 : null}
106 {!noRecords && (
107 <LocalizedTablePagination
108 prefix={prefix}
109 canNextPage={table.getCanNextPage()}
110 canPreviousPage={table.getCanPreviousPage()}
111 count={count}
112 nextPage={table.nextPage}
113 previousPage={table.previousPage}
114 pageCount={table.getPageCount()}
115 pageIndex={pageIndex}
116 pageSize={pageSize}
117 />
118 )}
119 </div>
120 )
121 }