Files
sorsfonalak/services/frontend/src/components/RedeemCode.tsx
T
fecoandClaude Opus 5.5 c90232f40c Gift packages: buy a main package for someone, with a printable gift card
- /ajandek: the package, whom it is for and from whom, the recipient's
  address typed twice (or "I hand it over"), a message; paid on Stripe
  like any package, the invoice is the buyer's
- On payment the gift gets its own code (not a promo code: it cannot be
  withdrawn, and it adds the whole package); redeemed on /bevaltas
- The recipient's e-mail carries the message and the code, with the gift
  card attached as a PDF; the buyer's confirmation says it was a gift
- /ajandekaim: codes, state, the card to download, sending it by e-mail
  (again, or to another address) up to three times
- crates/gift-card draws the A4 card (example chart, questions avatar,
  QR code) as SVG and makes a PDF with krilla, fonts compiled in; the
  backend draws downloads on a blocking thread, the worker the e-mails
- Telegram notices say "(ajándék)"; unpaid gifts go after a week

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

114 lines
3.8 KiB
TypeScript

"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("-") ?? "";
}
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",
tarot: "kártyajóslás",
};
/** "+2 képletelemzés, +3 kérdés", in the order of UNITS. */
function addedText(added: Record<string, number>): string {
const order = Object.keys(UNITS);
const rank = (unit: string) => (order.includes(unit) ? order.indexOf(unit) : order.length);
return Object.entries(added)
.sort(([a], [b]) => rank(a) - rank(b))
.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); a gift code adds the whole
* package.
*/
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);
const [got, setGot] = useState<{ name: string; added: string; gift: { from_name: string | null } | null } | null>(
null,
);
async function redeem(e: React.FormEvent) {
e.preventDefault();
setBusy(true);
setError(null);
try {
const r = await api<{
package: { name: string };
added: Record<string, number>;
gift?: { from_name: string | null };
}>("/promo-codes/redeem", { body: { code } });
setGot({ name: r.package.name, added: addedText(r.added ?? {}), gift: r.gift ?? null });
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 &&
(got.gift ? (
<p className="msg msg-ok">
Beváltva: <strong>{got.name}</strong>
{got.gift.from_name ? `, ${got.gift.from_name} ajándéka` : " ajándék"}. Hozzáadtuk az egyenlegedhez:{" "}
{got.added}. <Link href="/keplet">Tovább a képletedhez</Link>
</p>
) : (
<p className="msg msg-ok">
Beváltva: <strong>{got.name}</strong>. Feltöltöttük az egyenlegedet: {got.added}.{" "}
<Link href="/keplet">Tovább a képletedhez</Link>
</p>
))}
</div>
</form>
);
}