Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

1.0 KB
Raw Blame
"use client"

import { Button } from '@/components/atoms/Button/Button';
import { BinIcon } from "@/icons"
import { useCartContext } from "@/components/providers"
import { toast } from "@/lib/helpers/toast"

export const DeleteCartItemButton = ({
  id,
  disabled,
}: {
  id: string
  disabled?: boolean
}) => {
  const { removeCartItem } = useCartContext()

  const handleDelete = async (id: string) => {
    try {
      await removeCartItem(id)
    } catch (error) {
      console.error("Error deleting cart item:", error)
      toast.error({
        title: "Failed to remove item from cart",
      })
    }
  }

  const isBtnDisabled = disabled || !id || id.startsWith("optimistic:")

  return (
    <Button
      variant="text"
      className="w-10 h-10 flex items-center justify-center p-0 disabled:bg-transparent disabled:opacity-40"
      onClick={() => handleDelete(id)}
      disabled={isBtnDisabled}
      aria-label="Remove item from cart"
      data-testid="cart-item-remove"
    >
      <BinIcon size={20} />
    </Button>
  )
}