65 lines
2.1 KiB
TypeScript
65 lines
2.1 KiB
TypeScript
"use client";
|
|||
|
|
|
||
|
|
import { useRouter } from "next/navigation";
|
||
|
|
import { useState } from "react";
|
||
|
|
|
||
|
|
import { api, errorText } from "@/lib/api";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Person packages: a few more persons, or any number. Until Stripe is
|
||
|
|
* connected the payment is a stand-in (MOCK_PAYMENTS) that grants it at once.
|
||
|
|
*/
|
||
|
|
export function PersonPacks({ packSize, canPay }: { packSize: number; canPay: boolean }) {
|
||
|
|
const router = useRouter();
|
||
|
|
const [busy, setBusy] = useState<string | null>(null);
|
||
|
|
const [error, setError] = useState<string | null>(null);
|
||
|
|
|
||
|
|
async function buy(pkg: "persons" | "persons_unlimited") {
|
||
|
|
setBusy(pkg);
|
||
|
|
setError(null);
|
||
|
|
try {
|
||
|
|
await api("/payments/mock", { body: { package: pkg } });
|
||
|
|
router.refresh();
|
||
|
|
} catch (e) {
|
||
|
|
setError(errorText(e));
|
||
|
|
} finally {
|
||
|
|
setBusy(null);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!canPay) {
|
||
|
|
return <p className="hint">További személyekhez csomag kell; a csomagok hamarosan elérhetők.</p>;
|
||
|
|
}
|
||
|
|
return (
|
||
|
|
<div className="field" style={{ gap: 12 }}>
|
||
|
|
<div className="packs">
|
||
|
|
<div className="pack">
|
||
|
|
<strong>+{packSize} személy</strong>
|
||
|
|
<span className="muted">Még {packSize} ember képlete és elemzése.</span>
|
||
|
|
<button type="button" className="btn btn-primary btn-sm" onClick={() => buy("persons")} disabled={busy !== null}>
|
||
|
|
{busy === "persons" ? "Fizetés…" : "Fizetés"}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
<div className="pack">
|
||
|
|
<strong>Korlátlan</strong>
|
||
|
|
<span className="muted">Bárkit felvehetsz, nincs felső határ.</span>
|
||
|
|
<button
|
||
|
|
type="button"
|
||
|
|
className="btn btn-primary btn-sm"
|
||
|
|
onClick={() => buy("persons_unlimited")}
|
||
|
|
disabled={busy !== null}
|
||
|
|
>
|
||
|
|
{busy === "persons_unlimited" ? "Fizetés…" : "Fizetés"}
|
||
|
|
</button>
|
||
|
|
</div>
|
||
|
|
</div>
|
||
|
|
{error && (
|
||
|
|
<p className="msg msg-error" role="alert">
|
||
|
|
{error}
|
||
|
|
</p>
|
||
|
|
)}
|
||
|
|
<p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>
|
||
|
|
</div>
|
||
|
|
);
|
||
|
|
}
|