Files
sorsfonalak/services/frontend/src/components/PackageCards.tsx
T
fecoandClaude Opus 5.5 b0fc547b73
CI / test (push) Successful in 1m29s
CI / build (push) Successful in 5m52s
CI / deploy (push) Skipped
Tarot readings: AI-written, drawn on the server, sold as units
A new Kártyajóslás page: choose a spread (1, 3 or 5 cards), ask a
question, pick the cards from the fanned deck and watch them turn over;
the reading is written by the detailed-analysis model from the cards,
their positions and base meanings, the question and the reader's Sun,
Moon and Ascendant. The server shuffles and draws, so nobody can draw
again for free. A reading is a 'tarot' analysis (worker, retries,
fallback, refunds and admin regeneration as for analyses), uses one
'tarot' unit, and survives recalculations; deleting the account
deletes it.

Packages carry 2/5/10 readings, the new +5 kártyajóslás addon costs
1 000 Ft. The card artwork comes from the tarot design studio
(celestial astrolabe style); the deck JSON is shared by the frontend
and the prompt. A public example is at /kartyajoslas/pelda.

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

218 lines
7.8 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, PayMethod, PublicConfig } 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`);
if (p.tarot) lines.push(`${p.tarot} kártyajóslás`);
return lines;
}
/**
* Package cards with a buy button. Bigger packages show what they save and
* how little more they cost than the previous one. With Stripe the button
* goes to Stripe's payment page and comes back to this page (PaymentReturn
* shows the result); the stand-in payment (MOCK_PAYMENTS) grants at once.
*/
export function PackageCards({
packages,
ids,
canPay,
compact = false,
onBought,
}: {
packages: Package[];
/** Which packages, in this order (default: the main ladder). */
ids?: string[];
/** How the user can pay (null: purchases are off, the buttons say "soon"). */
canPay: PayMethod | null;
compact?: boolean;
/** After a stand-in 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);
// Back from Stripe's page (the browser may restore this page as it was).
useEffect(() => {
const reset = (e: PageTransitionEvent) => {
if (e.persisted) setBusy(null);
};
window.addEventListener("pageshow", reset);
return () => window.removeEventListener("pageshow", reset);
}, []);
// Whether Stripe's page asks for the ÁSZF and the withdrawal waiver
// (TERMS_CONSENT, off until the opening).
const stripe = canPay === "stripe" || canPay === "stripe_test";
const [termsConsent, setTermsConsent] = useState(false);
useEffect(() => {
if (!stripe) return;
api<PublicConfig>("/config")
.then((c) => setTermsConsent(c.terms_consent === true))
.catch(() => {});
}, [stripe]);
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 {
if (canPay === "stripe" || canPay === "stripe_test") {
const { url } = await api<{ url: string }>("/payments/checkout", {
body: { package: p.id, return_path: window.location.pathname },
});
window.location.assign(url);
return;
}
await api("/payments/mock", { body: { package: p.id } });
if (onBought) await onBought(p);
else {
setDone(p.name);
router.refresh();
}
setBusy(null);
} catch (e) {
setError(errorText(e));
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 ? (canPay === "mock" ? "Fizetés…" : "Átirányítá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 === "mock" && <p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>}
{stripe && (
<p className="hint">
<span aria-hidden="true">🔒</span> A fizetés a Stripe biztonságos oldalán történik (bankkártya, Apple Pay,
Google Pay). A kártyaadataidat közvetlenül a Stripe-nak adod meg: mi nem látjuk és nem tároljuk őket.
{termsConsent && (
<>
{" "}
A csomagot azonnal megkapod, ezért a fizetésnél kéred a teljesítés megkezdését, és ezzel lemondasz a 14
napos elállási jogról. <Link href="/aszf">ÁSZF</Link>
</>
)}
</p>
)}
{canPay === "stripe_test" && (
<p className="hint">
Stripe tesztmód (csak adminoknak látszik): kártyaszám 4242 4242 4242 4242, bármilyen jövőbeli lejárat és CVC.
Valódi pénz nem mozdul.
</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: PayMethod | null;
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>
);
}