phase-0: scaffold backend and frontend shell
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { render, screen, fireEvent } from "@testing-library/react";
|
||||
import { ColumnDef } from "@tanstack/react-table";
|
||||
import { DataTable } from "./DataTable";
|
||||
|
||||
interface RowItem {
|
||||
code: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
const columns: ColumnDef<RowItem, unknown>[] = [
|
||||
{ accessorKey: "code", header: "Code", enableColumnFilter: true },
|
||||
{ accessorKey: "name", header: "Name", enableColumnFilter: true },
|
||||
];
|
||||
|
||||
const data: RowItem[] = [
|
||||
{ code: "A-001", name: "Ventilator" },
|
||||
{ code: "A-002", name: "Monitor" },
|
||||
];
|
||||
|
||||
describe("DataTable", () => {
|
||||
it("renders rows and filters globally", () => {
|
||||
render(<DataTable data={data} columns={columns} globalFilterPlaceholder="Search..." />);
|
||||
expect(screen.getByText("Ventilator")).toBeInTheDocument();
|
||||
expect(screen.getByText("Monitor")).toBeInTheDocument();
|
||||
|
||||
const searchInput = screen.getByPlaceholderText("Search...");
|
||||
fireEvent.change(searchInput, { target: { value: "Ventilator" } });
|
||||
|
||||
expect(screen.getByText("Ventilator")).toBeInTheDocument();
|
||||
expect(screen.queryByText("Monitor")).not.toBeInTheDocument();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,266 @@
|
||||
import {
|
||||
useReactTable,
|
||||
getCoreRowModel,
|
||||
getFilteredRowModel,
|
||||
getPaginationRowModel,
|
||||
getSortedRowModel,
|
||||
flexRender,
|
||||
ColumnDef,
|
||||
SortingState,
|
||||
ColumnFiltersState,
|
||||
VisibilityState,
|
||||
PaginationState,
|
||||
FilterFn,
|
||||
} from "@tanstack/react-table";
|
||||
import { useState, useMemo } from "react";
|
||||
import { ChevronUp, ChevronDown, ChevronsUpDown, Download, Eye, Search } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
import { Button } from "@/components/ui/button";
|
||||
|
||||
interface DataTableProps<TData> {
|
||||
data: TData[];
|
||||
columns: ColumnDef<TData, unknown>[];
|
||||
loading?: boolean;
|
||||
emptyMessage?: string;
|
||||
emptyAction?: React.ReactNode;
|
||||
globalFilterPlaceholder?: string;
|
||||
onRowClick?: (row: TData) => void;
|
||||
exportFilename?: string;
|
||||
}
|
||||
|
||||
function exportToCsv<TData>(data: TData[], filename: string) {
|
||||
if (!data.length) return;
|
||||
const headers = Object.keys(data[0] as object);
|
||||
const rows = data.map((row) =>
|
||||
headers.map((h) => JSON.stringify((row as Record<string, unknown>)[h] ?? "")).join(",")
|
||||
);
|
||||
const csv = [headers.join(","), ...rows].join("\n");
|
||||
const blob = new Blob([csv], { type: "text/csv" });
|
||||
const url = URL.createObjectURL(blob);
|
||||
const a = document.createElement("a");
|
||||
a.href = url;
|
||||
a.download = `${filename}.csv`;
|
||||
a.click();
|
||||
URL.revokeObjectURL(url);
|
||||
}
|
||||
|
||||
export function DataTable<TData>({
|
||||
data,
|
||||
columns,
|
||||
loading = false,
|
||||
emptyMessage = "No records found.",
|
||||
emptyAction,
|
||||
globalFilterPlaceholder = "Search...",
|
||||
onRowClick,
|
||||
exportFilename = "export",
|
||||
}: DataTableProps<TData>) {
|
||||
const [sorting, setSorting] = useState<SortingState>([]);
|
||||
const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]);
|
||||
const [columnVisibility, setColumnVisibility] = useState<VisibilityState>({});
|
||||
const [globalFilter, setGlobalFilter] = useState("");
|
||||
const [pagination, setPagination] = useState<PaginationState>({ pageIndex: 0, pageSize: 20 });
|
||||
const [showColVis, setShowColVis] = useState(false);
|
||||
|
||||
const globalFilterFn: FilterFn<TData> = (row, _columnId, filterValue: string) => {
|
||||
const search = String(filterValue ?? "").toLowerCase();
|
||||
return Object.values(row.original as Record<string, unknown>).some((v) =>
|
||||
String(v ?? "").toLowerCase().includes(search)
|
||||
);
|
||||
};
|
||||
|
||||
const table = useReactTable({
|
||||
data,
|
||||
columns,
|
||||
state: { sorting, columnFilters, columnVisibility, globalFilter, pagination },
|
||||
onSortingChange: setSorting,
|
||||
onColumnFiltersChange: setColumnFilters,
|
||||
onColumnVisibilityChange: setColumnVisibility,
|
||||
onGlobalFilterChange: setGlobalFilter,
|
||||
onPaginationChange: setPagination,
|
||||
globalFilterFn,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
getFilteredRowModel: getFilteredRowModel(),
|
||||
getSortedRowModel: getSortedRowModel(),
|
||||
getPaginationRowModel: getPaginationRowModel(),
|
||||
});
|
||||
|
||||
const visibleRows = table.getRowModel().rows;
|
||||
const filteredRows = table.getFilteredRowModel().rows;
|
||||
const allFilteredData = useMemo(
|
||||
() => filteredRows.map((r) => r.original),
|
||||
[filteredRows]
|
||||
);
|
||||
|
||||
return (
|
||||
<div className="space-y-3">
|
||||
{/* Toolbar */}
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<div className="relative flex-1 min-w-48">
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
|
||||
<input
|
||||
value={globalFilter}
|
||||
onChange={(e) => setGlobalFilter(e.target.value)}
|
||||
placeholder={globalFilterPlaceholder}
|
||||
className="w-full pl-9 pr-4 py-1.5 text-sm border border-gray-200 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500"
|
||||
/>
|
||||
</div>
|
||||
<div className="relative">
|
||||
<Button variant="outline" size="sm" onClick={() => setShowColVis(!showColVis)}>
|
||||
<Eye className="h-4 w-4 mr-1" /> Columns
|
||||
</Button>
|
||||
{showColVis && (
|
||||
<div className="absolute right-0 mt-1 w-48 bg-white border border-gray-200 rounded-md shadow-lg z-20 p-2">
|
||||
{table.getAllLeafColumns().map((col) => (
|
||||
<label key={col.id} className="flex items-center gap-2 px-2 py-1 hover:bg-gray-50 rounded cursor-pointer text-sm">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={col.getIsVisible()}
|
||||
onChange={col.getToggleVisibilityHandler()}
|
||||
className="rounded"
|
||||
/>
|
||||
{String(col.columnDef.header ?? col.id)}
|
||||
</label>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
<Button variant="outline" size="sm" onClick={() => exportToCsv(allFilteredData, exportFilename)}>
|
||||
<Download className="h-4 w-4 mr-1" /> CSV (current view)
|
||||
</Button>
|
||||
</div>
|
||||
|
||||
{/* Table */}
|
||||
<div className="bg-white rounded-lg border border-gray-200 overflow-hidden">
|
||||
<div className="overflow-x-auto">
|
||||
<table className="w-full text-sm">
|
||||
<thead className="bg-gray-50 border-b border-gray-200">
|
||||
{table.getHeaderGroups().map((hg) => (
|
||||
<tr key={hg.id}>
|
||||
{hg.headers.map((header) => (
|
||||
<th key={header.id} className="text-left font-medium text-gray-600 px-4 py-3">
|
||||
{header.isPlaceholder ? null : (
|
||||
<div className="space-y-1">
|
||||
{/* Sortable header */}
|
||||
<button
|
||||
type="button"
|
||||
className={cn(
|
||||
"flex items-center gap-1 hover:text-gray-900 font-medium",
|
||||
header.column.getCanSort() ? "cursor-pointer select-none" : "cursor-default"
|
||||
)}
|
||||
onClick={header.column.getToggleSortingHandler()}
|
||||
>
|
||||
{flexRender(header.column.columnDef.header, header.getContext())}
|
||||
{header.column.getCanSort() && (
|
||||
header.column.getIsSorted() === "asc" ? (
|
||||
<ChevronUp className="h-3 w-3" />
|
||||
) : header.column.getIsSorted() === "desc" ? (
|
||||
<ChevronDown className="h-3 w-3" />
|
||||
) : (
|
||||
<ChevronsUpDown className="h-3 w-3 opacity-30" />
|
||||
)
|
||||
)}
|
||||
</button>
|
||||
{/* Per-column filter */}
|
||||
{header.column.getCanFilter() && (
|
||||
<input
|
||||
value={(header.column.getFilterValue() as string) ?? ""}
|
||||
onChange={(e) => header.column.setFilterValue(e.target.value)}
|
||||
placeholder="Filter..."
|
||||
className="w-full px-2 py-0.5 text-xs border border-gray-200 rounded focus:outline-none focus:ring-1 focus:ring-blue-400"
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</thead>
|
||||
<tbody className="divide-y divide-gray-100">
|
||||
{loading ? (
|
||||
[...Array(5)].map((_, i) => (
|
||||
<tr key={i}>
|
||||
{columns.map((_, j) => (
|
||||
<td key={j} className="px-4 py-3">
|
||||
<div className="h-4 bg-gray-100 rounded animate-pulse" />
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
))
|
||||
) : visibleRows.length === 0 ? (
|
||||
<tr>
|
||||
<td colSpan={columns.length} className="px-4 py-12 text-center">
|
||||
<div className="text-gray-400 space-y-2">
|
||||
<p>{emptyMessage}</p>
|
||||
{emptyAction}
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
) : (
|
||||
visibleRows.map((row) => (
|
||||
<tr
|
||||
key={row.id}
|
||||
className={cn(
|
||||
"hover:bg-blue-50/50 transition-colors",
|
||||
onRowClick && "cursor-pointer"
|
||||
)}
|
||||
onClick={() => onRowClick?.(row.original)}
|
||||
>
|
||||
{row.getVisibleCells().map((cell) => (
|
||||
<td key={cell.id} className="px-4 py-3 text-gray-700">
|
||||
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
||||
</td>
|
||||
))}
|
||||
</tr>
|
||||
))
|
||||
)}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
{/* Pagination */}
|
||||
{!loading && visibleRows.length > 0 && (
|
||||
<div className="flex items-center justify-between px-4 py-3 border-t border-gray-200 bg-gray-50 text-sm text-gray-600">
|
||||
<span>
|
||||
{filteredRows.length} record{filteredRows.length !== 1 ? "s" : ""}
|
||||
{globalFilter || columnFilters.length ? " (filtered)" : ""}
|
||||
</span>
|
||||
<div className="flex items-center gap-2">
|
||||
<select
|
||||
value={pagination.pageSize}
|
||||
onChange={(e) => table.setPageSize(Number(e.target.value))}
|
||||
className="border border-gray-200 rounded px-2 py-1 text-xs"
|
||||
>
|
||||
{[10, 20, 50, 100].map((s) => (
|
||||
<option key={s} value={s}>
|
||||
{s} / page
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => table.previousPage()}
|
||||
disabled={!table.getCanPreviousPage()}
|
||||
className="px-2 py-1 rounded border border-gray-200 disabled:opacity-40 hover:bg-white"
|
||||
>
|
||||
←
|
||||
</button>
|
||||
<span className="text-xs">
|
||||
Page {pagination.pageIndex + 1} / {table.getPageCount()}
|
||||
</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => table.nextPage()}
|
||||
disabled={!table.getCanNextPage()}
|
||||
className="px-2 py-1 rounded border border-gray-200 disabled:opacity-40 hover:bg-white"
|
||||
>
|
||||
→
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export { DataTable } from "./DataTable";
|
||||
@@ -0,0 +1,17 @@
|
||||
import { Outlet } from "react-router-dom";
|
||||
import { Sidebar } from "./Sidebar";
|
||||
import { TopBar } from "./TopBar";
|
||||
|
||||
export function AppShell() {
|
||||
return (
|
||||
<div className="flex min-h-screen bg-gray-50">
|
||||
<Sidebar />
|
||||
<div className="flex-1 flex flex-col min-w-0">
|
||||
<TopBar />
|
||||
<main className="flex-1 p-6 overflow-auto">
|
||||
<Outlet />
|
||||
</main>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
import { NavLink } from "react-router-dom";
|
||||
import {
|
||||
LayoutDashboard,
|
||||
Package,
|
||||
AlertCircle,
|
||||
Wrench,
|
||||
FlaskConical,
|
||||
FileText,
|
||||
Shield,
|
||||
Truck,
|
||||
Building2,
|
||||
BarChart3,
|
||||
Bell,
|
||||
Settings,
|
||||
ChevronRight,
|
||||
} from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
const NAV_ITEMS = [
|
||||
{ to: "/", label: "Dashboard", icon: LayoutDashboard, phase: 6 },
|
||||
{ to: "/assets", label: "Assets", icon: Package, phase: 2 },
|
||||
{ to: "/complaints", label: "Complaints", icon: AlertCircle, phase: 3 },
|
||||
{ to: "/pm", label: "Preventive Maintenance", icon: Wrench, phase: 4 },
|
||||
{ to: "/calibration", label: "Calibration", icon: FlaskConical, phase: 4 },
|
||||
{ to: "/contracts", label: "Contracts & Warranty", icon: FileText, phase: 5 },
|
||||
{ to: "/insurance", label: "Insurance", icon: Shield, phase: 5 },
|
||||
{ to: "/demo-equipment", label: "Demo Equipment", icon: Truck, phase: 5 },
|
||||
{ to: "/visits", label: "Department Visits", icon: Building2, phase: 5 },
|
||||
{ to: "/reports", label: "Reports", icon: BarChart3, phase: 7 },
|
||||
{ to: "/notifications", label: "Notifications", icon: Bell, phase: 6 },
|
||||
{ to: "/settings", label: "Settings", icon: Settings, phase: 1, adminOnly: true },
|
||||
];
|
||||
|
||||
export function Sidebar() {
|
||||
return (
|
||||
<aside className="w-56 lg:w-64 shrink-0 min-h-screen bg-[#1e3a8a] text-white flex flex-col">
|
||||
{/* Logo */}
|
||||
<div className="px-4 py-4 border-b border-blue-700 space-y-2">
|
||||
<div className="bg-white rounded-lg p-2">
|
||||
<img
|
||||
src="/logo.jpg"
|
||||
alt="Poona Hospital and Research Centre"
|
||||
className="w-full h-16 object-contain"
|
||||
/>
|
||||
</div>
|
||||
<div className="text-xs text-blue-200 text-center">
|
||||
Asset Management & Maintenance
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Navigation */}
|
||||
<nav className="flex-1 px-2 py-4 space-y-0.5 overflow-y-auto">
|
||||
{NAV_ITEMS.map(({ to, label, icon: Icon }) => (
|
||||
<NavLink
|
||||
key={to}
|
||||
to={to}
|
||||
end={to === "/"}
|
||||
className={({ isActive }) =>
|
||||
cn(
|
||||
"flex items-center gap-3 px-3 py-2.5 rounded-md text-sm transition-colors group",
|
||||
isActive
|
||||
? "bg-blue-700 text-white"
|
||||
: "text-blue-100 hover:bg-blue-700/50 hover:text-white"
|
||||
)
|
||||
}
|
||||
>
|
||||
<Icon className="h-4 w-4 shrink-0" />
|
||||
<span className="flex-1 truncate">{label}</span>
|
||||
<ChevronRight className="h-3 w-3 opacity-0 group-hover:opacity-50" />
|
||||
</NavLink>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
{/* Footer */}
|
||||
<div className="px-4 py-3 border-t border-blue-700 text-xs text-blue-300">
|
||||
Phase 0 — Scaffold
|
||||
</div>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
import { Search, Bell, ChevronDown, User } from "lucide-react";
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface TopBarProps {
|
||||
title?: string;
|
||||
}
|
||||
|
||||
export function TopBar({ title }: TopBarProps) {
|
||||
return (
|
||||
<header className="h-14 bg-white border-b border-gray-200 flex items-center px-4 gap-4 sticky top-0 z-10">
|
||||
{/* Page title */}
|
||||
{title && <h1 className="font-semibold text-gray-800 text-sm truncate mr-auto">{title}</h1>}
|
||||
|
||||
{/* Global search placeholder */}
|
||||
<div className={cn("relative ml-auto", title ? "" : "flex-1 max-w-md")}>
|
||||
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-gray-400" />
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Search assets, complaints... (Phase 8)"
|
||||
className="w-full pl-9 pr-4 py-1.5 text-sm border border-gray-200 rounded-md bg-gray-50 focus:outline-none focus:ring-2 focus:ring-blue-500 focus:bg-white"
|
||||
/>
|
||||
</div>
|
||||
|
||||
{/* Notification bell placeholder */}
|
||||
<button className="relative p-2 rounded-md hover:bg-gray-100 text-gray-500">
|
||||
<Bell className="h-5 w-5" />
|
||||
<span className="absolute top-1 right-1 h-2 w-2 rounded-full bg-red-500" />
|
||||
</button>
|
||||
|
||||
{/* User menu placeholder */}
|
||||
<button className="flex items-center gap-2 px-3 py-1.5 rounded-md hover:bg-gray-100 text-sm text-gray-700">
|
||||
<div className="h-7 w-7 rounded-full bg-[#1e3a8a] flex items-center justify-center text-white text-xs font-bold">
|
||||
<User className="h-4 w-4" />
|
||||
</div>
|
||||
<span className="hidden sm:block">Admin</span>
|
||||
<ChevronDown className="h-4 w-4 text-gray-400" />
|
||||
</button>
|
||||
</header>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { cn } from "@/lib/utils";
|
||||
|
||||
interface BadgeProps {
|
||||
variant?: "default" | "success" | "warning" | "danger" | "info" | "outline";
|
||||
className?: string;
|
||||
children: React.ReactNode;
|
||||
}
|
||||
|
||||
const variantClasses = {
|
||||
default: "bg-gray-100 text-gray-800",
|
||||
success: "bg-green-100 text-green-800",
|
||||
warning: "bg-yellow-100 text-yellow-800",
|
||||
danger: "bg-red-100 text-red-800",
|
||||
info: "bg-blue-100 text-blue-800",
|
||||
outline: "border border-gray-300 text-gray-700 bg-white",
|
||||
};
|
||||
|
||||
export function Badge({ variant = "default", className, children }: BadgeProps) {
|
||||
return (
|
||||
<span
|
||||
className={cn(
|
||||
"inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium",
|
||||
variantClasses[variant],
|
||||
className
|
||||
)}
|
||||
>
|
||||
{children}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
import { cn } from "@/lib/utils";
|
||||
import { ButtonHTMLAttributes, forwardRef } from "react";
|
||||
|
||||
export interface ButtonProps extends ButtonHTMLAttributes<HTMLButtonElement> {
|
||||
variant?: "default" | "outline" | "ghost" | "danger" | "success";
|
||||
size?: "sm" | "md" | "lg";
|
||||
loading?: boolean;
|
||||
}
|
||||
|
||||
const variantClasses = {
|
||||
default: "bg-[#1e3a8a] text-white hover:bg-[#1e3480] focus:ring-blue-500",
|
||||
outline: "border border-gray-300 bg-white text-gray-700 hover:bg-gray-50 focus:ring-blue-500",
|
||||
ghost: "text-gray-600 hover:bg-gray-100 focus:ring-gray-400",
|
||||
danger: "bg-red-600 text-white hover:bg-red-700 focus:ring-red-500",
|
||||
success: "bg-green-600 text-white hover:bg-green-700 focus:ring-green-500",
|
||||
};
|
||||
|
||||
const sizeClasses = {
|
||||
sm: "px-3 py-1.5 text-sm",
|
||||
md: "px-4 py-2 text-sm",
|
||||
lg: "px-6 py-3 text-base",
|
||||
};
|
||||
|
||||
export const Button = forwardRef<HTMLButtonElement, ButtonProps>(
|
||||
({ variant = "default", size = "md", loading, className, children, disabled, ...props }, ref) => (
|
||||
<button
|
||||
ref={ref}
|
||||
disabled={disabled || loading}
|
||||
className={cn(
|
||||
"inline-flex items-center justify-center rounded-md font-medium transition-colors",
|
||||
"focus:outline-none focus:ring-2 focus:ring-offset-1",
|
||||
"disabled:opacity-50 disabled:cursor-not-allowed",
|
||||
variantClasses[variant],
|
||||
sizeClasses[size],
|
||||
className
|
||||
)}
|
||||
{...props}
|
||||
>
|
||||
{loading && (
|
||||
<svg className="animate-spin -ml-1 mr-2 h-4 w-4" fill="none" viewBox="0 0 24 24">
|
||||
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4" />
|
||||
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z" />
|
||||
</svg>
|
||||
)}
|
||||
{children}
|
||||
</button>
|
||||
)
|
||||
);
|
||||
Button.displayName = "Button";
|
||||
Reference in New Issue
Block a user