import { useMemo, useState } from "react"; import { useNavigate } from "react-router-dom"; import { useQuery } from "@tanstack/react-query"; import { useVendorPortal } from "@/app/v/_components/vendor-portal-context"; import { vendorPortalApi } from "@/domain/vendor-portal/api/vendor-portal-api"; import { formatVendorPortalDateTime, vendorPortalStatusClass, } from "@/domain/vendor-portal/lib/status-helpers"; import type { VendorPortalDispatchSummary } from "@/domain/vendor-portal/types/vendor-portal"; import { queryKeys } from "@/infra/query-key/query-key"; const FILTERS = [ { key: "open", label: "Open", match: (dispatch: VendorPortalDispatchSummary) => dispatch.status !== "Completed" && dispatch.status !== "Verified" && dispatch.status !== "Cancelled", }, { key: "completed", label: "Completed", match: (dispatch: VendorPortalDispatchSummary) => dispatch.status === "Completed" || dispatch.status === "Verified", }, { key: "cancelled", label: "Cancelled", match: (dispatch: VendorPortalDispatchSummary) => dispatch.status === "Cancelled", }, { key: "all", label: "All", match: () => true }, ] as const; export default function VendorPortalDashboardPage() { const { token } = useVendorPortal(); const navigate = useNavigate(); const [filter, setFilter] = useState<(typeof FILTERS)[number]["key"]>("open"); const { data: dispatches = [], isLoading, error, } = useQuery({ queryKey: queryKeys.vendorPortal.dispatches(token), queryFn: () => vendorPortalApi.listDispatches(token), enabled: Boolean(token), }); const activeFilter = FILTERS.find((item) => item.key === filter) ?? FILTERS[0]; const filtered = useMemo(() => dispatches.filter(activeFilter.match), [dispatches, activeFilter]); return ( <>

Your Work

Active dispatches and work orders assigned to your company.
{FILTERS.map((item) => ( ))}
{isLoading &&
Loading dispatches…
} {error &&
{error.message}
} {!isLoading && !error && filtered.length === 0 && (
No dispatches in this view.
)}
{filtered.map((dispatch) => (
navigate(`../dispatch/${dispatch.id}`)} >
{dispatch.dispatchNumber} — {dispatch.workOrderTitle || "(untitled)"}
WO#{dispatch.internalWONumber} {dispatch.locationName ? ` · ${dispatch.locationName}` : ""} {dispatch.locationCity ? `, ${dispatch.locationCity}` : ""} {dispatch.locationState ? `, ${dispatch.locationState}` : ""} {dispatch.scheduledDate ? ` · Scheduled ${formatVendorPortalDateTime(dispatch.scheduledDate)}` : ""}
{dispatch.status}
))}
); }