DeleteItemButton.tsx 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import LoadingDots from "@components/common/icons/LoadingDots";
  2. import { TrashIcon } from "@heroicons/react/24/outline";
  3. import { useAddProduct } from "@utils/hooks/useAddToCart";
  4. import clsx from "clsx";
  5. function SubmitButton({
  6. loading,
  7. handleRemoveCart,
  8. }: {
  9. loading: boolean;
  10. handleRemoveCart: () => void;
  11. }) {
  12. return (
  13. <button
  14. aria-disabled={loading}
  15. aria-label="Remove cart item"
  16. className={clsx(
  17. "ease flex h-[17px] w-[17px] cursor-pointer items-center justify-center rounded-full transition-all duration-200",
  18. {
  19. "cursor-wait px-0": loading,
  20. }
  21. )}
  22. type="button"
  23. onClick={handleRemoveCart}
  24. >
  25. {loading ? (
  26. <LoadingDots className="bg-black dark:bg-white" />
  27. ) : (
  28. <TrashIcon className="hover:text-accent-3 mx-[1px] h-6 w-6" />
  29. )}
  30. </button>
  31. );
  32. }
  33. interface CartItemEdge {
  34. node: {
  35. id: string;
  36. quantity: number;
  37. name: string;
  38. price: number;
  39. };
  40. }
  41. export function DeleteItemButton({ item }: { item: CartItemEdge }) {
  42. const { deleteProductFromCart, isRemoveLoading } = useAddProduct();
  43. const itemId = item?.node?.id;
  44. const handleRemoveCart = () => {
  45. deleteProductFromCart(Number(itemId));
  46. };
  47. return (
  48. <SubmitButton
  49. handleRemoveCart={handleRemoveCart}
  50. loading={isRemoveLoading}
  51. />
  52. );
  53. }