50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
"use client"
|
|
|
|
import { useState } from "react"
|
|
import { deleteLineItem } from "@lib/data/cart"
|
|
import { notifyCartUpdated } from "@lib/util/cart-events"
|
|
import { cn } from "@/lib/utils"
|
|
import { Trash2, Loader2 } from "lucide-react"
|
|
|
|
const DeleteButton = ({
|
|
id,
|
|
children,
|
|
className,
|
|
}: {
|
|
id: string
|
|
children?: React.ReactNode
|
|
className?: string
|
|
}) => {
|
|
const [isDeleting, setIsDeleting] = useState(false)
|
|
|
|
const handleDelete = async (id: string) => {
|
|
setIsDeleting(true)
|
|
await deleteLineItem(id)
|
|
.then(notifyCartUpdated)
|
|
.catch((_err) => {
|
|
setIsDeleting(false)
|
|
})
|
|
}
|
|
|
|
return (
|
|
<div className={cn("flex items-center", className)}>
|
|
<button
|
|
type="button"
|
|
className="inline-flex items-center gap-1.5 text-xs text-muted-foreground hover:text-destructive transition-colors disabled:opacity-50"
|
|
onClick={() => handleDelete(id)}
|
|
disabled={isDeleting}
|
|
aria-label="Ukloni artikal"
|
|
>
|
|
{isDeleting ? (
|
|
<Loader2 className="h-3.5 w-3.5 animate-spin" />
|
|
) : (
|
|
<Trash2 className="h-3.5 w-3.5" />
|
|
)}
|
|
{children && <span>{children}</span>}
|
|
</button>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
export default DeleteButton
|