1 import { Heading } from "@medusajs/ui"
2 import { useTranslation } from "react-i18next"
3 import { useParams } from "react-router-dom"
4
5 import { RouteDrawer } from "../../../components/modals"
6 import { useRegion } from "../../../hooks/api/regions"
7 import { useStore } from "../../../hooks/api/store"
8 import { currencies } from "../../../lib/data/currencies"
9 import { EditRegionForm } from "./components/edit-region-form"
10 import { usePricePreferences } from "../../../hooks/api/price-preferences"
11
12 export const RegionEdit = () => {
13 const { t } = useTranslation()
14 const { id } = useParams()
15
16 const {
17 region,
18 isPending: isRegionLoading,
19 isError: isRegionError,
20 error: regionError,
21 } = useRegion(id!, {
22 fields: "*payment_providers,*countries,+automatic_taxes",
23 })
24
25 const {
26 store,
27 isPending: isStoreLoading,
28 isError: isStoreError,
29 error: storeError,
30 } = useStore()
31
32 const {
33 price_preferences: pricePreferences = [],
34 isPending: isPreferenceLoading,
35 isError: isPreferenceError,
36 error: preferenceError,
37 } = usePricePreferences(
38 {
39 attribute: "region_id",
40 value: id,
41 },
42 { enabled: !!region }
43 )
44
45 const isLoading = isRegionLoading || isStoreLoading || isPreferenceLoading
46
47 const storeCurrencies = (store?.supported_currencies ?? [])
48 .map((c) => currencies[c.currency_code.toUpperCase()])
49 .filter(Boolean)
50
51 if (isRegionError) {
52 throw regionError
53 }
54
55 if (isStoreError) {
56 throw storeError
57 }
58
59 if (isPreferenceError) {
60 throw preferenceError
61 }
62
63 return (
64 <RouteDrawer>
65 <RouteDrawer.Header>
66 <Heading>{t("regions.editRegion")}</Heading>
67 </RouteDrawer.Header>
68 {!isLoading && region && (
69 <EditRegionForm
70 region={region}
71 currencies={storeCurrencies}
72 pricePreferences={pricePreferences}
73 />
74 )}
75 </RouteDrawer>
76 )
77 }