Invitations with a package; promo codes fill up the balance
An admin can pick a package when inviting: each invitee gets a promo code (in the e-mail and its preview, kept on resend), redeemed by itself at sign up; revoking the invitation withdraws it, and the list shows the package. Redeeming a promo code now fills the units up to the package's amounts: used units come back, more than that stays. A code that would add nothing stays open, with a message saying so; the answer tells what was added. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012vLHzh5qUhFtmJhzkcTbXL
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
|
||||
import { usePackages } from "@/components/PackageCards";
|
||||
import { api, errorText } from "@/lib/api";
|
||||
import { formatDate } from "@/lib/astro";
|
||||
import type { AdminInvitation, InvitationResult, WaitlistEntry } from "@/lib/types";
|
||||
@@ -104,6 +105,8 @@ function InviteForm({ onSent }: { onSent: () => Promise<void> }) {
|
||||
const [emails, setEmails] = useState("");
|
||||
const [message, setMessage] = useState("");
|
||||
const [locale, setLocale] = useState("hu");
|
||||
const [pkg, setPkg] = useState("");
|
||||
const packages = usePackages();
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [results, setResults] = useState<InvitationResult[] | null>(null);
|
||||
@@ -116,7 +119,7 @@ function InviteForm({ onSent }: { onSent: () => Promise<void> }) {
|
||||
setResults(null);
|
||||
try {
|
||||
const res = await api<{ results: InvitationResult[] }>("/admin/invitations", {
|
||||
body: { emails: parseEmails(emails), message: message.trim() || null, locale },
|
||||
body: { emails: parseEmails(emails), message: message.trim() || null, locale, package: pkg || null },
|
||||
});
|
||||
setResults(res.results);
|
||||
if (res.results.some((r) => r.status === "invited")) setEmails("");
|
||||
@@ -163,6 +166,17 @@ function InviteForm({ onSent }: { onSent: () => Promise<void> }) {
|
||||
</span>
|
||||
</div>
|
||||
<div className="row" style={{ gap: 12, alignItems: "flex-end", flexWrap: "wrap" }}>
|
||||
<div className="field" style={{ minWidth: 220 }}>
|
||||
<label htmlFor="invite-package">Ajándék csomag</label>
|
||||
<select id="invite-package" className="inp" value={pkg} onChange={(e) => setPkg(e.target.value)}>
|
||||
<option value="">nincs</option>
|
||||
{(packages ?? []).map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</div>
|
||||
<div className="field" style={{ width: 160 }}>
|
||||
<label htmlFor="invite-locale">A levél nyelve</label>
|
||||
<select id="invite-locale" className="inp" value={locale} onChange={(e) => setLocale(e.target.value)}>
|
||||
@@ -175,6 +189,12 @@ function InviteForm({ onSent }: { onSent: () => Promise<void> }) {
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
{pkg && (
|
||||
<p className="hint" style={{ margin: 0 }}>
|
||||
Minden meghívott kap egy promókódot erre a csomagra. A levélben benne lesz, és a regisztrációkor magától
|
||||
beváltódik. A meghívó visszavonása a kódot is visszavonja.
|
||||
</p>
|
||||
)}
|
||||
{error && (
|
||||
<p className="msg msg-error" role="alert">
|
||||
{error}
|
||||
@@ -187,18 +207,19 @@ function InviteForm({ onSent }: { onSent: () => Promise<void> }) {
|
||||
<span className={r.status === "invited" ? "msg-ok" : "muted"}>
|
||||
{r.email}: {RESULT[r.status]}
|
||||
</span>
|
||||
{r.promo_code && <code className="promo-code">{r.promo_code}</code>}
|
||||
{r.url && <CopyLink url={r.url} />}
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
)}
|
||||
<MailPreview message={message} locale={locale} />
|
||||
<MailPreview message={message} locale={locale} pkg={pkg} />
|
||||
</section>
|
||||
);
|
||||
}
|
||||
|
||||
/** The invitation e-mail as the recipient sees it, and a test copy to the admin. */
|
||||
function MailPreview({ message, locale }: { message: string; locale: string }) {
|
||||
function MailPreview({ message, locale, pkg }: { message: string; locale: string; pkg: string }) {
|
||||
const [mail, setMail] = useState<{ subject: string; html: string; text: string } | null>(null);
|
||||
const [view, setView] = useState<"html" | "text">("html");
|
||||
const [busy, setBusy] = useState(false);
|
||||
@@ -210,7 +231,7 @@ function MailPreview({ message, locale }: { message: string; locale: string }) {
|
||||
try {
|
||||
const res = await api<{ subject: string; html: string; text: string; sent_to: string | null }>(
|
||||
"/admin/invitations/preview",
|
||||
{ body: { message: message.trim() || null, locale, send_to_me: sendToMe } },
|
||||
{ body: { message: message.trim() || null, locale, send_to_me: sendToMe, package: pkg || null } },
|
||||
);
|
||||
setMail(res);
|
||||
if (res.sent_to) setNote({ ok: true, text: `Tesztlevél a sorban: ${res.sent_to}. A linkjei nem működnek.` });
|
||||
@@ -337,6 +358,12 @@ function InvitationRow({ invitation: i, onChange }: { invitation: AdminInvitatio
|
||||
<td>
|
||||
<span className={`badge invite-${i.status}`}>{STATUS[i.status]}</span>
|
||||
{i.accepted_by && <div className="hint">{i.accepted_by}</div>}
|
||||
{i.package && (
|
||||
<div className="hint" title={i.package.code ?? undefined}>
|
||||
Ajándék: {i.package.name ?? i.package.id}
|
||||
{i.package.redeemed_at ? " (beváltva)" : ""}
|
||||
</div>
|
||||
)}
|
||||
</td>
|
||||
<td>
|
||||
<span className={i.mail === "failed" ? "msg-error" : i.mail === "queued" ? "muted" : undefined}>
|
||||
|
||||
@@ -17,8 +17,8 @@ export default async function RedeemPage({ searchParams }: Props) {
|
||||
<span className="eyebrow">Promókód</span>
|
||||
<h1>Promókód beváltása</h1>
|
||||
<p className="muted">
|
||||
Írd be a kapott kódot. A hozzá tartozó csomag azonnal a tiéd, az elemzései nem járnak le. Egy kód egyszer
|
||||
váltható be.
|
||||
Írd be a kapott kódot. Az egyenlegedet azonnal feltöltjük a csomag szintjéig: amit már elhasználtál belőle,
|
||||
visszakapod, ami ennél több van, megmarad. Egy kód egyszer váltható be.
|
||||
</p>
|
||||
</div>
|
||||
<section className="card">
|
||||
|
||||
@@ -12,22 +12,42 @@ function format(input: string): string {
|
||||
return clean.match(/.{1,4}/g)?.join("-") ?? "";
|
||||
}
|
||||
|
||||
/** Redeeming a promo code: the package arrives at once. */
|
||||
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",
|
||||
};
|
||||
|
||||
/** "+2 képletelemzés, +3 kérdés" */
|
||||
function addedText(added: Record<string, number>): string {
|
||||
return Object.entries(added)
|
||||
.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).
|
||||
*/
|
||||
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<string | null>(null);
|
||||
const [got, setGot] = useState<{ name: string; added: string } | null>(null);
|
||||
|
||||
async function redeem(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
setBusy(true);
|
||||
setError(null);
|
||||
try {
|
||||
const r = await api<{ package: { name: string } }>("/promo-codes/redeem", { body: { code } });
|
||||
setGot(r.package.name);
|
||||
const r = await api<{ package: { name: string }; added: Record<string, number> }>("/promo-codes/redeem", {
|
||||
body: { code },
|
||||
});
|
||||
setGot({ name: r.package.name, added: addedText(r.added ?? {}) });
|
||||
setCode("");
|
||||
router.refresh();
|
||||
} catch (err) {
|
||||
@@ -67,8 +87,8 @@ export function RedeemCode({ initial = "", compact = false }: { initial?: string
|
||||
{error && <p className="msg msg-error">{error}</p>}
|
||||
{got && (
|
||||
<p className="msg msg-ok">
|
||||
Megkaptad: <strong>{got}</strong>. Az elemzéseid már felhasználhatók,{" "}
|
||||
<Link href="/keplet">indulhat a képleted</Link>.
|
||||
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>
|
||||
|
||||
@@ -413,6 +413,8 @@ export interface AdminInvitation {
|
||||
mail: "sent" | "queued" | "failed" | "archived";
|
||||
mail_error: string | null;
|
||||
can_resend: boolean;
|
||||
/** The package it comes with (a promo code, redeemed at sign-up). */
|
||||
package: { id: string; name: string | null; code: string | null; redeemed_at: string | null } | null;
|
||||
}
|
||||
|
||||
export interface WaitlistEntry {
|
||||
@@ -428,6 +430,8 @@ export interface InvitationResult {
|
||||
status: "invited" | "invalid" | "already_user" | "already_invited" | "opted_out";
|
||||
id?: string;
|
||||
url?: string;
|
||||
/** The promo code of the package sent with it. */
|
||||
promo_code?: string | null;
|
||||
}
|
||||
|
||||
/** A calculated year in a list (`GET /v1/charts/{id}/forecasts`). */
|
||||
|
||||
Reference in New Issue
Block a user