import { Price, Product, SourceModal } from "@/types"; import { createCheckoutSession, isAuthenticated } from "@/app/(auth)/actions"; import { toast } from "react-toastify"; import { useRouter } from "next/navigation"; import Icon from "./Icon"; export default function PriceCard({ product, isAnnual, loading, }: { product?: Product; isAnnual?: boolean; loading?: boolean; }) { const router = useRouter(); const openSignUpModalOnPriceClick = (price: Price, type: string) => { const signUpModal = document.getElementById("sign-up-modal"); if (!signUpModal) return; signUpModal.setAttribute("price_id", price.price_id); signUpModal.setAttribute("type", type); signUpModal.setAttribute("name", SourceModal.SignUpViaPurchase); signUpModal.click(); }; const generateCheckoutPage = async (price: Price, type: string) => { try { const checkoutSessionResponse = await createCheckoutSession( price.price_id, type ); router.push(checkoutSessionResponse.url); } catch (error) { if (error instanceof Error) { toast.error(error.message, { position: "bottom-left", autoClose: 5000, hideProgressBar: false, closeOnClick: true, pauseOnHover: true, draggable: true, progress: undefined, theme: "colored", }); } } }; const submitForm = async (product: Product) => { const userIsAuthenticated = await isAuthenticated(); const price = isAnnual ? product.yearlyPrice : product.monthlyPrice; if (userIsAuthenticated) { await generateCheckoutPage(price, product.type); } else { localStorage.setItem("price", JSON.stringify(price)); openSignUpModalOnPriceClick(price, product.type); } }; return (
{false && (

Popular

)}

{product?.name}

{product?.description}

    {product?.metadata?.benefits?.map((x, i) => (
  • {x}

  • ))}
{!loading && (

$ {isAnnual ? (product?.yearlyPrice?.unit_amount ?? 0) / 100 : (product?.monthlyPrice?.unit_amount ?? 0) / 100}

per user per {isAnnual ? "year" : "month"}

)} {product && ( )}
); }