1 import { PencilSquare, Trash } from "@medusajs/icons"
2 import { FetchError } from "@medusajs/js-sdk"
3 import { HttpTypes } from "@medusajs/types"
4 import { toast, usePrompt } from "@medusajs/ui"
5 import { useTranslation } from "react-i18next"
6 import { useNavigate } from "react-router-dom"
7
8 import { ActionMenu } from "../../../components/common/action-menu"
9 import { stockLocationsQueryKeys } from "../../../hooks/api/stock-locations"
10 import { sdk } from "../../../lib/client"
11 import { queryClient } from "../../../lib/query-client"
12
13 export const LocationListTableActions = ({
14 location,
15 }: {
16 location: HttpTypes.AdminStockLocation
17 }) => {
18 const { t } = useTranslation()
19 const navigate = useNavigate()
20 const prompt = usePrompt()
21
22 const handleDelete = async () => {
23 const result = await prompt({
24 title: t("general.areYouSure"),
25 description: t("stockLocations.delete.confirmation", {
26 name: location.name,
27 }),
28 confirmText: t("actions.remove"),
29 cancelText: t("actions.cancel"),
30 })
31
32 if (!result) {
33 return
34 }
35
36 try {
37 await sdk.admin.stockLocation.delete(location.id)
38 queryClient.invalidateQueries({
39 queryKey: stockLocationsQueryKeys.lists(),
40 })
41 queryClient.invalidateQueries({
42 queryKey: stockLocationsQueryKeys.detail(location.id),
43 })
44 toast.success(
45 t("stockLocations.delete.successToast", { name: location.name })
46 )
47 } catch (e) {
48 toast.error((e as FetchError).message)
49 }
50 }
51
52 return (
53 <ActionMenu
54 groups={[
55 {
56 actions: [
57 {
58 icon: <PencilSquare />,
59 label: t("actions.edit"),
60 onClick: () =>
61 navigate(`/settings/locations/${location.id}/edit`),
62 },
63 ],
64 },
65 {
66 actions: [
67 {
68 icon: <Trash />,
69 label: t("actions.delete"),
70 onClick: handleDelete,
71 },
72 ],
73 },
74 ]}
75 />
76 )
77 }