mirror of
https://github.com/documenso/documenso.git
synced 2025-11-14 00:32:43 +10:00
56 lines
1.4 KiB
TypeScript
56 lines
1.4 KiB
TypeScript
'use client';
|
|
|
|
import { useState } from 'react';
|
|
|
|
import { Button } from '@documenso/ui/primitives/button';
|
|
import { useToast } from '@documenso/ui/primitives/use-toast';
|
|
|
|
import { createBillingPortal } from './create-billing-portal.action';
|
|
|
|
export const BillingPortalButton = () => {
|
|
const { toast } = useToast();
|
|
|
|
const [isFetchingPortalUrl, setIsFetchingPortalUrl] = useState(false);
|
|
|
|
const handleFetchPortalUrl = async () => {
|
|
if (isFetchingPortalUrl) {
|
|
return;
|
|
}
|
|
|
|
setIsFetchingPortalUrl(true);
|
|
|
|
try {
|
|
const sessionUrl = await createBillingPortal();
|
|
|
|
if (!sessionUrl) {
|
|
throw new Error('NO_SESSION');
|
|
}
|
|
|
|
window.open(sessionUrl, '_blank');
|
|
} catch (e) {
|
|
let description =
|
|
'We are unable to proceed to the billing portal at this time. Please try again, or contact support.';
|
|
|
|
if (e.message === 'CUSTOMER_NOT_FOUND') {
|
|
description =
|
|
'You do not currently have a customer record, this should not happen. Please contact support for assistance.';
|
|
}
|
|
|
|
toast({
|
|
title: 'Something went wrong',
|
|
description,
|
|
variant: 'destructive',
|
|
duration: 10000,
|
|
});
|
|
}
|
|
|
|
setIsFetchingPortalUrl(false);
|
|
};
|
|
|
|
return (
|
|
<Button onClick={async () => handleFetchPortalUrl()} loading={isFetchingPortalUrl}>
|
|
Manage Subscription
|
|
</Button>
|
|
);
|
|
};
|