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
This commit is contained in:
@@ -0,0 +1,174 @@
|
||||
"use client";
|
||||
|
||||
import { useId, useState } from "react";
|
||||
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import type { Gift } from "@/lib/types";
|
||||
|
||||
const day = (iso: string) =>
|
||||
new Date(iso).toLocaleDateString("hu-HU", { timeZone: "Europe/Budapest", year: "numeric", month: "long", day: "numeric" });
|
||||
const time = (iso: string) => new Date(iso).toLocaleTimeString("hu-HU", { timeZone: "Europe/Budapest", hour: "2-digit", minute: "2-digit" });
|
||||
|
||||
/** The user's gifts: code, state, the card to download, and sending it by e-mail. */
|
||||
export function GiftList({ gifts: initial }: { gifts: Gift[] }) {
|
||||
const [gifts, setGifts] = useState(initial);
|
||||
return (
|
||||
<ul className="gift-list">
|
||||
{gifts.map((g) => (
|
||||
<GiftItem key={g.id} gift={g} onChange={(next) => setGifts((all) => all.map((x) => (x.id === next.id ? next : x)))} />
|
||||
))}
|
||||
</ul>
|
||||
);
|
||||
}
|
||||
|
||||
function GiftItem({ gift, onChange }: { gift: Gift; onChange: (g: Gift) => void }) {
|
||||
const id = useId();
|
||||
const [sending, setSending] = useState(false);
|
||||
const [email, setEmail] = useState("");
|
||||
const [confirm, setConfirm] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [sent, setSent] = useState<string | null>(null);
|
||||
const [copied, setCopied] = useState(false);
|
||||
const open = gift.status === "open";
|
||||
const mismatch = confirm.trim() !== "" && email.trim().toLowerCase() !== confirm.trim().toLowerCase();
|
||||
|
||||
async function send(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const next = await api<Gift>(`/gifts/${gift.id}/send`, {
|
||||
body: { recipient_email: email.trim(), recipient_email_confirm: confirm.trim() },
|
||||
});
|
||||
onChange(next);
|
||||
setSent(next.recipient_email);
|
||||
setSending(false);
|
||||
setEmail("");
|
||||
setConfirm("");
|
||||
} catch (err) {
|
||||
setError(errorText(err));
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
async function copy() {
|
||||
try {
|
||||
await navigator.clipboard.writeText(gift.code);
|
||||
setCopied(true);
|
||||
setTimeout(() => setCopied(false), 2000);
|
||||
} catch {
|
||||
// The code stays visible and selectable.
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<li className="card gift-item">
|
||||
<div className="gift-item-head">
|
||||
<div>
|
||||
<h2 className="gift-item-title">
|
||||
„{gift.package.name ?? gift.package.id}” csomag
|
||||
{gift.recipient_name && <span className="gift-item-for"> {gift.recipient_name} részére</span>}
|
||||
</h2>
|
||||
<span className="hint">Vásárolva: {day(gift.paid_at)}</span>
|
||||
</div>
|
||||
{gift.status === "redeemed" ? (
|
||||
<span className="badge">Beváltva{gift.redeemed_at ? `: ${day(gift.redeemed_at)}` : ""}</span>
|
||||
) : (
|
||||
<span className="badge badge-gold">Még nincs beváltva</span>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="gift-code-row">
|
||||
<span className="eyebrow">Ajándékkód</span>
|
||||
<code className="gift-code">{gift.code}</code>
|
||||
<button type="button" className="btn btn-sm btn-ghost" onClick={copy}>
|
||||
{copied ? "Másolva" : "Másolás"}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<p className="gift-to">
|
||||
{gift.recipient_email ? (
|
||||
<>
|
||||
E-mailben elküldve ide: <strong>{gift.recipient_email}</strong>
|
||||
{gift.sendings > 1 ? ` (${gift.sendings}×)` : ""}
|
||||
</>
|
||||
) : (
|
||||
"Te adod át: töltsd le az utalványt."
|
||||
)}
|
||||
{gift.from_name && <span className="hint"> · Kitől: {gift.from_name}</span>}
|
||||
</p>
|
||||
{gift.message && <blockquote className="gift-message">{gift.message}</blockquote>}
|
||||
|
||||
<div className="form-actions">
|
||||
<a className="btn btn-primary btn-sm" href={`/api/v1/gifts/${gift.id}/card.pdf`} download>
|
||||
Utalvány letöltése (PDF)
|
||||
</a>
|
||||
{open && gift.can_send && !sending && (
|
||||
<button type="button" className="btn btn-sm" onClick={() => setSending(true)}>
|
||||
{gift.recipient_email ? "Újraküldés e-mailben" : "Küldés e-mailben"}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{open && !gift.can_send && gift.sendings > 0 && (
|
||||
<p className="hint">E-mailben már többször elküldtük. Ha nem érkezett meg, töltsd le az utalványt, és küldd el te.</p>
|
||||
)}
|
||||
{sent && (
|
||||
<p className="msg msg-ok" role="status">
|
||||
Elküldtük ide: {sent}.
|
||||
</p>
|
||||
)}
|
||||
|
||||
{sending && (
|
||||
<form className="gift-send" onSubmit={send}>
|
||||
<div className="grid-2">
|
||||
<div className="field">
|
||||
<label htmlFor={`${id}-email`}>Kinek küldjük?</label>
|
||||
<input
|
||||
id={`${id}-email`}
|
||||
className="inp"
|
||||
type="email"
|
||||
autoComplete="off"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
placeholder={gift.recipient_email ?? ""}
|
||||
required
|
||||
/>
|
||||
</div>
|
||||
<div className="field">
|
||||
<label htmlFor={`${id}-confirm`}>Még egyszer, ellenőrzésképp</label>
|
||||
<input
|
||||
id={`${id}-confirm`}
|
||||
className="inp"
|
||||
type="email"
|
||||
autoComplete="off"
|
||||
value={confirm}
|
||||
onChange={(e) => setConfirm(e.target.value)}
|
||||
aria-invalid={mismatch || undefined}
|
||||
required
|
||||
/>
|
||||
{mismatch && <span className="msg msg-error">A két cím nem egyezik.</span>}
|
||||
</div>
|
||||
</div>
|
||||
{gift.send_again_at && (
|
||||
<p className="hint">Most küldtük el: {time(gift.send_again_at)} után küldheted újra.</p>
|
||||
)}
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<div className="form-actions">
|
||||
<button type="submit" className="btn btn-primary btn-sm" disabled={busy || mismatch || !email.trim()}>
|
||||
{busy ? "Küldés…" : "Elküldöm"}
|
||||
</button>
|
||||
<button type="button" className="btn btn-sm btn-ghost" onClick={() => setSending(false)}>
|
||||
Mégse
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
)}
|
||||
</li>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import type { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import type { Gift } from "@/lib/types";
|
||||
|
||||
import { GiftList } from "./GiftList";
|
||||
|
||||
export const metadata: Metadata = { title: "Ajándékaim", robots: { index: false } };
|
||||
|
||||
/** The packages the user bought as gifts: codes, cards to download, sending them by e-mail. */
|
||||
export default async function MyGiftsPage() {
|
||||
await requireMe("/ajandekaim");
|
||||
const gifts = (await backendGet<{ gifts: Gift[] }>("/v1/gifts"))?.gifts ?? [];
|
||||
|
||||
return (
|
||||
<main className="page" style={{ maxWidth: 900 }}>
|
||||
<div className="page-head">
|
||||
<span className="eyebrow">Ajándék</span>
|
||||
<h1>Ajándékaim</h1>
|
||||
<p className="muted">
|
||||
Az ajándékba vett csomagjaid. Ha nem e-mailben adnád át, töltsd le az utalványt PDF-ben: kinyomtathatod, vagy
|
||||
elküldheted te magad. Amíg nem váltották be, e-mailben is elküldhetjük, akár másik címre.
|
||||
</p>
|
||||
<div className="form-actions">
|
||||
<Link className="btn btn-primary" href="/ajandek">
|
||||
Új ajándék
|
||||
</Link>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{gifts.length === 0 ? (
|
||||
<section className="card">
|
||||
<p className="muted">
|
||||
Még nem vettél ajándékot. Egy csomagot bárkinek megvehetsz: e-mailben kapja meg, vagy te adod át egy
|
||||
nyomtatható utalvánnyal. <Link href="/ajandek">Ajándékozás →</Link>
|
||||
</p>
|
||||
</section>
|
||||
) : (
|
||||
// A new gift (back from paying) or a change starts the list afresh.
|
||||
<GiftList key={gifts.map((g) => `${g.id}:${g.status}:${g.sendings}`).join()} gifts={gifts} />
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user