import clsx from "clsx" import { useAdminRegion, useAdminTaxRates } from "medusa-react" import { useEffect, useState } from "react" import { useTable } from "react-table" import { useTranslation } from "react-i18next" import Spinner from "../../../components/atoms/spinner" import PlusIcon from "../../../components/fundamentals/icons/plus-icon" import Table from "../../../components/molecules/table" import BodyCard from "../../../components/organisms/body-card" import { PaginationProps, TaxRateType } from "../../../types/shared" import EditTaxRate from "./edit" import NewTaxRate from "./new" import { RegionTaxForm } from "./region-form" import { TaxRateRow } from "./tax-rate-row" import useTaxRateColumns from "./use-tax-rate-columns" export type TaxRateTableEntries = { id: string name?: string rate: number | null code: string | null type: TaxRateType } const DEFAULT_PAGESIZE = 10 const TaxDetails = ({ id }) => { const { t } = useTranslation() const [pagination, setPagination] = useState({ limit: DEFAULT_PAGESIZE, offset: 0, }) const [showNew, setShowNew] = useState(false) const [editRate, setEditRate] = useState(null) const [tableEntries, setTableEntries] = useState([]) const { tax_rates, isLoading: taxRatesLoading } = useAdminTaxRates( { region_id: id, ...pagination, }, { enabled: !!id, } ) const { region, isLoading: regionIsLoading } = useAdminRegion(id, { enabled: !!id, }) useEffect(() => { if (!taxRatesLoading && !regionIsLoading && region && tax_rates) { const regionDefaultRate = { id: region.id, name: "Default", code: region.tax_code ?? null, rate: region.tax_rate ?? null, type: TaxRateType.REGION, } setTableEntries([ regionDefaultRate, ...tax_rates.map((tr) => { return { id: tr.id, name: tr.name, code: tr.code, rate: tr.rate, type: TaxRateType.RATE, } }), ]) } }, [taxRatesLoading, regionIsLoading, region, tax_rates]) const [columns] = useTaxRateColumns() const { getTableProps, getTableBodyProps, headerGroups, rows, prepareRow } = useTable({ columns, data: tableEntries || [], manualPagination: true, autoResetPage: false, }) if (!id) { return null } return ( <> setShowNew(true), icon: , }, ]} > {headerGroups?.map((headerGroup) => ( {headerGroup.headers.map((col) => ( {col.render("Header")} ))} ))} {regionIsLoading || taxRatesLoading ? (
) : ( {rows.map((row) => { prepareRow(row) return ( ) })} )}

{t("taxes-tax-calculation-settings", "Tax Calculation Settings")}

{!regionIsLoading && region && }
{showNew && ( setShowNew(false)} /> )} {editRate && ( setEditRate(null)} /> )} ) } export default TaxDetails