Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

1.1 KB
Raw Blame
"use client";

import useWorkspace from "@/lib/swr/use-workspace";
import { Button, ButtonProps } from "@dub/ui";
import { cn } from "@dub/utils";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { toast } from "sonner";

export default function ManageSubscriptionButton(props: ButtonProps) {
  const { id: workspaceId } = useWorkspace();
  const [clicked, setClicked] = useState(false);
  const router = useRouter();

  return (
    <Button
      {...props}
      text={props.text || "Manage Subscription"}
      variant={props.variant || "secondary"}
      className={cn("h-9", props.className)}
      onClick={() => {
        setClicked(true);
        fetch(`/api/workspaces/${workspaceId}/billing/manage`, {
          method: "POST",
        }).then(async (res) => {
          if (res.ok) {
            const { url } = await res.json();
            router.push(url);
          } else {
            const { error } = await res.json();
            toast.error(error.message);
            setClicked(false);
          }
        });
      }}
      loading={clicked}
    />
  );
}