Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
175 lines
6.3 KiB
TypeScript
175 lines
6.3 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useEffect, useMemo, useState } from "react";
|
|
import { useRouter } from "next/navigation";
|
|
import Link from "next/link";
|
|
import { Pencil, Plus } from "lucide-react";
|
|
import type { ColumnDef } from "@tanstack/react-table";
|
|
import { Badge } from "@/app/components/ui/badge";
|
|
import { Button } from "@/app/components/ui/button";
|
|
import { FilterField, FilterPanel, FilterSearch, FilterSelect } from "@/app/components/ui/filter-panel";
|
|
import { DataTable } from "@/app/components/ui/data-table";
|
|
import { PageHeader } from "@/app/components/ui/page-header";
|
|
import { RowActions } from "@/app/components/ui/row-actions";
|
|
import { EmptyState, ErrorState } from "@/app/components/ui/states";
|
|
import { RequireModule } from "@/app/lib/RequireModule";
|
|
import { useListFilters } from "@/app/lib/hooks/useListFilters";
|
|
import { useFitPageSize, MIN_PAGE_SIZE } from "@/app/lib/hooks/useFitPageSize";
|
|
import { listAdminEvents } from "@/app/lib/services/adminEvents";
|
|
import type { AdminEvent } from "@/app/lib/types";
|
|
import { useTableSort } from "@/app/lib/hooks/useTableSort";
|
|
import { useT } from "@/app/lib/i18n/MessagesProvider";
|
|
import { formatDmy } from "@/app/lib/date-input";
|
|
|
|
const DEFAULTS = { q: "", enabled: "" };
|
|
|
|
export const EVENT_STATUS_FILTER_OPTIONS = [{ value: "Y", label: "Enabled" }, { value: "N", label: "Disabled" }];
|
|
|
|
function EventsPageContent() {
|
|
const t = useT();
|
|
const router = useRouter();
|
|
const { tableRef, pageSize } = useFitPageSize();
|
|
const { filters, page, set, setPage, clear, activeCount } = useListFilters(DEFAULTS);
|
|
const { sorting, onSortingChange, sortBy, sortDir } = useTableSort();
|
|
const [data, setData] = useState<{ items: AdminEvent[]; total: number }>({ items: [], total: 0 });
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [reloadKey, setReloadKey] = useState(0);
|
|
|
|
const refetch = useCallback(() => setReloadKey((k) => k + 1), []);
|
|
|
|
useEffect(() => {
|
|
if (pageSize == null) return;
|
|
let cancelled = false;
|
|
// eslint-disable-next-line react-hooks/set-state-in-effect -- resets loading/error before the filter/page/reloadKey-triggered fetch below, which is the actual synchronization.
|
|
setLoading(true);
|
|
setError(null);
|
|
listAdminEvents({ q: filters.q || undefined, enabled: filters.enabled || undefined, page, pageSize, sortBy, sortDir })
|
|
.then((result) => {
|
|
if (!cancelled) setData(result);
|
|
})
|
|
.catch((err: unknown) => {
|
|
if (!cancelled) setError(err instanceof Error ? err.message : "EvtListLoadFailed");
|
|
})
|
|
.finally(() => {
|
|
if (!cancelled) setLoading(false);
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
};
|
|
}, [filters, page, reloadKey, pageSize, sortBy, sortDir]);
|
|
|
|
const statusOptions = useMemo(
|
|
() => EVENT_STATUS_FILTER_OPTIONS.map((o) => ({ value: o.value, label: t(o.label) })),
|
|
[t],
|
|
);
|
|
|
|
const columns = useMemo<ColumnDef<AdminEvent, unknown>[]>(
|
|
() => [
|
|
{
|
|
accessorKey: "name",
|
|
header: t("Name"),
|
|
cell: ({ row }) => (
|
|
<Link href={`/events/${row.original.uuid}`} className="font-medium text-primary underline-offset-4 hover:underline focus-visible:ring-2 focus-visible:ring-ring focus-visible:outline-none">
|
|
{row.original.name}
|
|
</Link>
|
|
),
|
|
},
|
|
{ accessorKey: "location", header: t("Location"), enableSorting: false },
|
|
{
|
|
accessorKey: "startDate",
|
|
header: t("EvtStartDate"),
|
|
cell: ({ row }) => <span>{formatDmy(row.original.startDate)}</span>,
|
|
},
|
|
{
|
|
accessorKey: "endDate",
|
|
header: t("EvtEndDate"),
|
|
cell: ({ row }) => <span>{formatDmy(row.original.endDate)}</span>,
|
|
},
|
|
{
|
|
accessorKey: "enabled",
|
|
header: t("Status"),
|
|
enableSorting: false,
|
|
cell: ({ row }) => (
|
|
<Badge variant={row.original.enabled ? "positive" : "neutral"}>
|
|
{row.original.enabled ? t("Enabled") : t("Disabled")}
|
|
</Badge>
|
|
),
|
|
},
|
|
{
|
|
id: "actions",
|
|
enableSorting: false,
|
|
header: t("CatActions"),
|
|
meta: { numeric: true },
|
|
cell: ({ row }) => (
|
|
<RowActions
|
|
label={row.original.name}
|
|
items={[{
|
|
label: t("EvtEdit"),
|
|
onSelect: () => router.push(`/events/${row.original.uuid}`),
|
|
}]}
|
|
>
|
|
<Button variant="ghost" size="sm" asChild className="h-8 px-2">
|
|
<Link href={`/events/${row.original.uuid}`} aria-label={t("EvtEditNamed", { name: row.original.name })}>
|
|
<Pencil aria-hidden="true" className="size-3.5" />
|
|
</Link>
|
|
</Button>
|
|
</RowActions>
|
|
),
|
|
},
|
|
],
|
|
[t, router],
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<PageHeader
|
|
title={t("BO_EVENTS")}
|
|
actions={
|
|
<Button asChild>
|
|
<Link href="/events/new">
|
|
<Plus aria-hidden="true" className="size-4" />
|
|
{t("EvtNewEvent")}
|
|
</Link>
|
|
</Button>
|
|
}
|
|
/>
|
|
|
|
{error && <ErrorState message={t(error)} onRetry={refetch} className="mb-4" />}
|
|
|
|
<FilterPanel activeCount={activeCount} onClear={clear}>
|
|
<FilterField label={t("EvtEventNameFilter")}>
|
|
<FilterSearch className="w-full" aria-label={t("EvtEventNameFilter")} placeholder={t("EvtEventNameFilter")} value={filters.q} onChange={q => set({ q })} />
|
|
</FilterField>
|
|
<FilterField label={t("EvtStatusFilter")}>
|
|
<FilterSelect className="w-full" aria-label={t("EvtStatusFilter")} allLabel={t("AllStatuses")} options={statusOptions} value={filters.enabled} onChange={enabled => set({ enabled })} />
|
|
</FilterField>
|
|
</FilterPanel>
|
|
|
|
<DataTable
|
|
label={t("BO_EVENTS")}
|
|
columns={columns}
|
|
data={data.items}
|
|
total={data.total}
|
|
page={page}
|
|
pageSize={pageSize ?? MIN_PAGE_SIZE}
|
|
sorting={sorting}
|
|
onSortingChange={onSortingChange}
|
|
tableRef={tableRef}
|
|
onPageChange={setPage}
|
|
isLoading={loading}
|
|
rowKey={(e) => String(e.uuid)}
|
|
empty={<EmptyState title={activeCount > 0 ? t("EvtNoMatch") : t("EvtNoEvents")} />}
|
|
/>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default function EventsPage() {
|
|
return (
|
|
<RequireModule pathname="/events">
|
|
<EventsPageContent />
|
|
</RequireModule>
|
|
);
|
|
}
|