"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { useEffect, useState } from "react"; import { api, errorText } from "@/lib/api"; import type { Package } from "@/lib/types"; export const huf = (n: number) => `${n.toLocaleString("hu-HU")} Ft`; /** What a package contains, as short lines. */ export function contents(p: Package): string[] { if (p.unlimited_months) { return [ "Korlátlan képletelemzés", "Korlátlan kapcsolati elemzés", "Korlátlan számú személy", `${p.unlimited_months} hónapig`, ]; } const lines: string[] = []; if (p.natal) lines.push(`${p.natal} részletes képletelemzés`); if (p.synastry) lines.push(`${p.synastry} kapcsolati elemzés`); if (p.persons) lines.push(`${p.persons} személy felvétele`); return lines; } /** * Package cards with a buy button. Bigger packages show what they save and * how little more they cost than the previous one. Until Stripe is connected * the payment is a stand-in (MOCK_PAYMENTS) that grants the package at once. */ export function PackageCards({ packages, ids, canPay, compact = false, onBought, }: { packages: Package[]; /** Which packages, in this order (default: the main ladder). */ ids?: string[]; canPay: boolean; compact?: boolean; /** After a purchase (default: reload the page data). */ onBought?: (p: Package) => Promise | void; }) { const router = useRouter(); const [busy, setBusy] = useState(null); const [error, setError] = useState(null); const [done, setDone] = useState(null); const shown = ids ? ids.map((id) => packages.find((p) => p.id === id)).filter((p): p is Package => p !== undefined) : packages.filter((p) => p.tier === "main"); async function buy(p: Package) { setBusy(p.id); setError(null); setDone(null); try { await api("/payments/mock", { body: { package: p.id } }); if (onBought) await onBought(p); else { setDone(p.name); router.refresh(); } } catch (e) { setError(errorText(e)); } finally { setBusy(null); } } return (
{shown.map((p, i) => { const prev = i > 0 ? shown[i - 1] : null; const step = prev && p.tier === "main" && prev.tier === "main" ? p.price_huf - prev.price_huf : null; return (
{p.badge && {p.badge}} {p.savings_percent ? −{p.savings_percent}% : null}

{p.name}

{!compact &&

{p.tagline}

}
{huf(p.price_huf)} {p.list_price_huf && p.list_price_huf > p.price_huf && ( {huf(p.list_price_huf)} )}
    {contents(p).map((line) => (
  • {line}
  • ))}

{p.unlimited_months ? "Aki sokakat vesz fel: családot, barátokat." : p.per_analysis_huf ? `Elemzésenként ${huf(p.per_analysis_huf)}` : " "}

{step !== null && step > 0 && prev && (

Csak +{huf(step)} a(z) {prev.name} csomaghoz képest

)} {canPay ? ( ) : ( )}
); })}
{error && (

{error}

)} {done && (

A(z) {done} csomag jóváírva.

)} {canPay &&

Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.

}
); } /** Loads the catalog in the browser (for choosers inside other components). */ export function usePackages(): Package[] | null { const [packages, setPackages] = useState(null); useEffect(() => { api<{ packages: Package[] }>("/packages") .then((d) => setPackages(d.packages)) .catch(() => setPackages([])); }, []); return packages; } /** A few packages to choose from, loaded on demand, with a link to all of them. */ export function PackageChooser({ ids, canPay, onBought, }: { ids: string[]; canPay: boolean; onBought?: (p: Package) => Promise | void; }) { const packages = usePackages(); if (packages === null) return

Csomagok betöltése…

; return (
Összes csomag és a részletek →
); }