- Gifts bought 15 Nov - 6 Jan get a Christmas card ("Boldog karácsonyt és
csillagos 2027-et!", snowflakes) and a Christmas e-mail subject.
- The buyer can time the gift e-mail (e.g. Christmas morning); "Küldés most"
replaces a waiting one, a refund takes it back.
- Admin: timed e-mails by hour (waiting / sent / failed), e-mail state per gift.
- /karacsony landing page; seasonal hints on /csomagok and /ajandek.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
276 lines
8.1 KiB
TypeScript
276 lines
8.1 KiB
TypeScript
"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",
|
||
});
|
||
/** "december 24., 8:00" */
|
||
const when = (iso: string) =>
|
||
new Date(iso).toLocaleString("hu-HU", {
|
||
timeZone: "Europe/Budapest",
|
||
month: "long",
|
||
day: "numeric",
|
||
hour: "numeric",
|
||
minute: "2-digit",
|
||
});
|
||
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>
|
||
) : gift.status === "refunded" ? (
|
||
<span className="badge">
|
||
Visszatérítve{gift.refunded_at ? `: ${day(gift.refunded_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 &&
|
||
gift.email_state === "scheduled" &&
|
||
gift.send_at ? (
|
||
<>
|
||
Időzítve: <strong>{when(gift.send_at)}</strong>, ide:{" "}
|
||
<strong>{gift.recipient_email}</strong>
|
||
</>
|
||
) : gift.recipient_email && gift.email_state === "failed" ? (
|
||
<>
|
||
Nem sikerült elküldeni ide: <strong>{gift.recipient_email}</strong>.
|
||
Ellenőrizd a címet, és küldd újra.
|
||
</>
|
||
) : gift.recipient_email ? (
|
||
<>
|
||
E-mailben elküldve ide: <strong>{gift.recipient_email}</strong>
|
||
{gift.sendings > 1 ? ` (${gift.sendings}×)` : ""}
|
||
</>
|
||
) : open ? (
|
||
"Te adod át: töltsd le az utalványt."
|
||
) : (
|
||
"Nem e-mailben ment."
|
||
)}
|
||
{gift.from_name && (
|
||
<span className="hint"> · Kitől: {gift.from_name}</span>
|
||
)}
|
||
</p>
|
||
{gift.message && (
|
||
<blockquote className="gift-message">{gift.message}</blockquote>
|
||
)}
|
||
|
||
{gift.status === "refunded" ? (
|
||
<p className="hint">
|
||
Elálltál a vásárlástól: a kód már nem érvényes, az árát
|
||
visszatérítettük.
|
||
</p>
|
||
) : (
|
||
<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.email_state === "scheduled"
|
||
? "Küldés most"
|
||
: 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>
|
||
);
|
||
}
|