Files
2026-07-08 16:15:37 +02:00

100 lines
4.2 KiB
TypeScript

// AK-P16b · bookings list route (M06 Block C). Loader builds BookingListVM from the
// tenant's appointments, server-side owner-filtered (read_own → own provider rows
// only, DoD 6). Module off -> locked empty; missing bookings.read_own -> 404.
// Baseline functional table (data-ui-id markers); bespoke polish follows D-P16.
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
import { useTranslation } from "react-i18next";
import { buildBookingListVM, bookingStatusKey, type BookingListRow, type BookingListVM } from "@hivegrid/view-models";
import { getTranslations } from "~/locale/i18next.server";
import { loadBookingContext, requireBookingPermission, canReadAllBookings, BOOKING_PERMISSIONS } from "~/modules/booking/routes/bookingAccess.server";
import { BOOKING_MODULE_KEY, bookingDetailPath, notFoundResponse } from "~/modules/booking/routes/bookingRoutePaths";
import { listAppointmentsForTenant } from "~/modules/booking/db/booking.db.server";
import ServerError from "~/components/ui/errors/ServerError";
type LoaderData = { title: string; vm: BookingListVM };
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const { t } = await getTranslations(request);
const ctx = await loadBookingContext({ request, params });
const title = `${t("bookings.list.title")} | ${process.env.APP_NAME}`;
if (!ctx.hasModule) {
return Response.json({ title, vm: buildBookingListVM({ rows: [] }) } satisfies LoaderData);
}
try {
requireBookingPermission(ctx, BOOKING_PERMISSIONS.readOwn);
} catch {
throw notFoundResponse();
}
const tenantSlug = params.tenant ?? "";
const appointments = await listAppointmentsForTenant({ tenantId: ctx.tenantId, ownerUserId: canReadAllBookings(ctx) ? null : ctx.userId });
const rows: BookingListRow[] = appointments.map((a) => ({
id: a.id,
startAtIso: a.startAt.toISOString(),
endAtIso: a.endAt.toISOString(),
status: a.status,
statusKey: bookingStatusKey(a.status),
customerName: a.customerName,
customerEmail: a.customerEmail,
pageTitle: a.pageTitle,
providerUserId: a.providerUserId,
detailHref: bookingDetailPath(tenantSlug, a.id),
}));
return Response.json({ title, vm: buildBookingListVM({ rows }) } satisfies LoaderData);
};
export default function BookingsListRoute() {
const { t } = useTranslation();
const { vm } = useLoaderData<LoaderData>();
if (vm.status !== "ready" || !vm.data) {
return <main className="mx-auto max-w-5xl px-4 py-10" data-ui-id="ui.booking.list" />;
}
const { rows, emptyHeadlineKey, emptyBodyKey } = vm.data;
return (
<main className="mx-auto max-w-5xl px-4 py-10" data-ui-id="ui.booking.list">
<h1 className="text-foreground text-xl font-semibold">{t("bookings.list.title")}</h1>
{rows.length === 0 ? (
<div className="mt-10 text-center" data-ui-id="ui.booking.list.empty">
<p className="text-foreground font-medium">{t(emptyHeadlineKey)}</p>
<p className="text-muted-foreground mt-1 text-sm">{t(emptyBodyKey)}</p>
</div>
) : (
<table className="mt-6 w-full text-sm" data-ui-id="ui.booking.list.table">
<thead>
<tr className="text-muted-foreground text-left">
<th className="py-2">{t("bookings.list.columns.when")}</th>
<th>{t("bookings.list.columns.customer")}</th>
<th>{t("bookings.list.columns.page")}</th>
<th>{t("bookings.list.columns.status")}</th>
</tr>
</thead>
<tbody className="divide-border divide-y">
{rows.map((row) => (
<tr key={row.id} data-row-id={row.id}>
<td className="py-2">
<a className="underline" href={row.detailHref}>
{new Date(row.startAtIso).toLocaleString()}
</a>
</td>
<td>{row.customerName || row.customerEmail}</td>
<td>{row.pageTitle}</td>
<td data-status={row.status}>{t(row.statusKey)}</td>
</tr>
))}
</tbody>
</table>
)}
</main>
);
}
export function ErrorBoundary() {
return <ServerError />;
}
export const handle = { moduleKey: BOOKING_MODULE_KEY };