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 import { useTaxRegions } from "../../../../../hooks/api/tax-regions"
6 import { useTaxRegionTableQuery } from "../../../../../hooks/table/query/use-tax-region-table-query"
7 import { getCountryProvinceObjectByIso2 } from "../../../../../lib/data/country-states"
8 import { TaxRegionTable } from "../../../common/components/tax-region-table"
9 import { useTaxRegionTable } from "../../../common/hooks/use-tax-region-table"
10
11 const PAGE_SIZE = 10
12 const PREFIX = "p"
13
14 type TaxRateListProps = {
15 taxRegion: HttpTypes.AdminTaxRegion
16 showSublevelRegions: boolean
17 }
18
19 export const TaxRegionProvinceSection = ({
20 taxRegion,
21 showSublevelRegions,
22 }: TaxRateListProps) => {
23 const { t } = useTranslation()
24
25 const { searchParams, raw } = useTaxRegionTableQuery({
26 pageSize: PAGE_SIZE,
27 prefix: PREFIX,
28 })
29 const { tax_regions, count, isPending, isError, error } = useTaxRegions(
30 {
31 ...searchParams,
32 parent_id: taxRegion.id,
33 },
34 {
35 placeholderData: keepPreviousData,
36 }
37 )
38
39 const { table } = useTaxRegionTable({
40 count,
41 data: tax_regions,
42 pageSize: PAGE_SIZE,
43 prefix: PREFIX,
44 })
45
46 const provinceObject = getCountryProvinceObjectByIso2(taxRegion.country_code!)
47
48 if (!provinceObject && !showSublevelRegions && !taxRegion.children.length) {
49 return null
50 }
51
52 const type = provinceObject?.type || "sublevel"
53
54 if (isError) {
55 throw error
56 }
57
58 return (
59 <Container className="divide-y p-0">
60 <TaxRegionTable
61 variant="province"
62 action={{ to: `provinces/create`, label: t("actions.create") }}
63 table={table}
64 isPending={isPending}
65 queryObject={raw}
66 count={count}
67 >
68 <Heading level="h2">{t(`taxRegions.${type}.header`)}</Heading>
69 </TaxRegionTable>
70 </Container>
71 )
72 }