1 import { HttpTypes } from "@medusajs/types"
2 import { Container, Heading } from "@medusajs/ui"
3 import { keepPreviousData } from "@tanstack/react-query"
4 import { useTranslation } from "react-i18next"
5
6 import { useTaxRates } from "../../../../../hooks/api/tax-rates"
7 import { useTaxRateTableQuery } from "../../../../../hooks/table/query/use-tax-rate-table-query"
8 import { TaxOverrideTable } from "../../../common/components/tax-override-table"
9 import { useTaxOverrideTable } from "../../../common/hooks/use-tax-override-table"
10
11 type TaxRegionOverrideSectionProps = {
12 taxRegion: HttpTypes.AdminTaxRegion
13 }
14
15 const PAGE_SIZE = 10
16 const PREFIX = "o"
17
18 export const TaxRegionOverrideSection = ({
19 taxRegion,
20 }: TaxRegionOverrideSectionProps) => {
21 const { t } = useTranslation()
22
23 const { searchParams, raw } = useTaxRateTableQuery({
24 pageSize: PAGE_SIZE,
25 prefix: PREFIX,
26 })
27 const { tax_rates, count, isPending, isError, error } = useTaxRates(
28 {
29 ...searchParams,
30 tax_region_id: taxRegion.id,
31 is_default: false,
32 },
33 {
34 placeholderData: keepPreviousData,
35 }
36 )
37
38 const { table } = useTaxOverrideTable({
39 count,
40 data: tax_rates,
41 pageSize: PAGE_SIZE,
42 prefix: PREFIX,
43 })
44
45 if (isError) {
46 throw error
47 }
48
49 return (
50 <Container className="p-0">
51 <TaxOverrideTable
52 isPending={isPending}
53 table={table}
54 count={count}
55 action={{
56 label: t("actions.create"),
57 to: "overrides/create",
58 }}
59 queryObject={raw}
60 prefix={PREFIX}
61 >
62 <Heading level="h2">{t("taxRegions.taxOverrides.header")}</Heading>
63 </TaxOverrideTable>
64 </Container>
65 )
66 }