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,251 @@
|
||||
"use client";
|
||||
|
||||
import Link from "next/link";
|
||||
import { useRouter } from "next/navigation";
|
||||
import { useEffect, useId, useState } from "react";
|
||||
|
||||
import { huf } from "@/components/PackageCards";
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import { contents } from "@/lib/packageContents";
|
||||
import type { Package, PayMethod } from "@/lib/types";
|
||||
|
||||
const MAX_MESSAGE = 300;
|
||||
const MAX_NAME = 60;
|
||||
|
||||
const looksLikeEmail = (s: string) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s.trim());
|
||||
const same = (a: string, b: string) => a.trim().toLowerCase() === b.trim().toLowerCase();
|
||||
|
||||
/**
|
||||
* The gift order: which package, whom it is for and from whom, how it gets
|
||||
* to the recipient (we e-mail it, the address typed twice; or the buyer
|
||||
* hands it over), and a message. Then Stripe's payment page, back to "My
|
||||
* gifts".
|
||||
*/
|
||||
export function GiftForm({
|
||||
packages,
|
||||
initial,
|
||||
defaultFrom,
|
||||
canPay,
|
||||
termsConsent,
|
||||
}: {
|
||||
packages: Package[];
|
||||
initial?: string;
|
||||
defaultFrom: string;
|
||||
canPay: PayMethod | null;
|
||||
termsConsent: boolean;
|
||||
}) {
|
||||
const router = useRouter();
|
||||
const id = useId();
|
||||
const start = packages.find((p) => p.id === initial) ?? packages.find((p) => p.id === "couple") ?? packages[0];
|
||||
const [pkg, setPkg] = useState(start?.id ?? "");
|
||||
const [name, setName] = useState("");
|
||||
const [byEmail, setByEmail] = useState(true);
|
||||
const [email, setEmail] = useState("");
|
||||
const [confirm, setConfirm] = useState("");
|
||||
const [from, setFrom] = useState(defaultFrom.slice(0, MAX_NAME));
|
||||
const [message, setMessage] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// Back from Stripe's page (the browser may restore this page as it was).
|
||||
useEffect(() => {
|
||||
const reset = (e: PageTransitionEvent) => {
|
||||
if (e.persisted) setBusy(false);
|
||||
};
|
||||
window.addEventListener("pageshow", reset);
|
||||
return () => window.removeEventListener("pageshow", reset);
|
||||
}, []);
|
||||
|
||||
const chosen = packages.find((p) => p.id === pkg);
|
||||
const mismatch = byEmail && confirm.trim() !== "" && !same(email, confirm);
|
||||
const emailOk = !byEmail || (looksLikeEmail(email) && same(email, confirm));
|
||||
const stripe = canPay === "stripe" || canPay === "stripe_test";
|
||||
|
||||
async function submit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
if (!chosen || !emailOk || !name.trim()) return;
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const r = await api<{ id: string; url: string | null }>("/gifts/checkout", {
|
||||
body: {
|
||||
package: chosen.id,
|
||||
recipient_name: name.trim(),
|
||||
recipient_email: byEmail ? email.trim() : null,
|
||||
recipient_email_confirm: byEmail ? confirm.trim() : null,
|
||||
from_name: from.trim() || null,
|
||||
message: message.trim() || null,
|
||||
},
|
||||
});
|
||||
if (r.url) {
|
||||
window.location.assign(r.url);
|
||||
return;
|
||||
}
|
||||
router.push("/ajandekaim");
|
||||
} catch (err) {
|
||||
setError(errorText(err));
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<form className="card gift-form" onSubmit={submit}>
|
||||
<fieldset className="gift-set">
|
||||
<legend className="label">Melyik csomagot adod?</legend>
|
||||
<div className="gift-packages">
|
||||
{packages.map((p) => (
|
||||
<label key={p.id} className={p.id === pkg ? "gift-option is-chosen" : "gift-option"}>
|
||||
<input type="radio" name={`${id}-pkg`} value={p.id} checked={p.id === pkg} onChange={() => setPkg(p.id)} />
|
||||
<span className="gift-option-head">
|
||||
<strong>{p.name}</strong>
|
||||
<span className="num">{huf(p.price_huf)}</span>
|
||||
</span>
|
||||
<span className="hint">{contents(p).join(" · ")}</span>
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
</fieldset>
|
||||
|
||||
<div className="grid-2">
|
||||
<div className="field">
|
||||
<label htmlFor={`${id}-name`}>Kinek szól?</label>
|
||||
<input
|
||||
id={`${id}-name`}
|
||||
className="inp"
|
||||
value={name}
|
||||
maxLength={MAX_NAME}
|
||||
onChange={(e) => setName(e.target.value)}
|
||||
placeholder="Például: Bea"
|
||||
aria-describedby={`${id}-name-hint`}
|
||||
required
|
||||
/>
|
||||
<span id={`${id}-name-hint`} className="hint">
|
||||
Az utalványon: „{name.trim() || "Bea"} részére”
|
||||
{byEmail ? `, a levélben: „Szia ${name.trim() || "Bea"}!”` : ""}.
|
||||
</span>
|
||||
</div>
|
||||
<div className="field">
|
||||
<label htmlFor={`${id}-from`}>Kitől? (a te neved)</label>
|
||||
<input
|
||||
id={`${id}-from`}
|
||||
className="inp"
|
||||
value={from}
|
||||
maxLength={MAX_NAME}
|
||||
onChange={(e) => setFrom(e.target.value)}
|
||||
placeholder="Például: Anna"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<fieldset className="gift-set">
|
||||
<legend className="label">Hogyan kapja meg?</legend>
|
||||
<div className="gift-ways">
|
||||
<label className={byEmail ? "gift-option is-chosen" : "gift-option"}>
|
||||
<input type="radio" name={`${id}-way`} checked={byEmail} onChange={() => setByEmail(true)} />
|
||||
<strong>E-mailben küldjük el neki</strong>
|
||||
<span className="hint">Fizetés után azonnal megkapja a kódot, mellé az utalványt PDF-ben.</span>
|
||||
</label>
|
||||
<label className={!byEmail ? "gift-option is-chosen" : "gift-option"}>
|
||||
<input type="radio" name={`${id}-way`} checked={!byEmail} onChange={() => setByEmail(false)} />
|
||||
<strong>Én adom át</strong>
|
||||
<span className="hint">
|
||||
Letöltöd az utalványt PDF-ben: kinyomtathatod, vagy elküldheted te magad. Később innen is kérheted, hogy
|
||||
e-mailben küldjük el.
|
||||
</span>
|
||||
</label>
|
||||
</div>
|
||||
{byEmail && (
|
||||
<div className="grid-2">
|
||||
<div className="field">
|
||||
<label htmlFor={`${id}-email`}>A címzett e-mail-címe</label>
|
||||
<input
|
||||
id={`${id}-email`}
|
||||
className="inp"
|
||||
type="email"
|
||||
autoComplete="off"
|
||||
value={email}
|
||||
onChange={(e) => setEmail(e.target.value)}
|
||||
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}
|
||||
aria-describedby={`${id}-confirm-msg`}
|
||||
required
|
||||
/>
|
||||
<span id={`${id}-confirm-msg`} className={mismatch ? "msg msg-error" : "hint"} aria-live="polite">
|
||||
{mismatch ? "A két cím nem egyezik." : "Hogy biztosan jó címre menjen."}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</fieldset>
|
||||
|
||||
<div className="field">
|
||||
<label htmlFor={`${id}-message`}>Személyes üzenet (nem kötelező)</label>
|
||||
<textarea
|
||||
id={`${id}-message`}
|
||||
className="inp"
|
||||
value={message}
|
||||
maxLength={MAX_MESSAGE}
|
||||
onChange={(e) => setMessage(e.target.value)}
|
||||
placeholder="Boldog születésnapot! Nézd meg, mit mesélnek rólad a csillagok."
|
||||
aria-describedby={`${id}-message-hint`}
|
||||
/>
|
||||
<span id={`${id}-message-hint`} className="hint">
|
||||
Rajta lesz az utalványon és a levélben. Link nem lehet benne. {message.length}/{MAX_MESSAGE}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
<div className="gift-submit">
|
||||
{chosen && (
|
||||
<p className="gift-total">
|
||||
<span className="muted">
|
||||
„{chosen.name}” csomag ajándékba{name.trim() ? `, ${name.trim()} részére` : ""}
|
||||
</span>
|
||||
<strong className="num">{huf(chosen.price_huf)}</strong>
|
||||
</p>
|
||||
)}
|
||||
{canPay ? (
|
||||
<button type="submit" className="btn btn-primary" disabled={busy || !chosen || !emailOk || !name.trim()}>
|
||||
{busy ? (stripe ? "Átirányítás…" : "Fizetés…") : stripe ? "Tovább a fizetéshez" : "Megveszem ajándékba"}
|
||||
</button>
|
||||
) : (
|
||||
<button type="button" className="btn" disabled>
|
||||
Hamarosan
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
{canPay === "mock" && <p className="hint">Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.</p>}
|
||||
{stripe && (
|
||||
<p className="hint">
|
||||
<span aria-hidden="true">🔒</span> A fizetés a Stripe biztonságos oldalán történik. A számlát te kapod.
|
||||
{termsConsent && (
|
||||
<>
|
||||
{" "}
|
||||
Az ajándékkód a fizetés után azonnal elkészül, ezért a fizetésnél kéred a teljesítés megkezdését, és ezzel
|
||||
lemondasz a 14 napos elállási jogról. <Link href="/aszf">ÁSZF</Link>
|
||||
</>
|
||||
)}
|
||||
</p>
|
||||
)}
|
||||
{canPay === "stripe_test" && (
|
||||
<p className="hint">
|
||||
Stripe tesztmód (csak adminoknak látszik): kártyaszám 4242 4242 4242 4242, bármilyen jövőbeli lejárat és CVC.
|
||||
</p>
|
||||
)}
|
||||
</form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
import type { Metadata } from "next";
|
||||
import Link from "next/link";
|
||||
|
||||
import { payMethod } from "@/lib/payments";
|
||||
import { backendGet, requireMe } from "@/lib/server";
|
||||
import type { Gift, Package, PublicConfig } from "@/lib/types";
|
||||
|
||||
import { GiftForm } from "./GiftForm";
|
||||
|
||||
export const metadata: Metadata = { title: "Ajándék csomag", robots: { index: false } };
|
||||
|
||||
type Props = { searchParams: Promise<{ csomag?: string }> };
|
||||
|
||||
/** Buying a package as a gift; `?csomag=` picks the package (links on the package cards). */
|
||||
export default async function GiftPage({ searchParams }: Props) {
|
||||
const { csomag } = await searchParams;
|
||||
const me = await requireMe(csomag ? `/ajandek?csomag=${encodeURIComponent(csomag)}` : "/ajandek");
|
||||
const [catalog, config, mine] = await Promise.all([
|
||||
backendGet<{ packages: Package[] }>("/v1/packages"),
|
||||
backendGet<PublicConfig>("/v1/config"),
|
||||
backendGet<{ gifts: Gift[] }>("/v1/gifts"),
|
||||
]);
|
||||
const packages = (catalog?.packages ?? []).filter((p) => p.tier === "main");
|
||||
const given = mine?.gifts.length ?? 0;
|
||||
|
||||
return (
|
||||
<main className="page" style={{ maxWidth: 1080 }}>
|
||||
<div className="page-head">
|
||||
<span className="eyebrow">Ajándék</span>
|
||||
<h1>Ajándékozz csomagot</h1>
|
||||
<p className="muted">
|
||||
Születési képlet, személyre szóló elemzések, kapcsolati összevetés: ajándékba annak, akit szeretsz. Ő egy
|
||||
ajándékkódot kap egy nyomtatható utalvánnyal, és a saját fiókjában váltja be.
|
||||
</p>
|
||||
{given > 0 && (
|
||||
<p className="hint">
|
||||
<Link href="/ajandekaim">Korábbi ajándékaim ({given}) →</Link>
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="gift-layout">
|
||||
<GiftForm
|
||||
packages={packages}
|
||||
initial={csomag}
|
||||
defaultFrom={me.user.display_name ?? ""}
|
||||
canPay={payMethod(config, me)}
|
||||
termsConsent={config?.terms_consent === true}
|
||||
/>
|
||||
<aside className="gift-aside">
|
||||
<figure className="gift-sample">
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img
|
||||
src="/img/ajandek-utalvany.webp"
|
||||
width={420}
|
||||
height={594}
|
||||
alt="Minta ajándékutalvány: a csomag neve, a személyes üzenet, az ajándékkód és a QR-kód"
|
||||
/>
|
||||
<figcaption className="hint">Így néz ki az utalvány (A4-es PDF, nyomtatható)</figcaption>
|
||||
</figure>
|
||||
<section className="card gift-how" aria-labelledby="gift-how-title">
|
||||
<h2 id="gift-how-title">Hogyan működik?</h2>
|
||||
<ol>
|
||||
<li>Kiválasztod a csomagot, és megírod, kinek szól.</li>
|
||||
<li>Fizetés után elkészül az ajándékkód és az utalvány.</li>
|
||||
<li>E-mailben elküldjük neki, vagy letöltöd a PDF-et, és te adod át.</li>
|
||||
<li>Belép vagy regisztrál, beváltja a kódot, és a csomag az övé.</li>
|
||||
</ol>
|
||||
<p className="hint">A kód nem jár le, és egyszer váltható be. A számlát te kapod.</p>
|
||||
</section>
|
||||
</aside>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user