Improve shop hub: CurrencyIcon on packages/vouchers, DataTable for transactions.
Local Build and Deploy / deploy (push) Successful in 1m3s
Local Build and Deploy / deploy (push) Successful in 1m3s
Co-authored-by: Cursor <[email protected]>
This commit is contained in:
1 parent
a2ca9b3c23
commit
cbdea70f83
8 files changed
+657
-349
No files matched your search
@@ -1,30 +1,42 @@
|
|||||||
"use server";
|
"use server";
|
||||||
|
|
||||||
import { revalidatePath } from "next/cache";
|
import { revalidatePath } from "next/cache";
|
||||||
|
import { positiveBigInt } from "@/lib/api";
|
||||||
import { requirePermission } from "@/lib/admin/guard";
|
import { requirePermission } from "@/lib/admin/guard";
|
||||||
import { PERMS } from "@/lib/permissions";
|
import { PERMS } from "@/lib/permissions";
|
||||||
import { formPositiveBigInt } from "@/lib/form-data";
|
|
||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
|
import {
|
||||||
|
actionError,
|
||||||
|
actionOk,
|
||||||
|
type ActionResult,
|
||||||
|
} from "@/lib/safe-action-shared";
|
||||||
import { logServerError } from "@/lib/server-log";
|
import { logServerError } from "@/lib/server-log";
|
||||||
|
|
||||||
export async function createVoucher(formData: FormData): Promise<void> {
|
export async function createVoucher(input: {
|
||||||
|
code: string;
|
||||||
|
amount: number;
|
||||||
|
maxUses: number;
|
||||||
|
expiresAt?: string;
|
||||||
|
}): Promise<ActionResult<{ id: string }>> {
|
||||||
await requirePermission(PERMS.SHOP_EDIT);
|
await requirePermission(PERMS.SHOP_EDIT);
|
||||||
|
|
||||||
const code = String(formData.get("code") ?? "")
|
const code = String(input.code ?? "")
|
||||||
.normalize("NFC")
|
.normalize("NFC")
|
||||||
.trim()
|
.trim()
|
||||||
.slice(0, 255);
|
.slice(0, 255);
|
||||||
const amount = Number(formData.get("amount"));
|
const amount = Number(input.amount);
|
||||||
const maxUsesRaw = Number(formData.get("maxUses"));
|
const maxUsesRaw = Number(input.maxUses);
|
||||||
const maxUses =
|
const maxUses =
|
||||||
Number.isFinite(maxUsesRaw) && maxUsesRaw > 0 ? Math.floor(maxUsesRaw) : 1;
|
Number.isFinite(maxUsesRaw) && maxUsesRaw > 0 ? Math.floor(maxUsesRaw) : 1;
|
||||||
|
|
||||||
if (!code || !(amount > 0)) return;
|
if (!code || !(amount > 0)) {
|
||||||
|
return actionError("Code and a positive amount are required");
|
||||||
|
}
|
||||||
|
|
||||||
const expiresRaw = String(formData.get("expiresAt") ?? "")
|
let expiresAt: Date | null = null;
|
||||||
|
const expiresRaw = String(input.expiresAt ?? "")
|
||||||
.normalize("NFC")
|
.normalize("NFC")
|
||||||
.trim();
|
.trim();
|
||||||
let expiresAt: Date | null = null;
|
|
||||||
if (expiresRaw) {
|
if (expiresRaw) {
|
||||||
const parsed = new Date(expiresRaw);
|
const parsed = new Date(expiresRaw);
|
||||||
if (!Number.isNaN(parsed.getTime())) expiresAt = parsed;
|
if (!Number.isNaN(parsed.getTime())) expiresAt = parsed;
|
||||||
@@ -33,7 +45,7 @@ export async function createVoucher(formData: FormData): Promise<void> {
|
|||||||
const now = new Date();
|
const now = new Date();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await prisma.websiteShopVouchers.create({
|
const created = await prisma.websiteShopVouchers.create({
|
||||||
data: {
|
data: {
|
||||||
code,
|
code,
|
||||||
amount: Math.floor(amount),
|
amount: Math.floor(amount),
|
||||||
@@ -44,29 +56,30 @@ export async function createVoucher(formData: FormData): Promise<void> {
|
|||||||
updatedAt: now,
|
updatedAt: now,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
revalidatePath("/admin/vouchers");
|
||||||
|
return actionOk({ id: String(created.id) });
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logServerError("admin.voucher_create_failed", error);
|
logServerError("admin.voucher_create_failed", error);
|
||||||
// Unique constraint on `code` (or DB unavailable) — swallow and re-render.
|
return actionError("Could not create voucher (code may already exist)");
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidatePath("/admin/vouchers");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function deleteVoucher(formData: FormData): Promise<void> {
|
export async function deleteVoucher(input: {
|
||||||
|
id: string;
|
||||||
|
}): Promise<ActionResult> {
|
||||||
await requirePermission(PERMS.SHOP_EDIT);
|
await requirePermission(PERMS.SHOP_EDIT);
|
||||||
|
|
||||||
const id = formPositiveBigInt(formData, "id");
|
const id = positiveBigInt(String(input.id ?? "").trim());
|
||||||
if (!id) return;
|
if (!id) return actionError("Missing voucher id");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await prisma.websiteShopVouchers.delete({ where: { id } });
|
await prisma.websiteShopVouchers.delete({ where: { id } });
|
||||||
|
revalidatePath("/admin/vouchers");
|
||||||
|
return actionOk();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
logServerError("admin.voucher_delete_failed", error, {
|
logServerError("admin.voucher_delete_failed", error, {
|
||||||
voucherId: String(id),
|
voucherId: String(id),
|
||||||
});
|
});
|
||||||
return;
|
return actionError("Could not delete voucher");
|
||||||
}
|
}
|
||||||
|
|
||||||
revalidatePath("/admin/vouchers");
|
|
||||||
}
|
}
|
||||||
+41
-14
@@ -2,6 +2,7 @@ import Link from "next/link";
|
|||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { getTranslations } from "next-intl/server";
|
import { getTranslations } from "next-intl/server";
|
||||||
import { StatusCard } from "@/components/admin/dashboard";
|
import { StatusCard } from "@/components/admin/dashboard";
|
||||||
|
import { CurrencyIcon } from "@/components/shared/currency-icon";
|
||||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||||
import { prisma } from "@/lib/prisma";
|
import { prisma } from "@/lib/prisma";
|
||||||
|
|
||||||
@@ -26,6 +27,24 @@ function statusState(
|
|||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function CurrencyCell({
|
||||||
|
kind,
|
||||||
|
value,
|
||||||
|
}: {
|
||||||
|
kind: "credits" | "duckets" | "diamonds";
|
||||||
|
value: number | null | undefined;
|
||||||
|
}) {
|
||||||
|
if (value == null) {
|
||||||
|
return <span className="muted">—</span>;
|
||||||
|
}
|
||||||
|
return (
|
||||||
|
<span className="inline-flex items-center gap-1.5 whitespace-nowrap">
|
||||||
|
<CurrencyIcon kind={kind} size={14} />
|
||||||
|
{value}
|
||||||
|
</span>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
export default async function AdminShop() {
|
export default async function AdminShop() {
|
||||||
const { session, permissions } = await getAdminContext();
|
const { session, permissions } = await getAdminContext();
|
||||||
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
||||||
@@ -64,7 +83,7 @@ export default async function AdminShop() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
<div style={{ display: "flex", alignItems: "center", justifyContent: "flex-end", gap: "1rem" }}>
|
<div className="flex items-center justify-end gap-4">
|
||||||
<Link href="/admin/shop/new" className="btn btn-primary">
|
<Link href="/admin/shop/new" className="btn btn-primary">
|
||||||
{t("newPackage")}
|
{t("newPackage")}
|
||||||
</Link>
|
</Link>
|
||||||
@@ -82,13 +101,13 @@ export default async function AdminShop() {
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<section className="mt-6">
|
<section className="mt-6">
|
||||||
<h2>
|
<h2 className="admin-section-title">
|
||||||
{t("packages")} ({packages.length})
|
{t("packages")} ({packages.length})
|
||||||
</h2>
|
</h2>
|
||||||
{packages.length === 0 ? (
|
{packages.length === 0 ? (
|
||||||
<p className="muted">{t("noPackages")}</p>
|
<div className="admin-empty">{t("noPackages")}</div>
|
||||||
) : (
|
) : (
|
||||||
<div style={{ overflowX: "auto" }}>
|
<div className="admin-card overflow-x-auto" style={{ padding: 0 }}>
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -110,9 +129,15 @@ export default async function AdminShop() {
|
|||||||
<Link href={`/admin/shop/${p.id}`}>{p.name}</Link>
|
<Link href={`/admin/shop/${p.id}`}>{p.name}</Link>
|
||||||
</td>
|
</td>
|
||||||
<td>{p.costs}</td>
|
<td>{p.costs}</td>
|
||||||
<td className="muted">{p.credits ?? "—"}</td>
|
<td>
|
||||||
<td className="muted">{p.duckets ?? "—"}</td>
|
<CurrencyCell kind="credits" value={p.credits} />
|
||||||
<td className="muted">{p.diamonds ?? "—"}</td>
|
</td>
|
||||||
|
<td>
|
||||||
|
<CurrencyCell kind="duckets" value={p.duckets} />
|
||||||
|
</td>
|
||||||
|
<td>
|
||||||
|
<CurrencyCell kind="diamonds" value={p.diamonds} />
|
||||||
|
</td>
|
||||||
<td className="muted">{p.giveRank ?? "—"}</td>
|
<td className="muted">{p.giveRank ?? "—"}</td>
|
||||||
<td className="muted">{p.position}</td>
|
<td className="muted">{p.position}</td>
|
||||||
</tr>
|
</tr>
|
||||||
@@ -124,16 +149,14 @@ export default async function AdminShop() {
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section className="mt-6">
|
<section className="mt-6">
|
||||||
<h2>
|
<h2 className="admin-section-title">
|
||||||
{t("recentOrders")} ({orders.length})
|
{t("recentOrders")} ({orders.length})
|
||||||
</h2>
|
</h2>
|
||||||
<p className="muted" style={{ marginTop: "-0.4rem" }}>
|
<p className="muted -mt-1 mb-3 text-sm">{t("ordersHint")}</p>
|
||||||
{t("ordersHint")} (<code>website_paypal_transactions</code>).
|
|
||||||
</p>
|
|
||||||
{orders.length === 0 ? (
|
{orders.length === 0 ? (
|
||||||
<p className="muted">{t("noOrders")}</p>
|
<div className="admin-empty">{t("noOrders")}</div>
|
||||||
) : (
|
) : (
|
||||||
<div style={{ overflowX: "auto" }}>
|
<div className="admin-card overflow-x-auto" style={{ padding: 0 }}>
|
||||||
<table>
|
<table>
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@@ -153,7 +176,11 @@ export default async function AdminShop() {
|
|||||||
<td>
|
<td>
|
||||||
<code>{o.transactionId}</code>
|
<code>{o.transactionId}</code>
|
||||||
</td>
|
</td>
|
||||||
<td className="muted">{o.userId}</td>
|
<td className="muted">
|
||||||
|
<Link href={`/admin/users/${o.userId}`}>
|
||||||
|
#{o.userId}
|
||||||
|
</Link>
|
||||||
|
</td>
|
||||||
<td className="muted">{o.description ?? "—"}</td>
|
<td className="muted">{o.description ?? "—"}</td>
|
||||||
<td>
|
<td>
|
||||||
{o.amount} {o.currency}
|
{o.amount} {o.currency}
|
||||||
|
|||||||
+128
-122
@@ -1,35 +1,35 @@
|
|||||||
import Link from "next/link";
|
|
||||||
import { getTranslations } from "next-intl/server";
|
import { getTranslations } from "next-intl/server";
|
||||||
import { StatusCard } from "@/components/admin/dashboard";
|
|
||||||
import { prisma } from "@/lib/prisma";
|
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
|
import type { Prisma } from "@/generated/prisma/client";
|
||||||
|
import { StatusCard } from "@/components/admin/dashboard";
|
||||||
|
import { calcPagination, parseListParams } from "@/lib/admin-helpers";
|
||||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
import {
|
||||||
|
TransactionsTable,
|
||||||
|
type TransactionRow,
|
||||||
|
} from "./transactions-table";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
const PER_PAGE = 25;
|
const PAID_STATUS = ["completed", "paid", "success", "approved"] as const;
|
||||||
|
const FAILED_STATUS = [
|
||||||
type Txn = Awaited<
|
|
||||||
ReturnType<typeof prisma.websitePaypalTransactions.findMany>
|
|
||||||
>[number];
|
|
||||||
|
|
||||||
const PAID_STATUS = new Set(["completed", "paid", "success", "approved"]);
|
|
||||||
const FAILED_STATUS = new Set([
|
|
||||||
"failed",
|
"failed",
|
||||||
"denied",
|
"denied",
|
||||||
"refunded",
|
"refunded",
|
||||||
"cancelled",
|
"cancelled",
|
||||||
"canceled",
|
"canceled",
|
||||||
]);
|
] as const;
|
||||||
|
const PENDING_STATUS = ["pending", "created", "processing"] as const;
|
||||||
|
|
||||||
function statusState(
|
function statusKind(
|
||||||
status: string | null | undefined,
|
status: string | null | undefined,
|
||||||
): "ok" | "danger" | undefined {
|
): TransactionRow["statusKind"] {
|
||||||
if (!status) return undefined;
|
if (!status) return "neutral";
|
||||||
const s = status.toLowerCase();
|
const s = status.toLowerCase();
|
||||||
if (PAID_STATUS.has(s)) return "ok";
|
if ((PAID_STATUS as readonly string[]).includes(s)) return "ok";
|
||||||
if (FAILED_STATUS.has(s)) return "danger";
|
if ((FAILED_STATUS as readonly string[]).includes(s)) return "danger";
|
||||||
return undefined;
|
return "neutral";
|
||||||
}
|
}
|
||||||
|
|
||||||
function fromDate(d: Date | null | undefined): string {
|
function fromDate(d: Date | null | undefined): string {
|
||||||
@@ -42,10 +42,22 @@ function formatAmount(amount: number, currency: string): string {
|
|||||||
return `${value} ${currency}`;
|
return `${value} ${currency}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const SORT_MAP: Record<
|
||||||
|
string,
|
||||||
|
Prisma.WebsitePaypalTransactionsOrderByWithRelationInput
|
||||||
|
> = {
|
||||||
|
id: { id: "desc" },
|
||||||
|
amount: { amount: "desc" },
|
||||||
|
date: { createdAt: "desc" },
|
||||||
|
status: { status: "desc" },
|
||||||
|
package: { description: "desc" },
|
||||||
|
payer: { userId: "desc" },
|
||||||
|
};
|
||||||
|
|
||||||
export default async function AdminTransactions({
|
export default async function AdminTransactions({
|
||||||
searchParams,
|
searchParams,
|
||||||
}: {
|
}: {
|
||||||
searchParams: Promise<{ page?: string }>;
|
searchParams: Promise<Record<string, string>>;
|
||||||
}) {
|
}) {
|
||||||
const { session, permissions } = await getAdminContext();
|
const { session, permissions } = await getAdminContext();
|
||||||
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
||||||
@@ -53,46 +65,106 @@ export default async function AdminTransactions({
|
|||||||
}
|
}
|
||||||
|
|
||||||
const t = await getTranslations("pages.admin.transactions");
|
const t = await getTranslations("pages.admin.transactions");
|
||||||
|
const raw = await searchParams;
|
||||||
|
const parsed = parseListParams(new URLSearchParams(raw));
|
||||||
|
const statusFilter = raw.filter_status ?? "";
|
||||||
|
|
||||||
const sp = await searchParams;
|
const where: Prisma.WebsitePaypalTransactionsWhereInput = {};
|
||||||
const page = Math.max(1, Number(sp.page ?? "1") || 1);
|
|
||||||
|
if (statusFilter === "paid") {
|
||||||
|
where.status = { in: [...PAID_STATUS] };
|
||||||
|
} else if (statusFilter === "failed") {
|
||||||
|
where.status = { in: [...FAILED_STATUS] };
|
||||||
|
} else if (statusFilter === "pending") {
|
||||||
|
where.status = { in: [...PENDING_STATUS] };
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsed.search.trim()) {
|
||||||
|
const q = parsed.search.trim();
|
||||||
|
const asId = Number(q);
|
||||||
|
let userIds: number[] = [];
|
||||||
|
try {
|
||||||
|
const users = await prisma.user.findMany({
|
||||||
|
where: { username: { contains: q } },
|
||||||
|
select: { id: true },
|
||||||
|
take: 50,
|
||||||
|
});
|
||||||
|
userIds = users.map((u) => u.id);
|
||||||
|
} catch {
|
||||||
|
userIds = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
where.AND = [
|
||||||
|
...(where.status ? [{ status: where.status }] : []),
|
||||||
|
{
|
||||||
|
OR: [
|
||||||
|
{ transactionId: { contains: q } },
|
||||||
|
{ description: { contains: q } },
|
||||||
|
...(Number.isFinite(asId) && asId > 0 ? [{ userId: asId }] : []),
|
||||||
|
...(userIds.length ? [{ userId: { in: userIds } }] : []),
|
||||||
|
],
|
||||||
|
},
|
||||||
|
];
|
||||||
|
delete where.status;
|
||||||
|
}
|
||||||
|
|
||||||
let transactions: Txn[] = [];
|
|
||||||
let total = 0;
|
let total = 0;
|
||||||
let names = new Map<number, string>();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
[transactions, total] = await Promise.all([
|
total = await prisma.websitePaypalTransactions.count({ where });
|
||||||
prisma.websitePaypalTransactions.findMany({
|
} catch {
|
||||||
orderBy: { id: "desc" },
|
total = 0;
|
||||||
skip: (page - 1) * PER_PAGE,
|
}
|
||||||
take: PER_PAGE,
|
|
||||||
}),
|
|
||||||
prisma.websitePaypalTransactions.count(),
|
|
||||||
]);
|
|
||||||
|
|
||||||
const userIds = [...new Set(transactions.map((t) => t.userId))];
|
const pagination = calcPagination(total, parsed.page, parsed.perPage);
|
||||||
if (userIds.length > 0) {
|
const sortKey = parsed.sort ?? "id";
|
||||||
|
const baseOrder = SORT_MAP[sortKey] ?? { id: "desc" as const };
|
||||||
|
const orderField = Object.keys(baseOrder)[0] as keyof typeof baseOrder;
|
||||||
|
const orderBy = {
|
||||||
|
[orderField]: parsed.order,
|
||||||
|
} as Prisma.WebsitePaypalTransactionsOrderByWithRelationInput;
|
||||||
|
|
||||||
|
let transactions: Awaited<
|
||||||
|
ReturnType<typeof prisma.websitePaypalTransactions.findMany>
|
||||||
|
> = [];
|
||||||
|
try {
|
||||||
|
transactions = await prisma.websitePaypalTransactions.findMany({
|
||||||
|
where,
|
||||||
|
orderBy,
|
||||||
|
skip: pagination.offset,
|
||||||
|
take: pagination.perPage,
|
||||||
|
});
|
||||||
|
} catch {
|
||||||
|
transactions = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
const userIds = [...new Set(transactions.map((x) => x.userId))];
|
||||||
|
let names = new Map<number, string>();
|
||||||
|
if (userIds.length > 0) {
|
||||||
|
try {
|
||||||
const users = await prisma.user.findMany({
|
const users = await prisma.user.findMany({
|
||||||
where: { id: { in: userIds } },
|
where: { id: { in: userIds } },
|
||||||
select: { id: true, username: true },
|
select: { id: true, username: true },
|
||||||
});
|
});
|
||||||
names = new Map(users.map((u) => [u.id, u.username]));
|
names = new Map(users.map((u) => [u.id, u.username]));
|
||||||
|
} catch {
|
||||||
|
names = new Map();
|
||||||
}
|
}
|
||||||
} catch {
|
|
||||||
transactions = [];
|
|
||||||
total = 0;
|
|
||||||
names = new Map();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const pages = Math.max(1, Math.ceil(total / PER_PAGE));
|
const rows: TransactionRow[] = transactions.map((x) => ({
|
||||||
|
id: Number(x.id),
|
||||||
|
payer: names.get(x.userId) ?? `#${x.userId}`,
|
||||||
|
payerId: x.userId,
|
||||||
|
amount: formatAmount(x.amount, x.currency),
|
||||||
|
package: x.description ?? "—",
|
||||||
|
status: x.status ?? "",
|
||||||
|
statusKind: statusKind(x.status),
|
||||||
|
transactionId: x.transactionId,
|
||||||
|
date: fromDate(x.createdAt),
|
||||||
|
}));
|
||||||
|
|
||||||
const pagePaid = transactions.filter(
|
const pagePaid = rows.filter((r) => r.statusKind === "ok").length;
|
||||||
(t) => statusState(t.status) === "ok",
|
const pageFailed = rows.filter((r) => r.statusKind === "danger").length;
|
||||||
).length;
|
|
||||||
const pageFailed = transactions.filter(
|
|
||||||
(t) => statusState(t.status) === "danger",
|
|
||||||
).length;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
@@ -113,87 +185,21 @@ export default async function AdminTransactions({
|
|||||||
</div>
|
</div>
|
||||||
|
|
||||||
<section className="mt-6">
|
<section className="mt-6">
|
||||||
<h2 className="admin-section-title">
|
<h2 className="admin-section-title">{t("title")}</h2>
|
||||||
{t("title")} ({total})
|
{total === 0 && !parsed.search && !statusFilter ? (
|
||||||
</h2>
|
|
||||||
{transactions.length === 0 ? (
|
|
||||||
<div className="admin-empty">{t("noTransactions")}</div>
|
<div className="admin-empty">{t("noTransactions")}</div>
|
||||||
) : (
|
) : (
|
||||||
<div className="overflow-x-auto">
|
<TransactionsTable
|
||||||
<table>
|
data={{
|
||||||
<thead>
|
rows,
|
||||||
<tr>
|
total,
|
||||||
<th>{t("colId")}</th>
|
page: pagination.page,
|
||||||
<th>{t("colPayer")}</th>
|
perPage: pagination.perPage,
|
||||||
<th>{t("colAmount")}</th>
|
lastPage: pagination.lastPage,
|
||||||
<th>{t("colPackage")}</th>
|
}}
|
||||||
<th>{t("colStatus")}</th>
|
/>
|
||||||
<th>{t("colTransaction")}</th>
|
|
||||||
<th>{t("colDate")}</th>
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{transactions.map((x) => {
|
|
||||||
const state = statusState(x.status);
|
|
||||||
return (
|
|
||||||
<tr key={String(x.id)}>
|
|
||||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
||||||
{String(x.id)}
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<Link href={`/admin/users/${x.userId}`}>
|
|
||||||
{names.get(x.userId) ?? `#${x.userId}`}
|
|
||||||
</Link>
|
|
||||||
</td>
|
|
||||||
<td>{formatAmount(x.amount, x.currency)}</td>
|
|
||||||
<td>{x.description ?? "—"}</td>
|
|
||||||
<td>
|
|
||||||
{x.status ? (
|
|
||||||
<span
|
|
||||||
className={`inline-block text-[0.72rem] font-bold px-2 py-0.5 rounded-full ${state === "ok" ? "bg-[var(--admin-success-subtle)] text-[var(--admin-text)]" : state === "danger" ? "bg-[var(--admin-error-subtle)] text-[var(--admin-text)]" : "bg-[var(--admin-accent)]/18 text-[var(--admin-text)]"}`}
|
|
||||||
>
|
|
||||||
{x.status}
|
|
||||||
</span>
|
|
||||||
) : (
|
|
||||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
||||||
—
|
|
||||||
</span>
|
|
||||||
)}
|
|
||||||
</td>
|
|
||||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
||||||
{x.transactionId}
|
|
||||||
</td>
|
|
||||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
||||||
{fromDate(x.createdAt)}
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
</div>
|
|
||||||
)}
|
)}
|
||||||
</section>
|
</section>
|
||||||
|
|
||||||
<p className="text-xs theme-text-muted dark:theme-text-muted">
|
|
||||||
{t("pagination", { page, pages, total })}
|
|
||||||
{page > 1 ? (
|
|
||||||
<>
|
|
||||||
{" · "}
|
|
||||||
<Link href={`/admin/transactions?page=${page - 1}`}>
|
|
||||||
← {t("prev")}
|
|
||||||
</Link>
|
|
||||||
</>
|
|
||||||
) : null}
|
|
||||||
{page < pages ? (
|
|
||||||
<>
|
|
||||||
{" · "}
|
|
||||||
<Link href={`/admin/transactions?page=${page + 1}`}>
|
|
||||||
{t("next")} →
|
|
||||||
</Link>
|
|
||||||
</>
|
|
||||||
) : null}
|
|
||||||
</p>
|
|
||||||
</main>
|
</main>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,90 @@
|
|||||||
|
"use client";
|
||||||
|
|
||||||
|
import Link from "next/link";
|
||||||
|
import { useTranslations } from "next-intl";
|
||||||
|
import { DataTable } from "@/components/admin/data-table";
|
||||||
|
import { Badge } from "@/components/ui/badge";
|
||||||
|
import type { DataTableColumn, PaginatedResult } from "@/types";
|
||||||
|
|
||||||
|
export interface TransactionRow {
|
||||||
|
id: number;
|
||||||
|
payer: string;
|
||||||
|
payerId: number;
|
||||||
|
amount: string;
|
||||||
|
package: string;
|
||||||
|
status: string;
|
||||||
|
statusKind: "ok" | "danger" | "neutral";
|
||||||
|
transactionId: string;
|
||||||
|
date: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
function statusVariant(
|
||||||
|
kind: TransactionRow["statusKind"],
|
||||||
|
): "default" | "destructive" | "secondary" {
|
||||||
|
if (kind === "ok") return "default";
|
||||||
|
if (kind === "danger") return "destructive";
|
||||||
|
return "secondary";
|
||||||
|
}
|
||||||
|
|
||||||
|
export function TransactionsTable({
|
||||||
|
data,
|
||||||
|
}: {
|
||||||
|
data: PaginatedResult<TransactionRow>;
|
||||||
|
}) {
|
||||||
|
const t = useTranslations("pages.admin.transactions");
|
||||||
|
|
||||||
|
const columns: DataTableColumn<TransactionRow>[] = [
|
||||||
|
{ key: "id", label: t("colId"), sortable: true },
|
||||||
|
{
|
||||||
|
key: "payer",
|
||||||
|
label: t("colPayer"),
|
||||||
|
sortable: true,
|
||||||
|
render: (_value, row) => (
|
||||||
|
<Link
|
||||||
|
href={`/admin/users/${row.payerId}`}
|
||||||
|
className="font-medium hover:underline"
|
||||||
|
>
|
||||||
|
{row.payer}
|
||||||
|
</Link>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{ key: "amount", label: t("colAmount"), sortable: true },
|
||||||
|
{ key: "package", label: t("colPackage"), sortable: true },
|
||||||
|
{
|
||||||
|
key: "status",
|
||||||
|
label: t("colStatus"),
|
||||||
|
sortable: true,
|
||||||
|
filterKey: "filter_status",
|
||||||
|
filterOptions: [
|
||||||
|
{ label: t("statusCompleted"), value: "paid" },
|
||||||
|
{ label: t("statusPending"), value: "pending" },
|
||||||
|
{ label: t("statusFailed"), value: "failed" },
|
||||||
|
],
|
||||||
|
render: (_value, row) =>
|
||||||
|
row.status ? (
|
||||||
|
<Badge
|
||||||
|
variant={statusVariant(row.statusKind)}
|
||||||
|
className="font-mono text-[0.7rem]"
|
||||||
|
>
|
||||||
|
{row.status}
|
||||||
|
</Badge>
|
||||||
|
) : (
|
||||||
|
<span className="text-muted-foreground">—</span>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
{ key: "transactionId", label: t("colTransaction") },
|
||||||
|
{ key: "date", label: t("colDate"), sortable: true },
|
||||||
|
];
|
||||||
|
|
||||||
|
return (
|
||||||
|
<DataTable
|
||||||
|
data={data}
|
||||||
|
columns={columns}
|
||||||
|
searchPlaceholder={t("searchPlaceholder")}
|
||||||
|
presets={[
|
||||||
|
{ label: t("presetPaid"), params: { filter_status: "paid" } },
|
||||||
|
{ label: t("presetFailed"), params: { filter_status: "failed" } },
|
||||||
|
]}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
}
|
||||||
+32
-194
@@ -1,39 +1,22 @@
|
|||||||
import { getTranslations } from "next-intl/server";
|
|
||||||
import { createVoucher, deleteVoucher } from "@/actions/admin-vouchers";
|
|
||||||
import { StatusCard } from "@/components/admin/dashboard";
|
|
||||||
import { prisma } from "@/lib/prisma";
|
|
||||||
import { redirect } from "next/navigation";
|
import { redirect } from "next/navigation";
|
||||||
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
import { canAccess, getAdminContext, PERMS } from "@/lib/permissions";
|
||||||
|
import { prisma } from "@/lib/prisma";
|
||||||
|
import { VouchersClient, type VoucherRow } from "./vouchers-client";
|
||||||
|
|
||||||
export const dynamic = "force-dynamic";
|
export const dynamic = "force-dynamic";
|
||||||
|
|
||||||
type VoucherRow = {
|
|
||||||
id: bigint;
|
|
||||||
code: string;
|
|
||||||
amount: number;
|
|
||||||
maxUses: number;
|
|
||||||
useCount: number;
|
|
||||||
expiresAt: Date | null;
|
|
||||||
createdAt: Date | null;
|
|
||||||
};
|
|
||||||
|
|
||||||
function fmtDate(d: Date | null): string {
|
|
||||||
if (!d) return "—";
|
|
||||||
return d.toISOString().slice(0, 16).replace("T", " ");
|
|
||||||
}
|
|
||||||
|
|
||||||
export default async function AdminVouchers() {
|
export default async function AdminVouchers() {
|
||||||
const { session, permissions } = await getAdminContext();
|
const { session, permissions } = await getAdminContext();
|
||||||
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
if (!canAccess(permissions, PERMS.SHOP_VIEW, session.user.rank)) {
|
||||||
redirect("/admin");
|
redirect("/admin");
|
||||||
}
|
}
|
||||||
|
|
||||||
const t = await getTranslations("pages.admin.vouchers");
|
const canEdit = canAccess(permissions, PERMS.SHOP_EDIT, session.user.rank);
|
||||||
const now = Date.now();
|
const now = Date.now();
|
||||||
|
|
||||||
let vouchers: VoucherRow[] = [];
|
let rows: VoucherRow[] = [];
|
||||||
try {
|
try {
|
||||||
vouchers = await prisma.websiteShopVouchers.findMany({
|
const vouchers = await prisma.websiteShopVouchers.findMany({
|
||||||
select: {
|
select: {
|
||||||
id: true,
|
id: true,
|
||||||
code: true,
|
code: true,
|
||||||
@@ -46,182 +29,37 @@ export default async function AdminVouchers() {
|
|||||||
orderBy: { id: "desc" },
|
orderBy: { id: "desc" },
|
||||||
take: 200,
|
take: 200,
|
||||||
});
|
});
|
||||||
|
rows = vouchers.map((v) => {
|
||||||
|
const expired = v.expiresAt ? v.expiresAt.getTime() <= now : false;
|
||||||
|
const usedUp = v.maxUses > 0 && v.useCount >= v.maxUses;
|
||||||
|
const status: VoucherRow["status"] = expired
|
||||||
|
? "expired"
|
||||||
|
: usedUp
|
||||||
|
? "usedUp"
|
||||||
|
: "active";
|
||||||
|
return {
|
||||||
|
id: String(v.id),
|
||||||
|
code: v.code,
|
||||||
|
amount: v.amount,
|
||||||
|
maxUses: v.maxUses,
|
||||||
|
useCount: v.useCount,
|
||||||
|
expiresAt: v.expiresAt?.toISOString() ?? null,
|
||||||
|
createdAt: v.createdAt?.toISOString() ?? null,
|
||||||
|
status,
|
||||||
|
};
|
||||||
|
});
|
||||||
} catch {
|
} catch {
|
||||||
vouchers = [];
|
rows = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
const active = vouchers.filter((v) => {
|
const stats = {
|
||||||
const expired = v.expiresAt ? v.expiresAt.getTime() <= now : false;
|
total: rows.length,
|
||||||
const usedUp = v.maxUses > 0 && v.useCount >= v.maxUses;
|
active: rows.filter((v) => v.status === "active").length,
|
||||||
return !expired && !usedUp;
|
usedUp: rows.filter((v) => v.status === "usedUp").length,
|
||||||
}).length;
|
expired: rows.filter((v) => v.status === "expired").length,
|
||||||
const expiredCount = vouchers.filter(
|
};
|
||||||
(v) => v.expiresAt && v.expiresAt.getTime() <= now,
|
|
||||||
).length;
|
|
||||||
const usedUpCount = vouchers.filter(
|
|
||||||
(v) => v.maxUses > 0 && v.useCount >= v.maxUses,
|
|
||||||
).length;
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<VouchersClient vouchers={rows} canEdit={canEdit} stats={stats} />
|
||||||
<div className="grid grid-cols-[repeat(auto-fit,minmax(180px,1fr))] gap-3.5 mb-6">
|
|
||||||
<StatusCard label={t("vouchers")} value={vouchers.length} icon="🎟️" />
|
|
||||||
<StatusCard
|
|
||||||
label={t("active")}
|
|
||||||
value={active}
|
|
||||||
state={active > 0 ? "ok" : "neutral"}
|
|
||||||
icon="✅"
|
|
||||||
/>
|
|
||||||
<StatusCard
|
|
||||||
label={t("usedUp")}
|
|
||||||
value={usedUpCount}
|
|
||||||
state={usedUpCount > 0 ? "warn" : "neutral"}
|
|
||||||
icon="📉"
|
|
||||||
/>
|
|
||||||
<StatusCard
|
|
||||||
label={t("expired")}
|
|
||||||
value={expiredCount}
|
|
||||||
state={expiredCount > 0 ? "danger" : "neutral"}
|
|
||||||
icon="⏰"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<form action={createVoucher} className="admin-card mb-6">
|
|
||||||
<h3 style={{ marginTop: 0 }}>{t("newVoucher")}</h3>
|
|
||||||
<div
|
|
||||||
className="grid grid-cols-1 gap-4 md:grid-cols-2"
|
|
||||||
style={{ marginBottom: "0.75rem" }}
|
|
||||||
>
|
|
||||||
<label
|
|
||||||
style={{ display: "flex", flexDirection: "column", gap: "0.3rem" }}
|
|
||||||
>
|
|
||||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
||||||
{t("form.code")}
|
|
||||||
</span>
|
|
||||||
<input
|
|
||||||
name="code"
|
|
||||||
placeholder={t("form.codePlaceholder")}
|
|
||||||
maxLength={255}
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
style={{ display: "flex", flexDirection: "column", gap: "0.3rem" }}
|
|
||||||
>
|
|
||||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
||||||
{t("form.amount")}
|
|
||||||
</span>
|
|
||||||
<input
|
|
||||||
name="amount"
|
|
||||||
type="number"
|
|
||||||
min={1}
|
|
||||||
placeholder={t("form.amountPlaceholder")}
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
style={{ display: "flex", flexDirection: "column", gap: "0.3rem" }}
|
|
||||||
>
|
|
||||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
||||||
{t("form.maxUses")}
|
|
||||||
</span>
|
|
||||||
<input
|
|
||||||
name="maxUses"
|
|
||||||
type="number"
|
|
||||||
min={1}
|
|
||||||
defaultValue={1}
|
|
||||||
required
|
|
||||||
/>
|
|
||||||
</label>
|
|
||||||
<label
|
|
||||||
style={{ display: "flex", flexDirection: "column", gap: "0.3rem" }}
|
|
||||||
>
|
|
||||||
<span className="text-sm theme-text-muted dark:theme-text-muted">
|
|
||||||
{t("form.expiresAt")}
|
|
||||||
</span>
|
|
||||||
<input name="expiresAt" type="datetime-local" />
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
<button type="submit" className="btn btn-primary">
|
|
||||||
{t("form.create")}
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
<section className="mt-6">
|
|
||||||
<h2 className="admin-section-title">
|
|
||||||
{t("title")} ({vouchers.length})
|
|
||||||
</h2>
|
|
||||||
<div className="admin-card">
|
|
||||||
<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>
|
|
||||||
<th />
|
|
||||||
</tr>
|
|
||||||
</thead>
|
|
||||||
<tbody>
|
|
||||||
{vouchers.map((v) => {
|
|
||||||
const expired = v.expiresAt
|
|
||||||
? v.expiresAt.getTime() <= now
|
|
||||||
: false;
|
|
||||||
const usedUp = v.maxUses > 0 && v.useCount >= v.maxUses;
|
|
||||||
return (
|
|
||||||
<tr key={v.id.toString()}>
|
|
||||||
<td>
|
|
||||||
<code>{v.code}</code>
|
|
||||||
</td>
|
|
||||||
<td>{v.amount}</td>
|
|
||||||
<td>
|
|
||||||
{v.useCount} / {v.maxUses}
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
{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>
|
|
||||||
) : 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">
|
|
||||||
{fmtDate(v.expiresAt)}
|
|
||||||
</td>
|
|
||||||
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
|
||||||
{fmtDate(v.createdAt)}
|
|
||||||
</td>
|
|
||||||
<td>
|
|
||||||
<form action={deleteVoucher}>
|
|
||||||
<input
|
|
||||||
type="hidden"
|
|
||||||
name="id"
|
|
||||||
value={v.id.toString()}
|
|
||||||
/>
|
|
||||||
<button type="submit" className="btn btn-danger">
|
|
||||||
{t("delete")}
|
|
||||||
</button>
|
|
||||||
</form>
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
);
|
|
||||||
})}
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
{vouchers.length === 0 ? (
|
|
||||||
<div className="admin-empty">{t("noVouchers")}</div>
|
|
||||||
) : null}
|
|
||||||
</div>
|
|
||||||
</section>
|
|
||||||
</main>
|
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,262 @@
|
|||||||
|
"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 { useServerAction } from "@/hooks/use-server-action";
|
||||||
|
|
||||||
|
export type VoucherRow = {
|
||||||
|
id: string;
|
||||||
|
code: string;
|
||||||
|
amount: number;
|
||||||
|
maxUses: number;
|
||||||
|
useCount: number;
|
||||||
|
expiresAt: string | null;
|
||||||
|
createdAt: string | null;
|
||||||
|
status: "active" | "expired" | "usedUp";
|
||||||
|
};
|
||||||
|
|
||||||
|
function fmtDate(iso: string | null): string {
|
||||||
|
if (!iso) return "—";
|
||||||
|
return iso.slice(0, 16).replace("T", " ");
|
||||||
|
}
|
||||||
|
|
||||||
|
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"
|
||||||
|
className="btn btn-primary"
|
||||||
|
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">
|
||||||
|
{fmtDate(v.expiresAt)}
|
||||||
|
</td>
|
||||||
|
<td className="text-sm theme-text-muted dark:theme-text-muted whitespace-nowrap">
|
||||||
|
{fmtDate(v.createdAt)}
|
||||||
|
</td>
|
||||||
|
{canEdit && (
|
||||||
|
<td>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="btn btn-danger"
|
||||||
|
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>
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -1229,6 +1229,9 @@
|
|||||||
"subtitle": "Manage packages and orders",
|
"subtitle": "Manage packages and orders",
|
||||||
"packages": "Packages",
|
"packages": "Packages",
|
||||||
"orders": "Orders",
|
"orders": "Orders",
|
||||||
|
"recentOrders": "Recent orders",
|
||||||
|
"paidOrders": "Paid orders",
|
||||||
|
"ordersHint": "Latest PayPal transactions",
|
||||||
"newPackage": "+ New package",
|
"newPackage": "+ New package",
|
||||||
"edit": "Edit",
|
"edit": "Edit",
|
||||||
"delete": "Delete",
|
"delete": "Delete",
|
||||||
@@ -1239,6 +1242,11 @@
|
|||||||
"colId": "ID",
|
"colId": "ID",
|
||||||
"colName": "Name",
|
"colName": "Name",
|
||||||
"colPrice": "Price",
|
"colPrice": "Price",
|
||||||
|
"colCredits": "Credits",
|
||||||
|
"colDuckets": "Duckets",
|
||||||
|
"colDiamonds": "Diamonds",
|
||||||
|
"colRank": "Rank",
|
||||||
|
"colPosition": "Position",
|
||||||
"colCurrency": "Currency",
|
"colCurrency": "Currency",
|
||||||
"colType": "Type",
|
"colType": "Type",
|
||||||
"colSold": "Sold",
|
"colSold": "Sold",
|
||||||
@@ -1247,6 +1255,9 @@
|
|||||||
"colOrderId": "Order ID",
|
"colOrderId": "Order ID",
|
||||||
"colCustomer": "Customer",
|
"colCustomer": "Customer",
|
||||||
"colPackage": "Package",
|
"colPackage": "Package",
|
||||||
|
"colTransaction": "Transaction",
|
||||||
|
"colUser": "User",
|
||||||
|
"colDescription": "Description",
|
||||||
"colAmount": "Amount",
|
"colAmount": "Amount",
|
||||||
"colStatus": "Status",
|
"colStatus": "Status",
|
||||||
"colDate": "Date",
|
"colDate": "Date",
|
||||||
@@ -1283,12 +1294,20 @@
|
|||||||
"title": "Transactions",
|
"title": "Transactions",
|
||||||
"subtitle": "PayPal payment log",
|
"subtitle": "PayPal payment log",
|
||||||
"noTransactions": "No transactions yet",
|
"noTransactions": "No transactions yet",
|
||||||
|
"totalTransactions": "Total transactions",
|
||||||
|
"pagePaid": "Paid (this page)",
|
||||||
|
"pageFailed": "Failed (this page)",
|
||||||
|
"searchPlaceholder": "Search by user, transaction ID or package…",
|
||||||
|
"presetPaid": "Paid",
|
||||||
|
"presetFailed": "Failed",
|
||||||
"colId": "ID",
|
"colId": "ID",
|
||||||
"colUser": "User",
|
"colUser": "User",
|
||||||
|
"colPayer": "Payer",
|
||||||
"colAmount": "Amount",
|
"colAmount": "Amount",
|
||||||
"colCurrency": "Currency",
|
"colCurrency": "Currency",
|
||||||
"colPackage": "Package",
|
"colPackage": "Package",
|
||||||
"colStatus": "Status",
|
"colStatus": "Status",
|
||||||
|
"colTransaction": "Transaction ID",
|
||||||
"colDate": "Date",
|
"colDate": "Date",
|
||||||
"colActions": "Actions",
|
"colActions": "Actions",
|
||||||
"statusCompleted": "Completed",
|
"statusCompleted": "Completed",
|
||||||
@@ -1307,16 +1326,30 @@
|
|||||||
"vouchers": {
|
"vouchers": {
|
||||||
"title": "Shop Vouchers",
|
"title": "Shop Vouchers",
|
||||||
"subtitle": "Manage redeemable voucher codes",
|
"subtitle": "Manage redeemable voucher codes",
|
||||||
|
"vouchers": "Vouchers",
|
||||||
|
"active": "Active",
|
||||||
|
"usedUp": "Used up",
|
||||||
|
"expired": "Expired",
|
||||||
"newVoucher": "+ New voucher",
|
"newVoucher": "+ New voucher",
|
||||||
"edit": "Edit",
|
"edit": "Edit",
|
||||||
"delete": "Delete",
|
"delete": "Delete",
|
||||||
"noVouchers": "No vouchers yet",
|
"noVouchers": "No vouchers yet",
|
||||||
|
"confirmDeleteTitle": "Delete voucher",
|
||||||
"confirmDelete": "Delete voucher \"{code}\"?",
|
"confirmDelete": "Delete voucher \"{code}\"?",
|
||||||
|
"created": "Voucher created",
|
||||||
|
"deleted": "Voucher deleted",
|
||||||
|
"createError": "Could not create voucher",
|
||||||
|
"deleteError": "Could not delete voucher",
|
||||||
|
"statusActive": "Active",
|
||||||
|
"statusUsedUp": "Used up",
|
||||||
|
"statusExpired": "Expired",
|
||||||
"colId": "ID",
|
"colId": "ID",
|
||||||
"colCode": "Code",
|
"colCode": "Code",
|
||||||
"colValue": "Value",
|
"colValue": "Value",
|
||||||
|
"colAmount": "Amount",
|
||||||
"colType": "Type",
|
"colType": "Type",
|
||||||
"colUses": "Uses / Max",
|
"colUses": "Uses / Max",
|
||||||
|
"colStatus": "Status",
|
||||||
"colExpires": "Expires",
|
"colExpires": "Expires",
|
||||||
"colCreated": "Created",
|
"colCreated": "Created",
|
||||||
"colActions": "Actions",
|
"colActions": "Actions",
|
||||||
@@ -1324,6 +1357,8 @@
|
|||||||
"code": "Code",
|
"code": "Code",
|
||||||
"codePlaceholder": "e.g. SUMMER2025",
|
"codePlaceholder": "e.g. SUMMER2025",
|
||||||
"value": "Value",
|
"value": "Value",
|
||||||
|
"amount": "Credits amount",
|
||||||
|
"amountPlaceholder": "e.g. 100",
|
||||||
"type": "Type",
|
"type": "Type",
|
||||||
"typeCredits": "Credits",
|
"typeCredits": "Credits",
|
||||||
"typeDuckets": "Duckets",
|
"typeDuckets": "Duckets",
|
||||||
@@ -1338,6 +1373,7 @@
|
|||||||
"maxUsesPlaceholder": "0 = unlimited",
|
"maxUsesPlaceholder": "0 = unlimited",
|
||||||
"expiresAt": "Expires at",
|
"expiresAt": "Expires at",
|
||||||
"enabled": "Enabled",
|
"enabled": "Enabled",
|
||||||
|
"create": "Create voucher",
|
||||||
"save": "Save voucher",
|
"save": "Save voucher",
|
||||||
"saving": "Saving…",
|
"saving": "Saving…",
|
||||||
"cancel": "Cancel"
|
"cancel": "Cancel"
|
||||||
|
|||||||
@@ -1193,6 +1193,9 @@
|
|||||||
"subtitle": "Gestisci pacchetti e ordini",
|
"subtitle": "Gestisci pacchetti e ordini",
|
||||||
"packages": "Pacchetti",
|
"packages": "Pacchetti",
|
||||||
"orders": "Ordini",
|
"orders": "Ordini",
|
||||||
|
"recentOrders": "Ordini recenti",
|
||||||
|
"paidOrders": "Ordini pagati",
|
||||||
|
"ordersHint": "Ultime transazioni PayPal",
|
||||||
"newPackage": "+ Nuovo pacchetto",
|
"newPackage": "+ Nuovo pacchetto",
|
||||||
"edit": "Modifica",
|
"edit": "Modifica",
|
||||||
"delete": "Elimina",
|
"delete": "Elimina",
|
||||||
@@ -1203,6 +1206,11 @@
|
|||||||
"colId": "ID",
|
"colId": "ID",
|
||||||
"colName": "Nome",
|
"colName": "Nome",
|
||||||
"colPrice": "Prezzo",
|
"colPrice": "Prezzo",
|
||||||
|
"colCredits": "Crediti",
|
||||||
|
"colDuckets": "Duckets",
|
||||||
|
"colDiamonds": "Diamanti",
|
||||||
|
"colRank": "Grado",
|
||||||
|
"colPosition": "Posizione",
|
||||||
"colCurrency": "Valuta",
|
"colCurrency": "Valuta",
|
||||||
"colType": "Tipo",
|
"colType": "Tipo",
|
||||||
"colSold": "Venduti",
|
"colSold": "Venduti",
|
||||||
@@ -1211,6 +1219,9 @@
|
|||||||
"colOrderId": "ID ordine",
|
"colOrderId": "ID ordine",
|
||||||
"colCustomer": "Cliente",
|
"colCustomer": "Cliente",
|
||||||
"colPackage": "Pacchetto",
|
"colPackage": "Pacchetto",
|
||||||
|
"colTransaction": "Transazione",
|
||||||
|
"colUser": "Utente",
|
||||||
|
"colDescription": "Descrizione",
|
||||||
"colAmount": "Importo",
|
"colAmount": "Importo",
|
||||||
"colStatus": "Stato",
|
"colStatus": "Stato",
|
||||||
"colDate": "Data",
|
"colDate": "Data",
|
||||||
@@ -1247,12 +1258,20 @@
|
|||||||
"title": "Transazioni",
|
"title": "Transazioni",
|
||||||
"subtitle": "Registro pagamenti PayPal",
|
"subtitle": "Registro pagamenti PayPal",
|
||||||
"noTransactions": "Ancora nessuna transazione",
|
"noTransactions": "Ancora nessuna transazione",
|
||||||
|
"totalTransactions": "Transazioni totali",
|
||||||
|
"pagePaid": "Pagate (questa pagina)",
|
||||||
|
"pageFailed": "Fallite (questa pagina)",
|
||||||
|
"searchPlaceholder": "Cerca per utente, ID transazione o pacchetto…",
|
||||||
|
"presetPaid": "Pagate",
|
||||||
|
"presetFailed": "Fallite",
|
||||||
"colId": "ID",
|
"colId": "ID",
|
||||||
"colUser": "Utente",
|
"colUser": "Utente",
|
||||||
|
"colPayer": "Pagatore",
|
||||||
"colAmount": "Importo",
|
"colAmount": "Importo",
|
||||||
"colCurrency": "Valuta",
|
"colCurrency": "Valuta",
|
||||||
"colPackage": "Pacchetto",
|
"colPackage": "Pacchetto",
|
||||||
"colStatus": "Stato",
|
"colStatus": "Stato",
|
||||||
|
"colTransaction": "ID transazione",
|
||||||
"colDate": "Data",
|
"colDate": "Data",
|
||||||
"colActions": "Azioni",
|
"colActions": "Azioni",
|
||||||
"statusCompleted": "Completato",
|
"statusCompleted": "Completato",
|
||||||
@@ -1271,16 +1290,30 @@
|
|||||||
"vouchers": {
|
"vouchers": {
|
||||||
"title": "Buoni negozio",
|
"title": "Buoni negozio",
|
||||||
"subtitle": "Gestisci codici buono riscattabili",
|
"subtitle": "Gestisci codici buono riscattabili",
|
||||||
|
"vouchers": "Buoni",
|
||||||
|
"active": "Attivi",
|
||||||
|
"usedUp": "Esauriti",
|
||||||
|
"expired": "Scaduti",
|
||||||
"newVoucher": "+ Nuovo buono",
|
"newVoucher": "+ Nuovo buono",
|
||||||
"edit": "Modifica",
|
"edit": "Modifica",
|
||||||
"delete": "Elimina",
|
"delete": "Elimina",
|
||||||
"noVouchers": "Ancora nessun buono",
|
"noVouchers": "Ancora nessun buono",
|
||||||
|
"confirmDeleteTitle": "Elimina buono",
|
||||||
"confirmDelete": "Eliminare il buono \"{code}\"?",
|
"confirmDelete": "Eliminare il buono \"{code}\"?",
|
||||||
|
"created": "Buono creato",
|
||||||
|
"deleted": "Buono eliminato",
|
||||||
|
"createError": "Impossibile creare il buono",
|
||||||
|
"deleteError": "Impossibile eliminare il buono",
|
||||||
|
"statusActive": "Attivo",
|
||||||
|
"statusUsedUp": "Esaurito",
|
||||||
|
"statusExpired": "Scaduto",
|
||||||
"colId": "ID",
|
"colId": "ID",
|
||||||
"colCode": "Codice",
|
"colCode": "Codice",
|
||||||
"colValue": "Valore",
|
"colValue": "Valore",
|
||||||
|
"colAmount": "Importo",
|
||||||
"colType": "Tipo",
|
"colType": "Tipo",
|
||||||
"colUses": "Usi / Max",
|
"colUses": "Usi / Max",
|
||||||
|
"colStatus": "Stato",
|
||||||
"colExpires": "Scade",
|
"colExpires": "Scade",
|
||||||
"colCreated": "Creato",
|
"colCreated": "Creato",
|
||||||
"colActions": "Azioni",
|
"colActions": "Azioni",
|
||||||
@@ -1288,6 +1321,8 @@
|
|||||||
"code": "Codice",
|
"code": "Codice",
|
||||||
"codePlaceholder": "es. ESTATE2025",
|
"codePlaceholder": "es. ESTATE2025",
|
||||||
"value": "Valore",
|
"value": "Valore",
|
||||||
|
"amount": "Crediti",
|
||||||
|
"amountPlaceholder": "es. 100",
|
||||||
"type": "Tipo",
|
"type": "Tipo",
|
||||||
"typeCredits": "Crediti",
|
"typeCredits": "Crediti",
|
||||||
"typeDuckets": "Duckets",
|
"typeDuckets": "Duckets",
|
||||||
@@ -1302,6 +1337,7 @@
|
|||||||
"maxUsesPlaceholder": "0 = illimitato",
|
"maxUsesPlaceholder": "0 = illimitato",
|
||||||
"expiresAt": "Scade il",
|
"expiresAt": "Scade il",
|
||||||
"enabled": "Attivo",
|
"enabled": "Attivo",
|
||||||
|
"create": "Crea buono",
|
||||||
"save": "Salva buono",
|
"save": "Salva buono",
|
||||||
"saving": "Salvataggio…",
|
"saving": "Salvataggio…",
|
||||||
"cancel": "Annulla"
|
"cancel": "Annulla"
|
||||||
|
|||||||
Reference in new issue
Block a user