"use client" import { useEffect, useState } from "react" import Link from "next/link" import { Copy, Pencil, Plus, Trash2 } from "lucide-react" import { toast } from "sonner" import { Button } from "@/shared/components/ui/button" import { Input } from "@/shared/components/ui/input" import { useAuthApi } from "@/shared/useApi" import { confirm } from "@/shared/components/confirm-dialog" import type { InspectionTemplate } from "@garage/api" export default function InspectionTemplatesPage() { const api = useAuthApi() const [items, setItems] = useState([]) const [search, setSearch] = useState("") const [loading, setLoading] = useState(true) const [creating, setCreating] = useState(false) const [newName, setNewName] = useState("") const load = async () => { setLoading(true) try { const res = await api.inspectionTemplates.list(search ? { search } : undefined) setItems(res.data ?? []) } catch (e: any) { toast.error(e?.message ?? "Failed to load templates") } finally { setLoading(false) } } useEffect(() => { load() }, []) const handleCreate = async () => { if (!newName.trim()) return try { await api.inspectionTemplates.create({ name: newName.trim(), is_active: true }) setNewName("") setCreating(false) toast.success("Template created") load() } catch (e: any) { toast.error(e?.message ?? "Failed to create template") } } const handleDuplicate = async (id: number) => { try { await api.inspectionTemplates.duplicate(id) toast.success("Template duplicated") load() } catch (e: any) { toast.error(e?.message ?? "Failed to duplicate template") } } const handleDelete = async (id: number, name: string) => { const confirmed = await confirm({ title: `Delete "${name}"?`, description: "This will remove the template and all its sections / checkpoints.", confirmLabel: "Delete", variant: "destructive", }) if (!confirmed) return try { await api.inspectionTemplates.destroy(id) toast.success("Template deleted") load() } catch (e: any) { toast.error(e?.message ?? "Failed to delete template") } } return (

Inspection Templates

setSearch(e.target.value)} onKeyDown={(e) => e.key === "Enter" && load()} className="w-56" />
{creating && (
setNewName(e.target.value)} onKeyDown={(e) => e.key === "Enter" && handleCreate()} className="max-w-md" />
)}
{loading && ( )} {!loading && items.length === 0 && ( )} {!loading && items.map((t) => { const sectionCount = t.sections?.length ?? 0 const cpCount = (t.sections ?? []).reduce((sum, s) => sum + (s.check_points?.length ?? 0), 0) return ( ) })}
Name Sections Checkpoints Status Actions
Loading…
No templates yet
{t.name} {sectionCount} {cpCount} {t.is_active ? "Active" : "Inactive"}
) }