import { createFileRoute, useNavigate } from '@tanstack/react-router' import { useQuery } from '@tanstack/react-query' import { useState } from 'react' import { repairBatchListOptions } from '@/api/repairs' import { usePagination } from '@/hooks/use-pagination' import { DataTable, type Column } from '@/components/shared/data-table' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Badge } from '@/components/ui/badge' import { Plus, Search } from 'lucide-react' import { useAuthStore } from '@/stores/auth.store' import type { RepairBatch } from '@/types/repair' export const Route = createFileRoute('/_authenticated/repair-batches/')({ validateSearch: (search: Record) => ({ page: Number(search.page) || 1, limit: Number(search.limit) || 25, q: (search.q as string) || undefined, sort: (search.sort as string) || undefined, order: (search.order as 'asc' | 'desc') || 'desc', }), component: RepairBatchesListPage, }) const columns: Column[] = [ { key: 'batch_number', header: 'Batch #', sortable: true, render: (b) => {b.batchNumber ?? '-'}, }, { key: 'contact', header: 'Contact', render: (b) => {b.contactName ?? '-'}, }, { key: 'status', header: 'Status', sortable: true, render: (b) => {b.status.replace('_', ' ')}, }, { key: 'approval', header: 'Approval', render: (b) => { const v = b.approvalStatus === 'approved' ? 'default' : b.approvalStatus === 'rejected' ? 'destructive' : 'secondary' return {b.approvalStatus} }, }, { key: 'items', header: 'Items', render: (b) => <>{b.receivedCount}/{b.itemCount}, }, { key: 'due_date', header: 'Due', sortable: true, render: (b) => <>{b.dueDate ? new Date(b.dueDate).toLocaleDateString() : '-'}, }, ] function RepairBatchesListPage() { const navigate = useNavigate() const hasPermission = useAuthStore((s) => s.hasPermission) const { params, setPage, setSearch, setSort } = usePagination() const [searchInput, setSearchInput] = useState(params.q ?? '') const { data, isLoading } = useQuery(repairBatchListOptions(params)) function handleSearchSubmit(e: React.FormEvent) { e.preventDefault() setSearch(searchInput) } function handleRowClick(batch: RepairBatch) { navigate({ to: '/repair-batches/$batchId', params: { batchId: batch.id }, search: { page: 1, limit: 25, q: undefined, sort: undefined, order: 'asc' as const } }) } return (

Repair Batches

{hasPermission('repairs.edit') && ( )}
setSearchInput(e.target.value)} className="pl-9" />
) }