1 import { HttpTypes } from "@medusajs/types"
2 import { toast, usePrompt } from "@medusajs/ui"
3 import { useTranslation } from "react-i18next"
4 import { useNavigate } from "react-router-dom"
5 import { useDeleteTaxRate } from "../../../hooks/api/tax-rates"
6 import { useDeleteTaxRegion } from "../../../hooks/api/tax-regions"
7
8 export const useDeleteTaxRegionAction = ({
9 taxRegion,
10 to = "/settings/tax-regions",
11 }: {
12 taxRegion: HttpTypes.AdminTaxRegion
13 to?: string
14 }) => {
15 const { t } = useTranslation()
16 const navigate = useNavigate()
17 const prompt = usePrompt()
18
19 const { mutateAsync } = useDeleteTaxRegion(taxRegion.id)
20
21 const handleDelete = async () => {
22 const res = await prompt({
23 title: t("general.areYouSure"),
24 description: t("taxRegions.delete.confirmation"),
25 confirmText: t("actions.delete"),
26 cancelText: t("actions.cancel"),
27 })
28
29 if (!res) {
30 return
31 }
32
33 await mutateAsync(undefined, {
34 onSuccess: () => {
35 toast.success(t("taxRegions.delete.successToast"))
36
37 navigate(to, { replace: true })
38 },
39 onError: (e) => {
40 toast.error(e.message)
41 },
42 })
43 }
44
45 return handleDelete
46 }
47
48 export const useDeleteTaxRateAction = (taxRate: HttpTypes.AdminTaxRate) => {
49 const { t } = useTranslation()
50 const prompt = usePrompt()
51
52 const { mutateAsync } = useDeleteTaxRate(taxRate.id)
53
54 const handleDelete = async () => {
55 const res = await prompt({
56 title: t("general.areYouSure"),
57 description: t("taxRegions.taxRates.delete.confirmation", {
58 name: taxRate.name,
59 }),
60 confirmText: t("actions.delete"),
61 cancelText: t("actions.cancel"),
62 })
63
64 if (!res) {
65 return
66 }
67
68 await mutateAsync(undefined, {
69 onSuccess: () => {
70 toast.success(t("taxRegions.taxRates.delete.successToast"))
71 },
72 onError: (e) => {
73 toast.error(e.message)
74 },
75 })
76 }
77
78 return handleDelete
79 }