Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 | 1x 1x 334x 334x 6x 334x 334x 334x 334x 7x 4x 4x 2x 2x 2x 16x 64x 64x 64x 64x 64x 64x 64x 64x 64x 5x 5x 4x 1x 2x 1x 1x 64x 3x 3x 1x 1x 327x | import { useState } from "react";
import { useQueryClient } from "@tanstack/react-query";
import { CalendarClock, ChevronDown, ChevronRight } from "lucide-react";
import { Card } from "../../components/ui/card";
import { Button } from "../../components/ui/button";
import { Pagination } from "../../components/ui/pagination";
import { EmptyState } from "../../components/ui/empty-state";
import {
adminApi,
type ConsultationBooking,
type ConsultationBookingStatus,
} from "../../lib/api";
import { queryKeys } from "../../lib/query-keys";
import {
useAdminConsultations,
type ConsultationFilters,
} from "../../hooks/queries/useAdminQueries";
import { useConfirmDialog } from "../../hooks";
import { Select } from "../../components/ui/select";
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "../../components/ui/table";
import { StatusBadge } from "../../components/ui/status-badge";
import type { StatusTone } from "../../components/ui/status-ink";
import { Alert } from "../../components/ui/alert";
import {
AdminBand,
AdminPageHead,
AdminToolbar,
} from "../../components/admin/AdminPageHead";
import { formatDateTime } from "../../lib/utils";
const PAGE_SIZE = 20;
// A booking moves forward one step at a time; cancellation is available from
// any non-terminal state, offered as a separate action rather than folded
// into "next status" below.
const NEXT_STATUS: Partial<
Record<ConsultationBookingStatus, ConsultationBookingStatus>
> = {
pending: "contacted",
contacted: "completed",
};
// ─── Status badge ────────────────────────────────────────────────────────────
function BookingStatusCode({ status }: { status: string }) {
// Status ink on its own `-light` tint, from `components/ui/status-ink.ts`.
const tones: Record<string, StatusTone> = {
pending: "info",
contacted: "warning",
completed: "success",
cancelled: "default",
};
return <StatusBadge status={status} variant={tones[status] ?? "default"} />;
}
// ─── Row detail panel ────────────────────────────────────────────────────────
function RowDetail({ booking }: { booking: ConsultationBooking }) {
return (
<TableRow>
<TableCell colSpan={6}>
<div className="rounded-lg bg-background-subtle border border-border-default p-4 text-sm space-y-3">
<div className="grid grid-cols-1 sm:grid-cols-2 gap-3">
<div>
<span className="text-foreground-muted font-medium">
Source page:
</span>{" "}
<span className="text-foreground-default">
{booking.sourcePage ?? "—"}
</span>
</div>
{booking.homeownerId && (
<div>
<span className="text-foreground-muted font-medium">
Homeowner ID:
</span>{" "}
<span className="font-mono text-xs text-foreground-default">
{booking.homeownerId}
</span>
</div>
)}
</div>
{booking.details && (
<div>
<p className="text-foreground-muted font-medium mb-1">
Details from homeowner:
</p>
<p className="text-foreground-default whitespace-pre-wrap text-xs bg-background-base rounded p-2">
{booking.details}
</p>
</div>
)}
</div>
</TableCell>
</TableRow>
);
}
// ─── Table row ───────────────────────────────────────────────────────────────
function ConsultationRow({
booking,
onStatusChange,
onCancel,
}: {
booking: ConsultationBooking;
onStatusChange: (id: number, status: ConsultationBookingStatus) => void;
onCancel: (id: number) => void;
}) {
const [expanded, setExpanded] = useState(false);
const nextStatus = NEXT_STATUS[booking.status];
const cancellable =
booking.status === "pending" || booking.status === "contacted";
return (
<>
<TableRow
className="cursor-pointer"
onClick={() => setExpanded((v) => !v)}
>
{/* `en-IN` from one place. */}
<TableCell className="whitespace-nowrap text-xs tabular-nums text-foreground-muted">
{formatDateTime(booking.dateCreated)}
</TableCell>
<TableCell className="text-sm font-mono text-foreground-default">
{booking.reference}
</TableCell>
<TableCell className="text-sm text-foreground-default whitespace-nowrap">
{booking.phone}
</TableCell>
<TableCell>
<BookingStatusCode status={booking.status} />
</TableCell>
<TableCell className="text-sm text-foreground-muted max-w-xs truncate">
{booking.details ?? "—"}
</TableCell>
<TableCell>
<div className="flex items-center gap-2">
{nextStatus && (
<Button
variant="outline"
size="sm"
onClick={(e) => {
e.stopPropagation();
onStatusChange(booking.id, nextStatus);
}}
>
Mark {nextStatus}
</Button>
)}
{cancellable && (
<Button
variant="outline"
size="sm"
onClick={(e) => {
e.stopPropagation();
onCancel(booking.id);
}}
>
Cancel
</Button>
)}
{expanded ? (
<ChevronDown className="h-4 w-4 text-foreground-subtle" />
) : (
<ChevronRight className="h-4 w-4 text-foreground-subtle" />
)}
</div>
</TableCell>
</TableRow>
{expanded && <RowDetail booking={booking} />}
</>
);
}
// ─── Skeleton rows ───────────────────────────────────────────────────────────
function SkeletonRows() {
return (
<>
{[1, 2, 3, 4, 5, 6, 7, 8].map((i) => (
<TableRow key={i}>
<TableCell colSpan={6}>
<div className="h-5 bg-background-muted rounded animate-pulse" />
</TableCell>
</TableRow>
))}
</>
);
}
// ─── Main page ───────────────────────────────────────────────────────────────
export function AdminConsultationsPage() {
const queryClient = useQueryClient();
const { confirm, dialog: confirmDialog } = useConfirmDialog();
const [statusFilter, setStatusFilter] = useState<
ConsultationBookingStatus | ""
>("");
const [page, setPage] = useState(1);
const filters: ConsultationFilters = {
page,
limit: PAGE_SIZE,
status: statusFilter || undefined,
};
const { data, isLoading, error } = useAdminConsultations(filters);
const items: ConsultationBooking[] = data?.data ?? [];
const total = data?.meta?.total ?? 0;
const totalPages = data?.meta?.totalPages ?? 1;
async function handleStatusChange(
id: number,
newStatus: ConsultationBookingStatus,
) {
try {
await adminApi.updateConsultationStatus(id, newStatus);
queryClient.invalidateQueries({
queryKey: queryKeys.admin.consultations.all,
});
} catch (err) {
console.error("Failed to update consultation booking status:", err);
}
}
// Cancelling is the one status move the homeowner cannot walk back from, so
// it asks first.
async function handleCancel(id: number) {
if (
!(await confirm({
title: "Cancel consultation",
description:
"Cancel this consultation booking? The slot is released and the homeowner is no longer expecting a call.",
confirmLabel: "Cancel booking",
cancelLabel: "Keep booking",
variant: "destructive",
}))
) {
return;
}
await handleStatusChange(id, "cancelled");
}
return (
<div className="space-y-6">
<AdminPageHead
title="Free Consultation Bookings"
description="Requests from the /book-consultation page. Coordinate the call, then update status here."
actions={
isLoading ? (
<span className="text-xs font-semibold uppercase tracking-wider text-foreground-muted">
Loading...
</span>
) : undefined
}
/>
<AdminToolbar aria-label="Booking filters">
<Select
aria-label="Filter by status"
value={statusFilter}
onChange={(e) => {
setStatusFilter(e.target.value as ConsultationBookingStatus | "");
setPage(1);
}}
className="sm:w-48"
>
<option value="">All Statuses</option>
<option value="pending">Pending</option>
<option value="contacted">Contacted</option>
<option value="completed">Completed</option>
<option value="cancelled">Cancelled</option>
</Select>
{statusFilter && (
<Button
variant="outline"
onClick={() => {
setStatusFilter("");
setPage(1);
}}
>
Clear
</Button>
)}
</AdminToolbar>
<section className="space-y-2">
<AdminBand
title="Bookings"
count={isLoading ? undefined : total}
quiet
/>
{error ? (
<Alert variant="error">
Failed to load consultation bookings. Please refresh the page.
</Alert>
) : (
<Card className="overflow-hidden">
<Table density="dense">
<TableCaption>
Consultation bookings, newest first. Expand a row for its
detail.
</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Date</TableHead>
<TableHead>Reference</TableHead>
<TableHead>Phone</TableHead>
<TableHead>Status</TableHead>
<TableHead>Details</TableHead>
<TableHead className="w-8" />
</TableRow>
</TableHeader>
<TableBody>
{isLoading ? (
<SkeletonRows />
) : items.length === 0 ? (
<TableRow>
<TableCell colSpan={6}>
<EmptyState
icon={CalendarClock}
title="No consultation bookings found"
description="Try adjusting your filters or check back later."
/>
</TableCell>
</TableRow>
) : (
items.map((booking) => (
<ConsultationRow
key={booking.id}
booking={booking}
onStatusChange={handleStatusChange}
onCancel={handleCancel}
/>
))
)}
</TableBody>
</Table>
{/* Pagination — the primitive owns the status line and the pager. */}
{!isLoading && total > PAGE_SIZE && (
<Pagination
className="border-t border-border-default px-4 py-3"
density="dense"
label="Consultation booking pages"
page={page}
totalPages={totalPages}
total={total}
onPageChange={setPage}
/>
)}
</Card>
)}
</section>
{confirmDialog}
</div>
);
}
|