66 lines
2.0 KiB
TypeScript
66 lines
2.0 KiB
TypeScript
"use client"
|
|
|
|
import { useAuthApi } from "@/shared/useApi"
|
|
import { useRouter } from "next/navigation"
|
|
import { Button } from "@/shared/components/ui/button"
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from "@/shared/components/ui/dropdown-menu"
|
|
import { Ellipsis, Pencil, Trash2 } from "lucide-react"
|
|
import { confirm } from "@/shared/components/confirm-dialog"
|
|
import { toast } from "sonner"
|
|
|
|
type CustomerActionsProps = {
|
|
customerId: string
|
|
}
|
|
|
|
export function CustomerActions({ customerId }: CustomerActionsProps) {
|
|
const api = useAuthApi()
|
|
const router = useRouter()
|
|
|
|
const handleEdit = () => {
|
|
router.push(`/sales/customers/${customerId}/edit`)
|
|
}
|
|
|
|
const handleDelete = async () => {
|
|
const confirmed = await confirm({
|
|
title: "Delete Customer",
|
|
description: "Are you sure you want to delete this customer? This action cannot be undone.",
|
|
confirmLabel: "Delete",
|
|
variant: "destructive",
|
|
})
|
|
if (!confirmed) return
|
|
|
|
try {
|
|
await api.customers.destroy(customerId)
|
|
toast.success("Customer deleted successfully.")
|
|
router.push("/sales/customers")
|
|
} catch {
|
|
toast.error("Failed to delete customer.")
|
|
}
|
|
}
|
|
|
|
return (
|
|
<DropdownMenu>
|
|
<DropdownMenuTrigger asChild>
|
|
<Button variant="ghost" size="icon">
|
|
<Ellipsis className="size-4" />
|
|
</Button>
|
|
</DropdownMenuTrigger>
|
|
<DropdownMenuContent align="end">
|
|
<DropdownMenuItem onClick={handleEdit}>
|
|
<Pencil className="size-4" />
|
|
Edit
|
|
</DropdownMenuItem>
|
|
<DropdownMenuItem variant="destructive" onClick={handleDelete}>
|
|
<Trash2 className="size-4" />
|
|
Delete
|
|
</DropdownMenuItem>
|
|
</DropdownMenuContent>
|
|
</DropdownMenu>
|
|
)
|
|
}
|