- Remove .prettierrc (dead config, Biome replaces Prettier) - Rename lighthouserc.json to lighthouserc.cjs with module.exports for ESM compat - Add logger.warn to empty catch blocks in auth, register, site-settings, prisma-cache, redis, security, rate-limit - Unify ActionResult type: action-helper.ts uses 'ok' consistent with safe-action-shared.ts - Add noUnusedLocals + noUnusedParameters to tsconfig + fix 25 pre-existing unused vars - Replace barrel export src/types/index.ts with direct @/types/common imports - Make trustHost conditional (development only) in auth.ts - Add pre-flight URL validation to update-Nitrov3.sh to catch image.library.url misconfigurations - Improve NITRO_IMAGE_LIBRARY_URL content validation in pre-flight & post-compute checks
254 lines
7.2 KiB
TypeScript
254 lines
7.2 KiB
TypeScript
"use client";
|
|
|
|
import { useTranslations } from "next-intl";
|
|
import { useState } from "react";
|
|
import { createVoucher, deleteVoucher } from "@/actions/admin-vouchers";
|
|
import { ConfirmDialog } from "@/components/admin/confirm-dialog";
|
|
import { StatusCard } from "@/components/admin/dashboard";
|
|
import { CurrencyIcon } from "@/components/shared/currency-icon";
|
|
import { Button } from "@/components/ui/button";
|
|
import { useServerAction } from "@/hooks/use-server-action";
|
|
import { formatDate } from "@/lib/format-date";
|
|
|
|
export type VoucherRow = {
|
|
id: string;
|
|
code: string;
|
|
amount: number;
|
|
maxUses: number;
|
|
useCount: number;
|
|
expiresAt: string | null;
|
|
createdAt: string | null;
|
|
status: "active" | "expired" | "usedUp";
|
|
};
|
|
|
|
export function VouchersClient({
|
|
vouchers,
|
|
canEdit,
|
|
stats,
|
|
}: {
|
|
vouchers: VoucherRow[];
|
|
canEdit: boolean;
|
|
stats: { total: number; active: number; usedUp: number; expired: number };
|
|
}) {
|
|
const t = useTranslations("pages.admin.vouchers");
|
|
const { run, isPending } = useServerAction();
|
|
const [pendingDelete, setPendingDelete] = useState<VoucherRow | null>(null);
|
|
const [code, setCode] = useState("");
|
|
const [amount, setAmount] = useState("");
|
|
const [maxUses, setMaxUses] = useState("1");
|
|
const [expiresAt, setExpiresAt] = useState("");
|
|
|
|
function handleCreate(e: React.FormEvent) {
|
|
e.preventDefault();
|
|
if (!canEdit) return;
|
|
run(
|
|
() =>
|
|
createVoucher({
|
|
code,
|
|
amount: Number(amount),
|
|
maxUses: Number(maxUses),
|
|
expiresAt: expiresAt || undefined,
|
|
}),
|
|
{
|
|
successMessage: t("created"),
|
|
errorMessage: t("createError"),
|
|
onSuccess: () => {
|
|
setCode("");
|
|
setAmount("");
|
|
setMaxUses("1");
|
|
setExpiresAt("");
|
|
},
|
|
},
|
|
);
|
|
}
|
|
|
|
function handleConfirmDelete() {
|
|
if (!pendingDelete) return;
|
|
const id = pendingDelete.id;
|
|
run(() => deleteVoucher({ id }), {
|
|
successMessage: t("deleted"),
|
|
errorMessage: t("deleteError"),
|
|
onSuccess: () => setPendingDelete(null),
|
|
});
|
|
}
|
|
|
|
return (
|
|
<main>
|
|
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
|
|
<StatusCard label={t("vouchers")} value={stats.total} icon="🎟️" />
|
|
<StatusCard
|
|
label={t("active")}
|
|
value={stats.active}
|
|
state={stats.active > 0 ? "ok" : "neutral"}
|
|
icon="✅"
|
|
/>
|
|
<StatusCard
|
|
label={t("usedUp")}
|
|
value={stats.usedUp}
|
|
state={stats.usedUp > 0 ? "warn" : "neutral"}
|
|
icon="📉"
|
|
/>
|
|
<StatusCard
|
|
label={t("expired")}
|
|
value={stats.expired}
|
|
state={stats.expired > 0 ? "danger" : "neutral"}
|
|
icon="⏰"
|
|
/>
|
|
</div>
|
|
|
|
{canEdit && (
|
|
<form onSubmit={handleCreate} className="admin-card mb-6">
|
|
<h3 style={{ marginTop: 0 }}>{t("newVoucher")}</h3>
|
|
<div className="mb-3 grid grid-cols-1 gap-4 md:grid-cols-2">
|
|
<label className="flex flex-col gap-1.5">
|
|
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
{t("form.code")}
|
|
</span>
|
|
<input
|
|
value={code}
|
|
onChange={(e) => setCode(e.target.value)}
|
|
placeholder={t("form.codePlaceholder")}
|
|
maxLength={255}
|
|
required
|
|
disabled={isPending}
|
|
/>
|
|
</label>
|
|
<label className="flex flex-col gap-1.5">
|
|
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
{t("form.amount")}
|
|
</span>
|
|
<input
|
|
value={amount}
|
|
onChange={(e) => setAmount(e.target.value)}
|
|
type="number"
|
|
min={1}
|
|
placeholder={t("form.amountPlaceholder")}
|
|
required
|
|
disabled={isPending}
|
|
/>
|
|
</label>
|
|
<label className="flex flex-col gap-1.5">
|
|
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
{t("form.maxUses")}
|
|
</span>
|
|
<input
|
|
value={maxUses}
|
|
onChange={(e) => setMaxUses(e.target.value)}
|
|
type="number"
|
|
min={1}
|
|
required
|
|
disabled={isPending}
|
|
/>
|
|
</label>
|
|
<label className="flex flex-col gap-1.5">
|
|
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
{t("form.expiresAt")}
|
|
</span>
|
|
<input
|
|
value={expiresAt}
|
|
onChange={(e) => setExpiresAt(e.target.value)}
|
|
type="datetime-local"
|
|
disabled={isPending}
|
|
/>
|
|
</label>
|
|
</div>
|
|
<Button type="submit" variant="default" disabled={isPending}>
|
|
{isPending ? t("form.saving") : t("form.create")}
|
|
</Button>
|
|
</form>
|
|
)}
|
|
|
|
<section className="mt-6">
|
|
<h2 className="admin-section-title">
|
|
{t("title")} ({vouchers.length})
|
|
</h2>
|
|
<div className="admin-card" style={{ padding: 0, overflowX: "auto" }}>
|
|
{vouchers.length === 0 ? (
|
|
<div className="admin-empty p-4">{t("noVouchers")}</div>
|
|
) : (
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>{t("colCode")}</th>
|
|
<th>{t("colAmount")}</th>
|
|
<th>{t("colUses")}</th>
|
|
<th>{t("colStatus")}</th>
|
|
<th>{t("colExpires")}</th>
|
|
<th>{t("colCreated")}</th>
|
|
{canEdit && <th>{t("colActions")}</th>}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{vouchers.map((v) => (
|
|
<tr key={v.id}>
|
|
<td>
|
|
<code>{v.code}</code>
|
|
</td>
|
|
<td>
|
|
<span className="inline-flex items-center gap-1.5 whitespace-nowrap">
|
|
<CurrencyIcon kind="credits" size={14} />
|
|
{v.amount}
|
|
</span>
|
|
</td>
|
|
<td>
|
|
{v.useCount} / {v.maxUses}
|
|
</td>
|
|
<td>
|
|
{v.status === "expired" ? (
|
|
<span className="inline-block text-[0.72rem] font-bold px-2 py-0.5 rounded-full bg-[var(--admin-error-subtle)] text-[var(--admin-text)]">
|
|
{t("statusExpired")}
|
|
</span>
|
|
) : v.status === "usedUp" ? (
|
|
<span className="inline-block text-[0.72rem] font-bold px-2 py-0.5 rounded-full bg-[var(--admin-accent)]/18 text-[var(--admin-text)]">
|
|
{t("statusUsedUp")}
|
|
</span>
|
|
) : (
|
|
<span className="inline-block text-[0.72rem] font-bold px-2 py-0.5 rounded-full bg-[var(--admin-success-subtle)] text-[var(--admin-text)]">
|
|
{t("statusActive")}
|
|
</span>
|
|
)}
|
|
</td>
|
|
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
{formatDate(v.expiresAt ? new Date(v.expiresAt) : null)}
|
|
</td>
|
|
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
{formatDate(v.createdAt ? new Date(v.createdAt) : null)}
|
|
</td>
|
|
{canEdit && (
|
|
<td>
|
|
<Button
|
|
type="button"
|
|
variant="destructive"
|
|
disabled={isPending}
|
|
onClick={() => setPendingDelete(v)}
|
|
>
|
|
{t("delete")}
|
|
</Button>
|
|
</td>
|
|
)}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
)}
|
|
</div>
|
|
</section>
|
|
|
|
<ConfirmDialog
|
|
open={pendingDelete !== null}
|
|
onOpenChange={(open) => {
|
|
if (!open) setPendingDelete(null);
|
|
}}
|
|
title={t("confirmDeleteTitle")}
|
|
description={
|
|
pendingDelete ? t("confirmDelete", { code: pendingDelete.code }) : ""
|
|
}
|
|
confirmLabel={t("delete")}
|
|
cancelLabel={t("form.cancel")}
|
|
loading={isPending}
|
|
onConfirm={handleConfirmDelete}
|
|
/>
|
|
</main>
|
|
);
|
|
}
|