import type { SessionContextValue } from "next-auth/react"; import { useSession } from "next-auth/react"; import { useState } from "react"; import { Controller, useForm } from "react-hook-form"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc"; import type { Ensure } from "@calcom/types/utils"; import { showToast } from "@calcom/ui"; import { Alert, Button, Form, Label, TextField, ToggleGroup } from "@calcom/ui"; import { UserPermissionRole } from "../../../../prisma/enums"; export const CreateANewLicenseKeyForm = () => { const session = useSession(); if (session.data?.user.role !== "ADMIN") { return null; } // @ts-expect-error session can't be null due to the early return return ; }; enum BillingType { PER_BOOKING = "PER_BOOKING", PER_USER = "PER_USER", } enum BillingPeriod { MONTHLY = "MONTHLY", ANNUALLY = "ANNUALLY", } interface FormValues { billingType: BillingType; entityCount: number; entityPrice: number; billingPeriod: BillingPeriod; overages: number; billingEmail: string; } const CreateANewLicenseKeyFormChild = ({ session }: { session: Ensure }) => { const { t } = useLocale(); const [serverErrorMessage, setServerErrorMessage] = useState(null); const [stripeCheckoutUrl, setStripeCheckoutUrl] = useState(null); const isAdmin = session.data.user.role === UserPermissionRole.ADMIN; const newLicenseKeyFormMethods = useForm({ defaultValues: { billingType: BillingType.PER_BOOKING, billingPeriod: BillingPeriod.MONTHLY, entityCount: 500, overages: 99, // $0.99 entityPrice: 50, // $0.5 billingEmail: undefined, }, }); const mutation = trpc.viewer.admin.createSelfHostedLicense.useMutation({ onSuccess: async (values) => { showToast(`Success: We have created a stripe payment URL for this billing email`, "success"); setStripeCheckoutUrl(values.stripeCheckoutUrl); }, onError: async (err) => { setServerErrorMessage(err.message); }, }); const watchedBillingPeriod = newLicenseKeyFormMethods.watch("billingPeriod"); const watchedEntityCount = newLicenseKeyFormMethods.watch("entityCount"); const watchedEntityPrice = newLicenseKeyFormMethods.watch("entityPrice"); function calculateMonthlyPrice() { const occurrence = watchedBillingPeriod === "MONTHLY" ? 1 : 12; const sum = watchedEntityCount * watchedEntityPrice * occurrence; return `$ ${sum / 100} / ${occurrence} months`; } return ( <> {!stripeCheckoutUrl ? (
{ mutation.mutate(values); }}>
{serverErrorMessage && (
)}
( <> onChange(e)} options={[ { value: "MONTHLY", label: "Monthly", }, { value: "ANNUALLY", label: "Annually", }, ]} /> )} />
(
)} />
( <> onChange(e)} options={[ { value: "PER_BOOKING", label: "Per Booking", tooltip: "Configure pricing on a per booking basis", }, { value: "PER_USER", label: "Per User", tooltip: "Configure pricing on a per user basis", }, ]} /> )} />
( onChange(+event.target.value)} /> )} /> ( onChange(+event.target.value * 100)} /> )} />
( <> onChange(+event.target.value * 100)} autoComplete="off" /> )} />
) : (
)} ); };