Gutscheine: Neuer Gutschein als Popup über „Gutschein erstellen“
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL
This commit is contained in:
parent
ce4820cab0
commit
dc45fae81b
4 changed files with 109 additions and 33 deletions
|
|
@ -1,42 +1,25 @@
|
|||
import { db } from "@/lib/db";
|
||||
import { requireAdmin } from "@/lib/auth";
|
||||
import { centsToInput, formatPrice } from "@/lib/money";
|
||||
import { Field, SelectField } from "@/components/Field";
|
||||
import { formatPrice } from "@/lib/money";
|
||||
import { AdminForm, ConfirmButton } from "@/components/admin/AdminForm";
|
||||
import { PageHead, Pill } from "@/components/admin/ui";
|
||||
import { CouponFields } from "@/components/admin/CouponFields";
|
||||
import { NewCouponDialog } from "@/components/admin/NewCouponDialog";
|
||||
import { Empty, PageHead, Pill } from "@/components/admin/ui";
|
||||
import { deleteCoupon, saveCoupon } from "./actions";
|
||||
import type { Coupon } from "@prisma/client";
|
||||
|
||||
function CouponFields({ c }: { c?: Coupon }) {
|
||||
return (
|
||||
<>
|
||||
{c && <input type="hidden" name="id" value={c.id} />}
|
||||
<div className="sm-form-row">
|
||||
<Field label="Code" name="code" defaultValue={c?.code} placeholder="z. B. SOMMER15" required />
|
||||
<SelectField label="Art" name="type" defaultValue={c?.type ?? "percent"}>
|
||||
<option value="percent">Prozent vom Warenwert</option>
|
||||
<option value="fixed">Fester Betrag in €</option>
|
||||
</SelectField>
|
||||
</div>
|
||||
<div className="sm-form-row">
|
||||
<Field label="Wert" name="value" inputMode="decimal" defaultValue={c ? (c.type === "percent" ? String(c.value) : centsToInput(c.value)) : ""} hint="z. B. 10 für 10 % oder 15,00 für 15 €" required />
|
||||
<Field label="Mindestbestellwert in €" optional name="minTotal" inputMode="decimal" defaultValue={c?.minTotal ? centsToInput(c.minTotal) : ""} />
|
||||
</div>
|
||||
<div className="sm-form-row">
|
||||
<Field label="Gültig bis" optional name="validUntil" type="date" defaultValue={c?.validUntil ? c.validUntil.toISOString().slice(0, 10) : ""} />
|
||||
<Field label="Höchstens einlösbar" optional name="maxUses" type="number" min={0} defaultValue={c?.maxUses || ""} hint="Leer = unbegrenzt" />
|
||||
</div>
|
||||
<label className="ad-switch"><span className="ad-switch__text"><strong>Aktiv</strong></span><input type="checkbox" role="switch" name="active" defaultChecked={c?.active ?? true} /></label>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
export default async function CouponsPage() {
|
||||
await requireAdmin("coupons.edit");
|
||||
const coupons = await db.coupon.findMany({ orderBy: { createdAt: "desc" } });
|
||||
return (
|
||||
<div className="ad-wrap ad-wrap--narrow">
|
||||
<PageHead title="Gutscheine" lead="Rabattcodes, die Kunden im Warenkorb einlösen." />
|
||||
<PageHead title="Gutscheine" lead="Rabattcodes, die Kunden im Warenkorb einlösen. Klick auf einen Gutschein, um ihn zu bearbeiten.">
|
||||
<NewCouponDialog />
|
||||
</PageHead>
|
||||
{coupons.length === 0 && (
|
||||
<section className="ad-card">
|
||||
<Empty title="Noch keine Gutscheine" text="Lege z. B. SOMMER15 für 15 % Rabatt an." />
|
||||
</section>
|
||||
)}
|
||||
{coupons.map((c) => (
|
||||
<details className="ad-card" key={c.id} style={{ marginBottom: "var(--space-3)" }}>
|
||||
<summary className="ad-card__head" style={{ cursor: "pointer", listStyle: "none" }}>
|
||||
|
|
@ -53,10 +36,6 @@ export default async function CouponsPage() {
|
|||
</div>
|
||||
</details>
|
||||
))}
|
||||
<section className="ad-card" style={{ marginTop: "var(--space-6)" }}>
|
||||
<div className="ad-card__head"><h2>Neuer Gutschein</h2></div>
|
||||
<div className="ad-card__body"><AdminForm action={saveCoupon} submitLabel="Gutschein anlegen"><CouponFields /></AdminForm></div>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
|
|
|||
28
src/components/admin/CouponFields.tsx
Normal file
28
src/components/admin/CouponFields.tsx
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import type { Coupon } from "@prisma/client";
|
||||
import { centsToInput } from "@/lib/money";
|
||||
import { Field, SelectField } from "../Field";
|
||||
|
||||
/** Felder eines Gutscheins, für Bearbeiten und Anlegen (im Popup). */
|
||||
export function CouponFields({ c }: { c?: Coupon }) {
|
||||
return (
|
||||
<>
|
||||
{c && <input type="hidden" name="id" value={c.id} />}
|
||||
<div className="sm-form-row">
|
||||
<Field label="Code" name="code" defaultValue={c?.code} placeholder="z. B. SOMMER15" required />
|
||||
<SelectField label="Art" name="type" defaultValue={c?.type ?? "percent"}>
|
||||
<option value="percent">Prozent vom Warenwert</option>
|
||||
<option value="fixed">Fester Betrag in €</option>
|
||||
</SelectField>
|
||||
</div>
|
||||
<div className="sm-form-row">
|
||||
<Field label="Wert" name="value" inputMode="decimal" defaultValue={c ? (c.type === "percent" ? String(c.value) : centsToInput(c.value)) : ""} hint="z. B. 10 für 10 % oder 15,00 für 15 €" required />
|
||||
<Field label="Mindestbestellwert in €" optional name="minTotal" inputMode="decimal" defaultValue={c?.minTotal ? centsToInput(c.minTotal) : ""} />
|
||||
</div>
|
||||
<div className="sm-form-row">
|
||||
<Field label="Gültig bis" optional name="validUntil" type="date" defaultValue={c?.validUntil ? c.validUntil.toISOString().slice(0, 10) : ""} />
|
||||
<Field label="Höchstens einlösbar" optional name="maxUses" type="number" min={0} defaultValue={c?.maxUses || ""} hint="Leer = unbegrenzt" />
|
||||
</div>
|
||||
<label className="ad-switch"><span className="ad-switch__text"><strong>Aktiv</strong></span><input type="checkbox" role="switch" name="active" defaultChecked={c?.active ?? true} /></label>
|
||||
</>
|
||||
);
|
||||
}
|
||||
55
src/components/admin/NewCouponDialog.tsx
Normal file
55
src/components/admin/NewCouponDialog.tsx
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
"use client";
|
||||
import { useEffect, useRef } from "react";
|
||||
import type { ActionResult } from "@/lib/action-result";
|
||||
import { saveCoupon } from "@/app/admin/(panel)/gutscheine/actions";
|
||||
import { AdminForm } from "./AdminForm";
|
||||
import { CouponFields } from "./CouponFields";
|
||||
import { Icon } from "../Icon";
|
||||
|
||||
function CloseOnSuccess({ state, close }: { state: ActionResult; close: () => void }) {
|
||||
useEffect(() => {
|
||||
if (state?.ok) close();
|
||||
}, [state, close]);
|
||||
return null;
|
||||
}
|
||||
|
||||
/** Knopf „Gutschein erstellen“, das Formular öffnet sich als Popup und schließt nach dem Anlegen. */
|
||||
export function NewCouponDialog() {
|
||||
const dialog = useRef<HTMLDialogElement>(null);
|
||||
const close = useRef(() => dialog.current?.close()).current;
|
||||
return (
|
||||
<>
|
||||
<button className="sm-btn sm-btn--primary" type="button" onClick={() => {
|
||||
dialog.current?.showModal();
|
||||
// Gleich ins Feld „Code“ statt auf den Schließen-Knopf
|
||||
dialog.current?.querySelector<HTMLInputElement>("input[name=code]")?.focus();
|
||||
}}>
|
||||
<Icon name="plus" size="sm" /> Gutschein erstellen
|
||||
</button>
|
||||
<dialog
|
||||
ref={dialog}
|
||||
className="ad-dialog"
|
||||
aria-labelledby="new-coupon-title"
|
||||
// Klick auf den abgedunkelten Hintergrund schließt
|
||||
onClick={(e) => e.target === e.currentTarget && close()}
|
||||
>
|
||||
<div className="ad-dialog__panel">
|
||||
<div className="ad-dialog__head">
|
||||
<h2 id="new-coupon-title">Neuer Gutschein</h2>
|
||||
<button className="ad-dialog__close" type="button" aria-label="Schließen" onClick={close}>×</button>
|
||||
</div>
|
||||
<div className="ad-dialog__body">
|
||||
<AdminForm action={saveCoupon} submitLabel="Gutschein anlegen">
|
||||
{(state) => (
|
||||
<>
|
||||
<CloseOnSuccess state={state} close={close} />
|
||||
<CouponFields />
|
||||
</>
|
||||
)}
|
||||
</AdminForm>
|
||||
</div>
|
||||
</div>
|
||||
</dialog>
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
|
@ -288,3 +288,17 @@ a.ad-kpi:hover { border-color: var(--line-strong); }
|
|||
.ad-nl-confirm { display: inline-flex; gap: var(--space-2); align-items: center; flex-wrap: wrap; }
|
||||
.ad-nl-progress { height: 8px; border-radius: 4px; background: var(--surface); overflow: hidden; }
|
||||
.ad-nl-progress span { display: block; height: 100%; background: var(--gold); transition: width 0.4s; }
|
||||
|
||||
/* Popup */
|
||||
.ad-dialog { padding: 0; border: 0; background: transparent; max-width: min(640px, calc(100vw - 32px)); width: 100%; max-height: calc(100dvh - 32px); color: var(--ink); }
|
||||
.ad-dialog::backdrop { background: rgb(22 20 17 / 0.45); backdrop-filter: blur(2px); }
|
||||
.ad-dialog[open] { animation: ad-dialog-in 0.18s ease-out; }
|
||||
@keyframes ad-dialog-in { from { opacity: 0; transform: translateY(8px) scale(0.98); } }
|
||||
@media (prefers-reduced-motion: reduce) { .ad-dialog[open] { animation: none; } }
|
||||
.ad-dialog__panel { background: var(--surface-raised); border: var(--hairline) solid var(--line); border-top: 2px solid var(--gold); border-radius: var(--radius-md); box-shadow: 0 24px 60px rgb(0 0 0 / 0.18); }
|
||||
.ad-dialog__head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-3); padding: var(--space-4) var(--space-5); border-bottom: var(--hairline) solid var(--line); }
|
||||
.ad-dialog__head h2 { margin: 0; font-family: var(--font-serif); font-size: 24px; font-weight: 500; }
|
||||
.ad-dialog__close { width: 36px; height: 36px; border: 0; border-radius: var(--radius-sm); background: transparent; color: var(--ink-muted); font-size: 26px; line-height: 1; cursor: pointer; }
|
||||
.ad-dialog__close:hover { background: var(--surface-sunken); color: var(--ink); }
|
||||
.ad-dialog__body { padding: var(--space-4) var(--space-5) var(--space-2); }
|
||||
.ad-dialog__body .ad-savebar { position: static; background: none; }
|
||||
|
|
|
|||
Loading…
Reference in a new issue