2026-11-19 09:45:42 +00:00
|
|
|
"use client";
|
|
|
|
|
|
|
|
|
|
import Link from "next/link";
|
|
|
|
|
import { useRouter } from "next/navigation";
|
|
|
|
|
import { useId, useState } from "react";
|
|
|
|
|
|
|
|
|
|
import { api, ApiError, errorText } from "@/lib/api";
|
|
|
|
|
|
|
|
|
|
/** Typing help: upper case, groups of four (ABCD-EFGH-JKLM). */
|
|
|
|
|
function format(input: string): string {
|
|
|
|
|
const clean = input.toUpperCase().replace(/[^0-9A-Z]/g, "").slice(0, 32);
|
|
|
|
|
return clean.match(/.{1,4}/g)?.join("-") ?? "";
|
|
|
|
|
}
|
|
|
|
|
|
2026-11-19 09:57:01 +00:00
|
|
|
const UNITS: Record<string, string> = {
|
|
|
|
|
natal: "képletelemzés",
|
|
|
|
|
synastry: "kapcsolati elemzés",
|
|
|
|
|
forecast: "éves előrejelzés",
|
|
|
|
|
person: "személyhely",
|
|
|
|
|
question: "kérdés",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/** "+2 képletelemzés, +3 kérdés" */
|
|
|
|
|
function addedText(added: Record<string, number>): string {
|
|
|
|
|
return Object.entries(added)
|
|
|
|
|
.map(([unit, n]) => `+${n} ${UNITS[unit] ?? unit}`)
|
|
|
|
|
.join(", ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Redeeming a promo code. It fills the units up to the package's amounts
|
|
|
|
|
* (used ones come back, more than that stays).
|
|
|
|
|
*/
|
2026-11-19 09:45:42 +00:00
|
|
|
export function RedeemCode({ initial = "", compact = false }: { initial?: string; compact?: boolean }) {
|
|
|
|
|
const router = useRouter();
|
|
|
|
|
const id = useId();
|
|
|
|
|
const [code, setCode] = useState(format(initial));
|
|
|
|
|
const [busy, setBusy] = useState(false);
|
|
|
|
|
const [error, setError] = useState<string | null>(null);
|
2026-11-19 09:57:01 +00:00
|
|
|
const [got, setGot] = useState<{ name: string; added: string } | null>(null);
|
2026-11-19 09:45:42 +00:00
|
|
|
|
|
|
|
|
async function redeem(e: React.FormEvent) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
setBusy(true);
|
|
|
|
|
setError(null);
|
|
|
|
|
try {
|
2026-11-19 09:57:01 +00:00
|
|
|
const r = await api<{ package: { name: string }; added: Record<string, number> }>("/promo-codes/redeem", {
|
|
|
|
|
body: { code },
|
|
|
|
|
});
|
|
|
|
|
setGot({ name: r.package.name, added: addedText(r.added ?? {}) });
|
2026-11-19 09:45:42 +00:00
|
|
|
setCode("");
|
|
|
|
|
router.refresh();
|
|
|
|
|
} catch (err) {
|
|
|
|
|
setError(
|
|
|
|
|
err instanceof ApiError && err.status === 429
|
|
|
|
|
? "Túl sok sikertelen próbálkozás. Várj negyed órát, és próbáld újra."
|
|
|
|
|
: errorText(err),
|
|
|
|
|
);
|
|
|
|
|
} finally {
|
|
|
|
|
setBusy(false);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<form className={compact ? "redeem redeem-compact" : "redeem"} onSubmit={redeem}>
|
|
|
|
|
<label htmlFor={id} className={compact ? "label" : "sr-only"}>
|
|
|
|
|
Promókód
|
|
|
|
|
</label>
|
|
|
|
|
<div className="redeem-row">
|
|
|
|
|
<input
|
|
|
|
|
id={id}
|
|
|
|
|
className="inp redeem-input"
|
|
|
|
|
value={code}
|
|
|
|
|
onChange={(e) => setCode(format(e.target.value))}
|
|
|
|
|
placeholder="XXXX-XXXX-XXXX"
|
|
|
|
|
autoComplete="off"
|
|
|
|
|
autoCapitalize="characters"
|
|
|
|
|
spellCheck={false}
|
|
|
|
|
inputMode="text"
|
|
|
|
|
aria-describedby={`${id}-msg`}
|
|
|
|
|
/>
|
|
|
|
|
<button type="submit" className="btn btn-primary" disabled={busy || code.replace(/-/g, "").length < 8}>
|
|
|
|
|
{busy ? "Beváltás…" : "Beváltom"}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
<div id={`${id}-msg`} aria-live="polite">
|
|
|
|
|
{error && <p className="msg msg-error">{error}</p>}
|
|
|
|
|
{got && (
|
|
|
|
|
<p className="msg msg-ok">
|
2026-11-19 09:57:01 +00:00
|
|
|
Beváltva: <strong>{got.name}</strong>. Feltöltöttük az egyenlegedet: {got.added}.{" "}
|
|
|
|
|
<Link href="/keplet">Tovább a képletedhez</Link>
|
2026-11-19 09:45:42 +00:00
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|