-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstripe-button.tsx
59 lines (50 loc) · 1.36 KB
/
stripe-button.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"use client";
import { createStripeSession } from "@/actions/create-stripe-session";
import { Button } from "@/components/ui/button";
import { useAction } from "@/hooks/use-action";
import { Loader2 } from "lucide-react";
import { toast } from "sonner";
import { StripeSession } from "@/actions/create-stripe-session/schema";
import { cn } from "@/lib/utils";
interface StripeButtonProps {
customerId?: string;
priceId?: string;
flow_data?: string;
label: string;
};
export const StripeButton = ({
customerId,
priceId,
flow_data,
label,
}: StripeButtonProps) => {
const { execute, isLoading } = useAction(createStripeSession, {
onSuccess: (data) => {
window.location.href = data;
},
onError: (error) => {
toast.error(error);
}
});
const onClick = () => {
execute({ customerId, priceId, flow_data, label });
}
return (
<Button
onClick={onClick}
disabled={isLoading}
className="relative"
>
<span
className={cn(
isLoading ? "opacity-0" : "opacity-100",
)}
>
{label}
</span>
{isLoading
&& <Loader2 className="absolute mx-auto h-4 w-4 animate-spin" />
}
</Button>
)
}