59 lines
2.5 KiB
TypeScript
59 lines
2.5 KiB
TypeScript
// AK-P16b · booking-pages list route (M06 Block E.1). bookings.read_all to view;
|
|
// editing requires bookings.admin (enforced in the editor). Shows each page's public
|
|
// URL with copy (DoD 3). Interim ~/ui screen (AD-28); final polish follows D-P16.
|
|
|
|
import { type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { buildBookingPagesListVM, type BookingPageListRow as BookingPageListRowVM, type BookingPagesListVM } from "@hivegrid/view-models";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { loadBookingContext, requireBookingPermission, BOOKING_PERMISSIONS } from "~/modules/booking/routes/bookingAccess.server";
|
|
import { BOOKING_MODULE_KEY, bookingPageEditPath, bookingPageNewPath, publicBookingPath, notFoundResponse } from "~/modules/booking/routes/bookingRoutePaths";
|
|
import { listBookingPages } from "~/modules/booking/db/booking.db.server";
|
|
import { BookingPagesPage } from "~/components/screens/booking/BookingPagesPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; vm: BookingPagesListVM };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadBookingContext({ request, params, requireModule: true });
|
|
try {
|
|
requireBookingPermission(ctx, BOOKING_PERMISSIONS.readAll);
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
const tenantSlug = params.tenant ?? "";
|
|
const origin = new URL(request.url).origin;
|
|
const pages = await listBookingPages(ctx.tenantId);
|
|
const rows: BookingPageListRowVM[] = pages.map((p) => {
|
|
const publicPath = publicBookingPath(tenantSlug, p.slug);
|
|
return {
|
|
id: p.id,
|
|
title: p.title,
|
|
slug: p.slug,
|
|
durationMinutes: p.durationMinutes,
|
|
isPublished: p.isPublished,
|
|
providerCount: p.providerCount,
|
|
editHref: bookingPageEditPath(tenantSlug, p.id),
|
|
publicPath,
|
|
publicUrl: `${origin}${publicPath}`,
|
|
};
|
|
});
|
|
const vm = buildBookingPagesListVM({
|
|
rows,
|
|
canCreate: ctx.permissions.includes(BOOKING_PERMISSIONS.admin),
|
|
newHref: bookingPageNewPath(tenantSlug),
|
|
});
|
|
return Response.json({ title: `${t("bookings.pages.title")} | ${process.env.APP_NAME}`, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export default function BookingPagesListRoute() {
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
return <BookingPagesPage vm={vm} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: BOOKING_MODULE_KEY };
|