287 lines
14 KiB
TypeScript
287 lines
14 KiB
TypeScript
import { useState } from "react";
|
|
import { Form, useNavigate, useNavigation } from "react-router";
|
|
import { useTranslation } from "react-i18next";
|
|
import { FeatherCalendarClock, FeatherCheck, FeatherUserX } from "@subframe/core";
|
|
import type { BookingDetailVM, RescheduleSlotOption } from "@hivegrid/view-models";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
|
|
import { Button } from "~/ui/components/Button";
|
|
import { Calendar } from "~/ui/components/Calendar";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { Dialog } from "~/ui/components/Dialog";
|
|
import { Loader } from "~/ui/components/Loader";
|
|
import { Select } from "~/ui/components/Select";
|
|
import { TextInput } from "~/ui/components/TextInput";
|
|
import { bookingStatusTone } from "./BookingListPage";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
|
|
/**
|
|
* AK-P16b · interim booking detail (AD-28, archetype A3): appointment data,
|
|
* intake answers and the full M129 lifecycle — held/no-show inline, cancel
|
|
* (reason) / reschedule (slot picker) / re-assign (provider select) as real
|
|
* Dialogs (S 560) posting the route's existing intents. Date selection
|
|
* GET-navigates `?slotsDate=` so the loader recomputes engine slots; the
|
|
* component stays mounted, so the dialog survives the navigation. Final
|
|
* Subframe optics follow David's D-P16 delta (design-nachzug).
|
|
*
|
|
* Page chrome matches the boards/pipelines and task/lead detail screens: the
|
|
* full-bleed PageHeader bar carries icon, title, status badge and the lifecycle
|
|
* actions, content sits in PageBody Cards. Navigation back to the list is the
|
|
* app breadcrumb's job, so the screen renders no back link of its own.
|
|
*/
|
|
|
|
export interface BookingDetailPageProps {
|
|
vm: BookingDetailVM;
|
|
csrf: string | null;
|
|
rescheduleSlots: readonly RescheduleSlotOption[] | null;
|
|
slotsDate: string | null;
|
|
}
|
|
|
|
type OpenDialog = "cancel" | "reschedule" | "reassign" | null;
|
|
|
|
/** Card section heading — same weight the task/lead detail cards use. */
|
|
const SECTION_HEADING_CLASS = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary";
|
|
|
|
function isoToLocalDate(dateIso: string): Date {
|
|
const [year, month, day] = dateIso.split("-").map(Number);
|
|
return new Date(year ?? 1970, (month ?? 1) - 1, day ?? 1);
|
|
}
|
|
|
|
function localDateToIso(date: Date): string {
|
|
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
const day = String(date.getDate()).padStart(2, "0");
|
|
return `${date.getFullYear()}-${month}-${day}`;
|
|
}
|
|
|
|
function CsrfField({ csrf }: { csrf: string | null }): React.JSX.Element | null {
|
|
return csrf ? <input type="hidden" name="csrf" value={csrf} /> : null;
|
|
}
|
|
|
|
export function BookingDetailPage({ vm, csrf, rescheduleSlots, slotsDate }: BookingDetailPageProps): React.JSX.Element {
|
|
const { t } = useTranslation();
|
|
const navigate = useNavigate();
|
|
const navigation = useNavigation();
|
|
const [openDialog, setOpenDialog] = useState<OpenDialog>(null);
|
|
const [selectedSlot, setSelectedSlot] = useState<string | null>(null);
|
|
const [selectedProvider, setSelectedProvider] = useState<string>("");
|
|
|
|
if (vm.status !== "ready" || !vm.data) {
|
|
return <PageShell uiId="ui.booking.detail" />;
|
|
}
|
|
const data = vm.data;
|
|
const canAct = data.canWrite && data.status === "scheduled";
|
|
const loadingSlots = navigation.state === "loading" && navigation.location?.search.includes("slotsDate=");
|
|
|
|
const closeDialogProps = (dialog: OpenDialog) => ({
|
|
open: openDialog === dialog,
|
|
onOpenChange: (open: boolean) => {
|
|
if (!open) setOpenDialog(null);
|
|
},
|
|
});
|
|
|
|
return (
|
|
<PageShell uiId="ui.booking.detail" className="bg-surface-neutral-primary">
|
|
<PageHeader
|
|
uiId="ui.booking.detail.header"
|
|
icon={<FeatherCalendarClock className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={
|
|
<span className="inline-flex flex-wrap items-center gap-2">
|
|
{data.pageTitle}
|
|
<span data-status={data.status}>
|
|
<BadgeFeedback farbe={bookingStatusTone(data.status)} size="small">
|
|
{t(data.statusKey)}
|
|
</BadgeFeedback>
|
|
</span>
|
|
</span>
|
|
}
|
|
subtitle={data.whenLabel}
|
|
subtitleTone="secondary"
|
|
actions={
|
|
canAct ? (
|
|
<span className="flex flex-wrap items-center gap-2" data-ui-id="ui.booking.detail.actions">
|
|
<Form method="post">
|
|
<CsrfField csrf={csrf} />
|
|
<input type="hidden" name="intent" value="held" />
|
|
<Button type="submit" variant="secondary" icon={<FeatherCheck />}>
|
|
{t("bookings.actions.markHeld")}
|
|
</Button>
|
|
</Form>
|
|
<Form method="post">
|
|
<CsrfField csrf={csrf} />
|
|
<input type="hidden" name="intent" value="no_show" />
|
|
<Button type="submit" variant="secondary" icon={<FeatherUserX />}>
|
|
{t("bookings.actions.markNoShow")}
|
|
</Button>
|
|
</Form>
|
|
<Button variant="secondary" icon={<FeatherCalendarClock />} onClick={() => setOpenDialog("reschedule")} data-ui-id="ui.booking.detail.reschedule">
|
|
{t("bookings.actions.reschedule")}
|
|
</Button>
|
|
{data.canAdmin ? (
|
|
<Button variant="secondary" onClick={() => setOpenDialog("reassign")} data-ui-id="ui.booking.detail.reassign">
|
|
{t("bookings.actions.reassign")}
|
|
</Button>
|
|
) : null}
|
|
<Button variant="destructive" onClick={() => setOpenDialog("cancel")} data-ui-id="ui.booking.detail.cancel">
|
|
{t("bookings.actions.cancel")}
|
|
</Button>
|
|
</span>
|
|
) : undefined
|
|
}
|
|
/>
|
|
|
|
<PageBody>
|
|
{data.cancellationReason ? <Alert variant="error" title={t("bookings.detail.cancelledBanner")} description={data.cancellationReason} /> : null}
|
|
|
|
<Card data-ui-id="ui.booking.detail.customer">
|
|
<span className={SECTION_HEADING_CLASS}>{t("bookings.detail.customer")}</span>
|
|
<div className="flex flex-col gap-1">
|
|
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{data.customerName || "—"}</span>
|
|
{data.customerEmail ? (
|
|
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-tertiary">{data.customerEmail}</span>
|
|
) : null}
|
|
</div>
|
|
</Card>
|
|
|
|
{data.intake.length > 0 ? (
|
|
<Card data-ui-id="ui.booking.detail.intake">
|
|
<span className={SECTION_HEADING_CLASS}>{t("bookings.detail.intake")}</span>
|
|
<dl className="grid w-full grid-cols-1 gap-4 sm:grid-cols-2">
|
|
{data.intake.map((entry) => (
|
|
<div key={entry.key} className="flex min-w-0 flex-col gap-1">
|
|
<dt className="break-words text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{entry.key}</dt>
|
|
<dd className="break-words text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{entry.value || "—"}</dd>
|
|
</div>
|
|
))}
|
|
</dl>
|
|
</Card>
|
|
) : null}
|
|
</PageBody>
|
|
|
|
{/* Cancel (S 560): reason + destructive confirm, auto-mail server-side. */}
|
|
<Dialog {...closeDialogProps("cancel")}>
|
|
<Dialog.Content className="w-[560px] max-w-[90vw] p-4">
|
|
<Form method="post" className="flex w-full flex-col gap-6" data-ui-id="ui.booking.detail.cancelDialog">
|
|
<CsrfField csrf={csrf} />
|
|
<input type="hidden" name="intent" value="cancel" />
|
|
<div className="flex flex-col gap-2">
|
|
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("bookings.cancelDialog.title")}</span>
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{t("bookings.cancelDialog.body")}</span>
|
|
</div>
|
|
<TextInput className="w-full" label={t("bookings.actions.cancelReason")} showLabel>
|
|
<TextInput.Input name="reason" placeholder={t("bookings.cancelDialog.reasonPlaceholder")} />
|
|
</TextInput>
|
|
<div className="flex w-full items-center justify-end gap-2">
|
|
<Button type="button" variant="tertiary" onClick={() => setOpenDialog(null)}>
|
|
{t("shared.cancel")}
|
|
</Button>
|
|
<Button type="submit" variant="destructive">
|
|
{t("bookings.cancelDialog.confirm")}
|
|
</Button>
|
|
</div>
|
|
</Form>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
|
|
{/* Reschedule (S 560): pick a date -> loader recomputes slots -> pick a slot. */}
|
|
<Dialog {...closeDialogProps("reschedule")}>
|
|
<Dialog.Content className="w-[560px] max-w-[90vw] p-4">
|
|
<div className="flex w-full flex-col gap-6" data-ui-id="ui.booking.detail.rescheduleDialog">
|
|
<div className="flex flex-col gap-2">
|
|
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("bookings.reschedule.title")}</span>
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{t("bookings.reschedule.body")}</span>
|
|
</div>
|
|
<div className="flex w-full justify-center">
|
|
<Calendar
|
|
mode="single"
|
|
selected={slotsDate ? isoToLocalDate(slotsDate) : undefined}
|
|
disabled={{ before: isoToLocalDate(data.minRescheduleDateIso), after: isoToLocalDate(data.maxRescheduleDateIso) }}
|
|
onSelect={(date: Date | undefined) => {
|
|
if (!date) return;
|
|
setSelectedSlot(null);
|
|
navigate(`?slotsDate=${localDateToIso(date)}`, { preventScrollReset: true, replace: true });
|
|
}}
|
|
/>
|
|
</div>
|
|
{loadingSlots ? (
|
|
<div className="flex w-full justify-center py-2">
|
|
<Loader />
|
|
</div>
|
|
) : slotsDate && rescheduleSlots ? (
|
|
rescheduleSlots.length === 0 ? (
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("bookings.reschedule.noSlots")}</span>
|
|
) : (
|
|
<div className="flex w-full flex-wrap gap-2" data-ui-id="ui.booking.detail.rescheduleSlots">
|
|
{rescheduleSlots.map((slot) => (
|
|
<button
|
|
key={slot.startAtIso}
|
|
type="button"
|
|
onClick={() => setSelectedSlot(slot.startAtIso)}
|
|
className={
|
|
selectedSlot === slot.startAtIso
|
|
? "flex items-center rounded-md border border-solid border-brand-brand-border-solid-default bg-brand-brand-secondary-surface-subtle px-3 py-1.5 text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-primary"
|
|
: "flex items-center rounded-md border border-solid border-border-default bg-surface-neutral-secondary px-3 py-1.5 text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary hover:bg-surface-neutral-tertiary"
|
|
}
|
|
>
|
|
{slot.label}
|
|
</button>
|
|
))}
|
|
</div>
|
|
)
|
|
) : (
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("bookings.reschedule.pickDateHint")}</span>
|
|
)}
|
|
<Form method="post" className="flex w-full items-center justify-end gap-2">
|
|
<CsrfField csrf={csrf} />
|
|
<input type="hidden" name="intent" value="reschedule" />
|
|
{selectedSlot ? <input type="hidden" name="newStartAt" value={selectedSlot} /> : null}
|
|
<Button type="button" variant="tertiary" onClick={() => setOpenDialog(null)}>
|
|
{t("shared.cancel")}
|
|
</Button>
|
|
<Button type="submit" disabled={!selectedSlot} data-ui-id="ui.booking.detail.rescheduleSubmit">
|
|
{t("bookings.reschedule.confirm")}
|
|
</Button>
|
|
</Form>
|
|
</div>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
|
|
{/* Re-assign (S 560, admin): pick another provider. */}
|
|
<Dialog {...closeDialogProps("reassign")}>
|
|
<Dialog.Content className="w-[560px] max-w-[90vw] p-4">
|
|
<Form method="post" className="flex w-full flex-col gap-6" data-ui-id="ui.booking.detail.reassignDialog">
|
|
<CsrfField csrf={csrf} />
|
|
<input type="hidden" name="intent" value="reassign" />
|
|
<div className="flex flex-col gap-2">
|
|
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("bookings.reassign.title")}</span>
|
|
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">{t("bookings.reassign.body")}</span>
|
|
</div>
|
|
<Select
|
|
className="w-full"
|
|
label={t("bookings.reassign.providerLabel")}
|
|
showLabel
|
|
placeholder={t("bookings.reassign.providerPlaceholder")}
|
|
value={selectedProvider || undefined}
|
|
onValueChange={(value: string) => setSelectedProvider(value)}
|
|
>
|
|
{data.providerOptions.map((option) => (
|
|
<Select.Item key={option.id} value={option.id}>
|
|
{option.label}
|
|
</Select.Item>
|
|
))}
|
|
</Select>
|
|
{selectedProvider ? <input type="hidden" name="newProviderUserId" value={selectedProvider} /> : null}
|
|
<div className="flex w-full items-center justify-end gap-2">
|
|
<Button type="button" variant="tertiary" onClick={() => setOpenDialog(null)}>
|
|
{t("shared.cancel")}
|
|
</Button>
|
|
<Button type="submit" disabled={!selectedProvider} data-ui-id="ui.booking.detail.reassignSubmit">
|
|
{t("bookings.reassign.confirm")}
|
|
</Button>
|
|
</div>
|
|
</Form>
|
|
</Dialog.Content>
|
|
</Dialog>
|
|
</PageShell>
|
|
);
|
|
}
|