import { useState } from "preact/hooks"; import type { ConfirmAction } from "./LeaseApprovalConfirmDialog.tsx"; import LeaseApprovalConfirmDialog from "./LeaseApprovalConfirmDialog.tsx"; export interface PendingApprovalItem { id: string; property?: { title?: string | null; address_1?: string | null; city?: string | null; state?: string | null; zip?: string | null; monthly_rent?: number | null; bedrooms?: number | null; bathrooms?: number | null; } | null; property_address?: string | null; tenant?: { name?: string | null; email?: string | null } | null; approval_requested_at?: number | null; } interface LandlordPendingApprovalsProps { approvals: PendingApprovalItem[]; } function formatAddress(prop: PendingApprovalItem["property"]): string | null { if (!prop) return null; const parts = [ prop.address_1, [prop.city, prop.state].filter(Boolean).join(", "), prop.zip, ].filter(Boolean); return parts.length > 0 ? parts.join(", ") : null; } function formatRentBeds(prop: PendingApprovalItem["property"]): string | null { if (!prop) return null; const hasRent = prop.monthly_rent != null; const hasBeds = prop.bedrooms != null || prop.bathrooms != null; if (!hasRent && !hasBeds) return null; const rent = hasRent ? `$${Number(prop.monthly_rent).toLocaleString()}/mo` : null; const beds = hasBeds ? `${prop.bedrooms ?? "—"} bed, ${prop.bathrooms ?? "—"} bath` : null; return [rent, beds].filter(Boolean).join(" · "); } export default function LandlordPendingApprovals({ approvals, }: LandlordPendingApprovalsProps) { const [confirmState, setConfirmState] = useState< { action: ConfirmAction; leaseId: string; } | null >(null); const [submitting, setSubmitting] = useState(false); const handleApproveClick = (e: Event, leaseId: string) => { e.preventDefault(); setConfirmState({ action: "approve", leaseId }); }; const handleRejectClick = (e: Event, leaseId: string) => { e.preventDefault(); setConfirmState({ action: "reject", leaseId }); }; const handleConfirm = async () => { if (!confirmState) return; setSubmitting(true); const url = confirmState.action === "approve" ? "/api/lease/approve" : "/api/lease/reject"; const body = confirmState.action === "reject" ? { lease_id: confirmState.leaseId, rejection_reason: "" } : { lease_id: confirmState.leaseId }; try { const res = await fetch(url, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(body), }); if (res.redirected) { window.location.href = res.url; return; } if (res.ok) { window.location.reload(); return; } } finally { setSubmitting(false); setConfirmState(null); } }; const handleCancel = () => { if (!submitting) setConfirmState(null); }; return ( <>

Pending Lease Approvals

View all
{approvals && approvals.length > 0 ? ( approvals.slice(0, 3).map((approval) => { const prop = approval.property; const addressLine = formatAddress(prop); const rentBeds = formatRentBeds(prop); return (

{prop?.title || approval.property_address || "Property"}

{addressLine && (

{addressLine}

)} {rentBeds && (

{rentBeds}

)}

Tenant: {approval.tenant?.name || approval.tenant?.email || "Unknown"}

Requested {approval.approval_requested_at ? new Date( approval.approval_requested_at as number, ) .toLocaleDateString() : "Unknown date"}

{ e.preventDefault(); handleApproveClick(e, approval.id); }} >
{ e.preventDefault(); handleRejectClick(e, approval.id); }} >
); }) ) : (

No pending lease approvals

)}
); }