"use client"; import { useState } from "react"; import { useRouter } from "next/navigation"; import type { SeafarerDocType } from "@prisma/client"; import type { RankRow } from "./ranks-manager"; import { addRankDocRequirement, removeRankDocRequirement } from "./actions"; // Listed (not imported as a runtime enum) to keep @prisma/client out of the client bundle. const DOC_TYPES: { value: SeafarerDocType; label: string }[] = [ { value: "STCW", label: "STCW" }, { value: "AADHAAR", label: "Aadhaar" }, { value: "PAN", label: "PAN" }, { value: "PASSPORT", label: "Passport" }, { value: "CDC", label: "CDC" }, { value: "COC", label: "COC" }, { value: "PHOTOGRAPH", label: "Photograph" }, { value: "DRIVING_LICENSE", label: "Driving licence" }, { value: "MEDICAL_FITNESS", label: "Medical fitness" }, { value: "CONTRACT_LETTER", label: "Contract letter" }, ]; const DOC_LABEL = Object.fromEntries(DOC_TYPES.map((d) => [d.value, d.label])) as Record; const INPUT = "w-full rounded-lg border border-neutral-300 px-3 py-2 text-sm focus:border-primary-500 focus:outline-none focus:ring-2 focus:ring-primary-500/20"; export function RankDocPanel({ rank }: { rank: RankRow | null }) { const router = useRouter(); const [adding, setAdding] = useState(false); const [pending, setPending] = useState(false); const [error, setError] = useState(""); if (!rank) { return (
Select a rank to manage its required documents.
); } async function handleAdd(e: React.FormEvent) { e.preventDefault(); setPending(true); setError(""); const fd = new FormData(e.currentTarget); fd.set("rankId", rank!.id); const result = await addRankDocRequirement(fd); if ("error" in result) { setError(result.error); setPending(false); } else { setPending(false); setAdding(false); router.refresh(); } } async function handleRemove(id: string) { await removeRankDocRequirement(id); router.refresh(); } return (

Required documents

{rank.code} — {rank.name}

{adding && (
{error &&

{error}

}
)} {rank.docRequirements.length === 0 ? (

No required documents for this rank.

) : (
{rank.docRequirements.map((d) => (
{DOC_LABEL[d.docType] ?? d.docType} {d.note && {d.note}} {d.isMandatory ? "Mandatory" : "Conditional"}
))}
)}
); }