Todo : - replace all instances of useNavigate( - remove getSettingsPagePath - add eslint rule to enfore usage of useNavigateApp instead of useNavigate
51 lines
1.5 KiB
TypeScript
51 lines
1.5 KiB
TypeScript
import { SettingsPath } from '@/types/SettingsPath';
|
|
import { SnackBarVariant } from '@/ui/feedback/snack-bar-manager/components/SnackBar';
|
|
import { useSnackBar } from '@/ui/feedback/snack-bar-manager/hooks/useSnackBar';
|
|
import { useState } from 'react';
|
|
import {
|
|
BillingPlanKey,
|
|
SubscriptionInterval,
|
|
} from '~/generated-metadata/graphql';
|
|
import { useCheckoutSessionMutation } from '~/generated/graphql';
|
|
import { getSettingsPath } from '~/utils/navigation/getSettingsPath';
|
|
|
|
export const useHandleCheckoutSession = ({
|
|
recurringInterval,
|
|
plan,
|
|
requirePaymentMethod,
|
|
}: {
|
|
recurringInterval: SubscriptionInterval;
|
|
plan: BillingPlanKey;
|
|
requirePaymentMethod: boolean;
|
|
}) => {
|
|
const { enqueueSnackBar } = useSnackBar();
|
|
|
|
const [checkoutSession] = useCheckoutSessionMutation();
|
|
|
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
|
|
const handleCheckoutSession = async () => {
|
|
setIsSubmitting(true);
|
|
const { data } = await checkoutSession({
|
|
variables: {
|
|
recurringInterval,
|
|
successUrlPath: getSettingsPath(SettingsPath.Billing),
|
|
plan,
|
|
requirePaymentMethod,
|
|
},
|
|
});
|
|
setIsSubmitting(false);
|
|
if (!data?.checkoutSession.url) {
|
|
enqueueSnackBar(
|
|
'Checkout session error. Please retry or contact Twenty team',
|
|
{
|
|
variant: SnackBarVariant.Error,
|
|
},
|
|
);
|
|
return;
|
|
}
|
|
window.location.replace(data.checkoutSession.url);
|
|
};
|
|
return { isSubmitting, handleCheckoutSession };
|
|
};
|