2026-11-23 20:28:11 +00:00
|
|
|
"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;
|
|
|
|
|
|
2026-11-24 07:01:40 +00:00
|
|
|
const looksLikeEmail = (s: string) =>
|
|
|
|
|
/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(s.trim());
|
|
|
|
|
const same = (a: string, b: string) =>
|
|
|
|
|
a.trim().toLowerCase() === b.trim().toLowerCase();
|
2026-11-23 20:28:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
2026-11-24 07:01:40 +00:00
|
|
|
const start =
|
|
|
|
|
packages.find((p) => p.id === initial) ??
|
|
|
|
|
packages.find((p) => p.id === "couple") ??
|
|
|
|
|
packages[0];
|
2026-11-23 20:28:11 +00:00
|
|
|
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 {
|
2026-11-24 07:01:40 +00:00
|
|
|
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,
|
|
|
|
|
},
|
2026-11-23 20:28:11 +00:00
|
|
|
},
|
2026-11-24 07:01:40 +00:00
|
|
|
);
|
2026-11-23 20:28:11 +00:00
|
|
|
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) => (
|
2026-11-24 07:01:40 +00:00
|
|
|
<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)}
|
|
|
|
|
/>
|
2026-11-23 20:28:11 +00:00
|
|
|
<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"}>
|
2026-11-24 07:01:40 +00:00
|
|
|
<input
|
|
|
|
|
type="radio"
|
|
|
|
|
name={`${id}-way`}
|
|
|
|
|
checked={byEmail}
|
|
|
|
|
onChange={() => setByEmail(true)}
|
|
|
|
|
/>
|
2026-11-23 20:28:11 +00:00
|
|
|
<strong>E-mailben küldjük el neki</strong>
|
2026-11-24 07:01:40 +00:00
|
|
|
<span className="hint">
|
|
|
|
|
Fizetés után azonnal megkapja a kódot, mellé az utalványt PDF-ben.
|
|
|
|
|
</span>
|
2026-11-23 20:28:11 +00:00
|
|
|
</label>
|
|
|
|
|
<label className={!byEmail ? "gift-option is-chosen" : "gift-option"}>
|
2026-11-24 07:01:40 +00:00
|
|
|
<input
|
|
|
|
|
type="radio"
|
|
|
|
|
name={`${id}-way`}
|
|
|
|
|
checked={!byEmail}
|
|
|
|
|
onChange={() => setByEmail(false)}
|
|
|
|
|
/>
|
2026-11-23 20:28:11 +00:00
|
|
|
<strong>Én adom át</strong>
|
|
|
|
|
<span className="hint">
|
2026-11-24 07:01:40 +00:00
|
|
|
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.
|
2026-11-23 20:28:11 +00:00
|
|
|
</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">
|
2026-11-24 07:01:40 +00:00
|
|
|
<label htmlFor={`${id}-confirm`}>
|
|
|
|
|
Még egyszer, ellenőrzésképp
|
|
|
|
|
</label>
|
2026-11-23 20:28:11 +00:00
|
|
|
<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
|
|
|
|
|
/>
|
2026-11-24 07:01:40 +00:00
|
|
|
<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."}
|
2026-11-23 20:28:11 +00:00
|
|
|
</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">
|
2026-11-24 07:01:40 +00:00
|
|
|
Rajta lesz az utalványon és a levélben. Link nem lehet benne.{" "}
|
|
|
|
|
{message.length}/{MAX_MESSAGE}
|
2026-11-23 20:28:11 +00:00
|
|
|
</span>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
|
<p className="msg msg-error" role="alert">
|
|
|
|
|
{error}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
<div className="gift-submit">
|
|
|
|
|
{chosen && (
|
|
|
|
|
<p className="gift-total">
|
|
|
|
|
<span className="muted">
|
2026-11-24 07:01:40 +00:00
|
|
|
„{chosen.name}” csomag ajándékba
|
|
|
|
|
{name.trim() ? `, ${name.trim()} részére` : ""}
|
2026-11-23 20:28:11 +00:00
|
|
|
</span>
|
|
|
|
|
<strong className="num">{huf(chosen.price_huf)}</strong>
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
{canPay ? (
|
2026-11-24 07:01:40 +00:00
|
|
|
<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"}
|
2026-11-23 20:28:11 +00:00
|
|
|
</button>
|
|
|
|
|
) : (
|
|
|
|
|
<button type="button" className="btn" disabled>
|
|
|
|
|
Hamarosan
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-11-24 07:01:40 +00:00
|
|
|
{canPay === "mock" && (
|
|
|
|
|
<p className="hint">
|
|
|
|
|
Tesztüzem: a fizetés még nincs bekötve, a gomb nem terhel semmit.
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
2026-11-23 20:28:11 +00:00
|
|
|
{stripe && (
|
|
|
|
|
<p className="hint">
|
2026-11-24 07:01:40 +00:00
|
|
|
<span aria-hidden="true">🔒</span> A fizetés a Stripe biztonságos
|
|
|
|
|
oldalán történik. A számlát te kapod.
|
2026-11-23 20:28:11 +00:00
|
|
|
{termsConsent && (
|
|
|
|
|
<>
|
|
|
|
|
{" "}
|
2026-11-24 07:01:40 +00:00
|
|
|
A vásárlástól számított 14 napon belül, amíg a kódot nem váltották
|
|
|
|
|
be, elállhatsz, és visszatérítjük az árát. A kód beváltásával az
|
|
|
|
|
elállási jog megszűnik. <Link href="/aszf">ÁSZF</Link>
|
2026-11-23 20:28:11 +00:00
|
|
|
</>
|
|
|
|
|
)}
|
|
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
{canPay === "stripe_test" && (
|
|
|
|
|
<p className="hint">
|
2026-11-24 07:01:40 +00:00
|
|
|
Stripe tesztmód (csak adminoknak látszik): kártyaszám 4242 4242 4242
|
|
|
|
|
4242, bármilyen jövőbeli lejárat és CVC.
|
2026-11-23 20:28:11 +00:00
|
|
|
</p>
|
|
|
|
|
)}
|
|
|
|
|
</form>
|
|
|
|
|
);
|
|
|
|
|
}
|