"use client"; import { useState } from "react"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { Badge } from "@/components/ui/badge"; import { AdminDialog } from "@/components/ui/admin-dialog"; import { approveSalary, returnSalary, selectCandidate, returnSelection, approveInterviewWaiver, declineInterviewWaiver, } from "../crewing/applications/actions"; import { decideLeave } from "../crewing/leave/actions"; import { approveAppraisal } from "../crewing/appraisals/actions"; export type CrewApprovalKind = "SALARY" | "SELECTION" | "WAIVER" | "LEAVE" | "APPRAISAL"; export type CrewApprovalItem = { id: string; // applicationId, or leaveRequestId for LEAVE kind: CrewApprovalKind; candidateName: string; rank: string; requisitionCode: string; detail: string; link: string; }; const KIND_LABEL: Record = { SALARY: "Salary", SELECTION: "Selection", WAIVER: "Waiver", LEAVE: "Leave", APPRAISAL: "Appraisal" }; const KIND_VARIANT = { SALARY: "warning", SELECTION: "default", WAIVER: "secondary", LEAVE: "warning", APPRAISAL: "default" } as const; const approveFn: Record Promise<{ ok: true } | { error: string }>> = { SALARY: approveSalary, SELECTION: selectCandidate, WAIVER: approveInterviewWaiver, LEAVE: (id) => decideLeave(id, true), APPRAISAL: (id) => approveAppraisal(id, true), }; const returnFn: Record Promise<{ ok: true } | { error: string }>> = { SALARY: returnSalary, SELECTION: returnSelection, WAIVER: declineInterviewWaiver, LEAVE: (id, reason) => decideLeave(id, false, reason), APPRAISAL: (id, reason) => approveAppraisal(id, false, reason), }; function Row({ item }: { item: CrewApprovalItem }) { const router = useRouter(); const [pending, setPending] = useState(false); const [error, setError] = useState(""); const [returnOpen, setReturnOpen] = useState(false); const [reason, setReason] = useState(""); async function approve() { setPending(true); setError(""); const res = await approveFn[item.kind](item.id); setPending(false); if ("error" in res) setError(res.error); else router.refresh(); } async function doReturn(e: React.FormEvent) { e.preventDefault(); setPending(true); setError(""); const res = await returnFn[item.kind](item.id, reason); setPending(false); if ("error" in res) setError(res.error); else { setReturnOpen(false); router.refresh(); } } return ( {KIND_LABEL[item.kind]} {item.candidateName} {item.rank} ยท {item.requisitionCode} {item.detail}
{error &&

{error}

} setReturnOpen(false)}>