Files
sorsfonalak/services/frontend/src/components/PackageCards.tsx
T
fecoandClaude Opus 5.5 182d71fe65
CI / test (push) Successful in 1m5s
CI / build (push) Successful in 4m56s
CI / deploy (push) Skipped
Questions about an analysis or forecast, answered by the model that wrote it
- 3 questions in every package, +5 questions add-on (1 000 Ft); question units, refund on failure
- The answer sees the chart data, the analysis and earlier questions; at most 250 words
- Editable question prompt (Promptok → Kérdés-válasz); admins regenerate answers
- Package texts and FAQ updated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
2026-11-18 18:44:20 +00:00

171 lines
5.7 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"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[] {
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.forecast) lines.push(`${p.forecast} éves előrejelzés`);
if (p.persons) lines.push(`${p.persons} személy felvétele`);
if (p.questions) lines.push(`${p.questions} kérdés az elemzéseidről`);
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> | void;
}) {
const router = useRouter();
const [busy, setBusy] = useState<string | null>(null);
const [error, setError] = useState<string | null>(null);
const [done, setDone] = useState<string | null>(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 (
<div className="field" style={{ gap: 12 }}>
<div className={compact ? "pkg-grid pkg-grid-compact" : "pkg-grid"}>
{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 (
<article key={p.id} className={p.badge ? "pkg pkg-featured" : "pkg"} aria-labelledby={`pkg-${p.id}`}>
<div className="pkg-tags">
{p.badge && <span className="pkg-badge">{p.badge}</span>}
{p.savings_percent ? <span className="pkg-save">−{p.savings_percent}%</span> : null}
</div>
<h3 id={`pkg-${p.id}`} className="pkg-name">
{p.name}
</h3>
{!compact && <p className="muted pkg-tagline">{p.tagline}</p>}
<div className="pkg-price">
<strong>{huf(p.price_huf)}</strong>
{p.list_price_huf > p.price_huf && (
<s className="hint" aria-label={`Külön megvéve ${huf(p.list_price_huf)}`}>
{huf(p.list_price_huf)}
</s>
)}
</div>
<ul className="pkg-list">
{contents(p).map((line) => (
<li key={line}>{line}</li>
))}
</ul>
<p className="hint pkg-per">
{p.per_analysis_huf ? `Elemzésenként ${huf(p.per_analysis_huf)}` : " "}
</p>
{step !== null && step > 0 && prev && (
<p className="pkg-step">
Csak +{huf(step)} a(z) {prev.name} csomaghoz képest
</p>
)}
{canPay ? (
<button
type="button"
className={p.badge ? "btn btn-primary" : "btn"}
onClick={() => buy(p)}
disabled={busy !== null}
>
{busy === p.id ? "Fizetés…" : "Megveszem"}
</button>
) : (
<button type="button" className="btn" disabled>
Hamarosan
</button>
)}
</article>
);
})}
</div>
{error && (
<p className="msg msg-error" role="alert">
{error}
</p>
)}
{done && (
<p className="msg msg-ok" role="status">
A(z) {done} csomag jóváírva.
</p>
)}
{canPay && <p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>}
</div>
);
}
/** Loads the catalog in the browser (for choosers inside other components). */
export function usePackages(): Package[] | null {
const [packages, setPackages] = useState<Package[] | null>(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> | void;
}) {
const packages = usePackages();
if (packages === null) return <p className="muted">Csomagok betöltése…</p>;
return (
<div className="field" style={{ gap: 8 }}>
<PackageCards packages={packages} ids={ids} canPay={canPay} compact onBought={onBought} />
<Link href="/csomagok" className="hint">
Összes csomag és a részletek →
</Link>
</div>
);
}