A fixed catalog (packages.rs, reasoning in docs/csomagok.md): Egy elemzés
2 990 Ft, Páros 6 990 Ft, Család 11 990 Ft (featured), Korlátlan 17 990 Ft
for a year, and two top-ups. Every grant is a package_orders row; units are
credit_ledger rows with reason 'package' and never expire. The unlimited
package is a time-limited 'unlimited' entitlement: analyses use no unit and
persons have no limit, with a daily fair-use cap (UNLIMITED_DAILY_LIMIT, 5);
buying it again extends it by a year.
- GET /v1/packages, mock payment by package id, /v1/me unlimited_until
- POST /v1/admin/users/{id}/packages: give a package as a gift
- /csomagok page; the paid step of an analysis offers three packages with
the bigger one featured; person packs use the catalog; admin user list
has a package column
- PERSON_PACK_SIZE is replaced by the catalog's +5 személy top-up
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
181 lines
5.9 KiB
TypeScript
181 lines
5.9 KiB
TypeScript
"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> | 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.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.unlimited_months
|
||
? "Aki sokakat vesz fel: családot, barátokat."
|
||
: 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>
|
||
);
|
||
}
|