1 import { PencilSquare, Trash } from "@medusajs/icons"
2 import { Badge, Container, Heading, Text, toast, usePrompt } from "@medusajs/ui"
3 import { useTranslation } from "react-i18next"
4
5 import { HttpTypes } from "@medusajs/types"
6 import { useNavigate } from "react-router-dom"
7 import { ActionMenu } from "../../../../../components/common/action-menu/index.ts"
8 import { ListSummary } from "../../../../../components/common/list-summary/index.ts"
9 import { useDeleteRegion } from "../../../../../hooks/api/regions.tsx"
10 import { currencies } from "../../../../../lib/data/currencies.ts"
11 import { formatProvider } from "../../../../../lib/format-provider.ts"
12 import { SectionRow } from "../../../../../components/common/section/section-row.tsx"
13
14 type RegionGeneralSectionProps = {
15 region: HttpTypes.AdminRegion
16 pricePreferences: HttpTypes.AdminPricePreference[]
17 }
18
19 export const RegionGeneralSection = ({
20 region,
21 pricePreferences,
22 }: RegionGeneralSectionProps) => {
23 const { t } = useTranslation()
24 const pricePreferenceForRegion = pricePreferences?.find(
25 (preference) =>
26 preference.attribute === "region_id" && preference.value === region.id
27 )
28
29 return (
30 <Container className="divide-y p-0">
31 <div className="flex items-center justify-between px-6 py-4">
32 <Heading>{region.name}</Heading>
33 <RegionActions region={region} />
34 </div>
35 <SectionRow
36 title={t("fields.currency")}
37 value={
38 <div className="flex items-center gap-x-2">
39 <Badge size="2xsmall" className="uppercase">
40 {region.currency_code}
41 </Badge>
42 <Text size="small" leading="compact">
43 {currencies[region.currency_code.toUpperCase()]?.name}
44 </Text>
45 </div>
46 }
47 />
48
49 <SectionRow
50 title={t("fields.automaticTaxes")}
51 value={region.automatic_taxes ? t("fields.true") : t("fields.false")}
52 />
53
54 <SectionRow
55 title={t("fields.taxInclusivePricing")}
56 value={
57 pricePreferenceForRegion?.is_tax_inclusive
58 ? t("fields.true")
59 : t("fields.false")
60 }
61 />
62
63 <SectionRow
64 title={t("fields.paymentProviders")}
65 value={
66 <div className="inline-flex">
67 {region.payment_providers?.length ? (
68 <ListSummary
69 list={region.payment_providers.map((p) => formatProvider(p.id))}
70 />
71 ) : (
72 "-"
73 )}
74 </div>
75 }
76 />
77 </Container>
78 )
79 }
80
81 const RegionActions = ({ region }: { region: HttpTypes.AdminRegion }) => {
82 const navigate = useNavigate()
83 const { t } = useTranslation()
84 const { mutateAsync } = useDeleteRegion(region.id)
85 const prompt = usePrompt()
86
87 const handleDelete = async () => {
88 const res = await prompt({
89 title: t("general.areYouSure"),
90 description: t("regions.deleteRegionWarning", {
91 name: region.name,
92 }),
93 verificationText: region.name,
94 verificationInstruction: t("general.typeToConfirm"),
95 confirmText: t("actions.delete"),
96 cancelText: t("actions.cancel"),
97 })
98
99 if (!res) {
100 return
101 }
102
103 await mutateAsync(undefined, {
104 onSuccess: () => {
105 toast.success(t("regions.toast.delete"))
106 navigate("/settings/regions", { replace: true })
107 },
108 onError: (e) => {
109 toast.error(e.message)
110 },
111 })
112 }
113
114 return (
115 <ActionMenu
116 groups={[
117 {
118 actions: [
119 {
120 icon: <PencilSquare />,
121 label: t("actions.edit"),
122 to: `/settings/regions/${region.id}/edit`,
123 },
124 ],
125 },
126 {
127 actions: [
128 {
129 icon: <Trash />,
130 label: t("actions.delete"),
131 onClick: handleDelete,
132 },
133 ],
134 },
135 ]}
136 />
137 )
138 }